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 83 84 85 86 87
| #pragma GCC optimize ("O2") #include <cstdio> #include <cmath> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long LL; typedef unsigned long long ULL; const int N = 2e5 + 10; LL n, m, w[N]; bool vis[N]; int B, tot; ULL f[N]; int cnt, p[N], id1[N], id2[N]; void init () { vis[1] = true; for (int i = 2; i < N; ++i) { if (!vis[i]) p[++cnt] = i; for (int j = 1; j <= cnt && i * p[j] < N; ++j) { vis[i * p[j]] = true; if (i % p[j] == 0) break; } } } int &id (LL x) { return x <= B ? id1[x] : id2[n / x]; } void solve () { for (int i = 1; i <= tot; ++i) f[i] = (w[i] - 1); for (int i = 1; i <= cnt; ++i) for (int j = 1; j <= tot && w[j] >= (LL)p[i] * p[i]; ++j) f[j] -= f[id(w[j] / p[i])] - (i - 1); } ULL S (LL a, int b) { if (p[b] >= a) return 0; ULL res = (f[id(a)] - b * (m + 1)); for (int i = b + 1; i <= cnt && (LL)p[i] * p[i] <= a; ++i) { LL t = p[i]; for (int k = 1; t <= a; ++k, t *= p[i]) res += (m * k + 1) * (S(a / t, i) + (k > 1)); } return res; } int main () { init(); int T; read(T); while (T--) { tot = 0; read(n, m); B = sqrt(n); for (LL l = 1, r; l <= n; l = r + 1) { r = n / (n / l); w[id(n / l) = ++tot] = n / l; } solve(); for (int i = 1; i <= tot; ++i) f[i] *= m + 1; write((S(n, 0) + 1)), puts(""); } return 0; }
|