LuoGu: SP19975 APS2 - Amazing Prime Sequence (hard)
SPOJ: DIVFACT4 - Divisors of factorial (extreme)
对于 $i \le \sqrt n$ 的质数,统计 $[i + 1, n]$ 有多少个数最小质因数为 $i$ 。这可以在 Min_25 第一步过程中计算。对于更大的质数,贡献只有自己,考虑计算所有质数的和,这也可以用 Min_25 第一步计算。
查看代码
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
| #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 = 3e6 + 10; bool vis[N]; LL n, w[N]; int B, tot; ULL f[N], g[N], ps[N], ans; 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; } } for (int i = 1; i <= cnt; ++i) ps[i] = ps[i - 1] + p[i]; } 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; g[i] = (w[i] & 1 ? (w[i] + 1) / 2 * w[i] : w[i] / 2 * (w[i] + 1)) - 1; } for (int i = 1; i <= cnt && p[i] <= B; ++i) for (int j = 1; j <= tot && w[j] >= (LL)p[i] * p[i]; ++j) { int t = id(w[j] / p[i]); if (j == 1) ans += (ULL)p[i] * (f[t] - (i - 1)); f[j] -= f[t] - (i - 1); g[j] -= (ULL)p[i] * (g[t] - ps[i - 1]); } } int main () { init(); int T; read(T); while (T--) { read(n); B = sqrt(n); tot = 0; ans = 0; for (LL l = 1, r; l <= n; l = r + 1) { r = n / (n / l); w[id(n / l) = ++tot] = n / l; } solve(); write(ans + g[1]), puts(""); } return 0; }
|