P6055 [RC-02] GCD
易得答案为
$$
\sum _ {i = 1} ^ n \mu (i) \lfloor \frac n i \rfloor ^ 3
$$
用杜教筛筛 $\mu$ 。
查看代码
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
| #include <cstdio> #include <map> 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; const int N = 2e6 + 10, mod = 998244353; bool vis[N]; int n, cnt, p[N], f[N]; map <int, int> F; void init () { f[1] = 1; vis[1] = true; for (int i = 2; i < N; ++i) { if (!vis[i]) f[p[++cnt] = i] = -1; for (int j = 1; j <= cnt && i * p[j] < N; ++j) { vis[i * p[j]] = true; if (i % p[j] == 0) break; f[i * p[j]] = -f[i]; } } for (int i = 2; i < N; ++i) f[i] += f[i - 1]; } int calc (int x) { if (x < N) return f[x]; if (F.count(x)) return F[x]; int res = 1; for (int l = 2, r; l <= x; l = r + 1) { r = x / (x / l); res -= (r - l + 1) * calc(x / l); } return F[x] = res; } int a3 (int x) { return (LL)x * x % mod * x % mod; } int main () { init(); read(n); int res = 0; for (int l = 1, r; l <= n; l = r + 1) { r = n / (n / l); res = ((LL)a3(n / l) * (calc(r) - calc(l - 1)) + res) % mod; } write((res + mod) % mod); return 0; }
|