1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <iostream> #include <algorithm> #include <vector> #include <cmath> #define maxn 100010 #define ll long long #define INF 1000000000000000000 #define lowbit(i) ((i) & (-i)) using namespace std;
bool isp[maxn]; int pri[maxn], cnt, phi[maxn]; ll s[maxn]; void init_isp(int n) { phi[1] = 1; for (int i = 2; i <= n; ++i) { if (!isp[i]) pri[++cnt] = i, phi[i] = i - 1; for (int j = 1; j <= cnt && i * pri[j] <= n; ++j) { isp[i * pri[j]] = 1; if (i % pri[j] == 0) { phi[i * pri[j]] = phi[i] * pri[j]; break; } phi[i * pri[j]] = phi[i] * (pri[j] - 1); } } for (int i = 1; i <= n; ++i) s[i] = phi[i] + s[i - 1]; }
int n, k, q;
vector<int> D[maxn];
int l, r; ll sum; inline void addL(int x) { sum += s[r / x];} inline void addR(int x) { for (auto t : D[x]) { if (t < l) break; sum += s[r / t] - s[r / t - 1]; } } inline void delL(int x) { sum -= s[r / x]; } inline void delR(int x) { for (auto t : D[x]) { if (t < l) break; sum -= s[r / t] - s[r / t - 1]; } } void move(int L, int R) { while (r < R) ++r, addR(r); while (l > L) --l, addL(l); while (r > R) delR(r), --r; while (l < L) delL(l), ++l; }
ll f[20][maxn]; void solve(int l, int r, int L, int R, ll *f, ll *g) { if (l > r) return ; int m = l + r >> 1, p = L; for (int i = L; i <= min(R, m - 1); ++i) { move(i + 1, m); if (g[i] + sum < f[m]) f[m] = g[i] + sum, p = i; } solve(l, m - 1, L, p, f, g), solve(m + 1, r, p, R, f, g); }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
n = 100000; k = 18; init_isp(n); for (int i = 1; i <= n; ++i) for (int j = i; j <= n; j += i) D[j].push_back(i); for (int i = 1; i <= n; ++i) reverse(D[i].begin(), D[i].end()); for (int i = 0; i <= n; ++i) for (int j = 0; j <= k; ++j) f[j][i] = INF; f[0][0] = 0; l = r = 1, sum = s[1]; for (int o = 1; o <= k; ++o) solve(1, n, 0, n - 1, f[o], f[o - 1]); cin >> q; for (int i = 1; i <= q; ++i) { int x, y; cin >> x >> y; if (y > 30 || (1 << y) > x) cout << x << "\n"; else cout << f[y][x] << "\n"; } return 0; }
|