P6271 [湖北省队互测2014]一个人的数论
$$
\begin {aligned}
\sum _ {i = 1} ^ n i ^ d [(i, n) = 1] & = \sum _ {i = 1} ^ n i ^ d \sum _ {k | (i, n)} \mu(k) \\
& = \sum _ {k | n} \mu(k) \sum _ {i = 1} ^ {\frac n k} (ki) ^ d \\
& = \sum _ {k | n} k ^ d \mu(k) \sum _ {i = 1} ^ {\frac n k} i ^ d
\end {aligned}
$$
后面的式子是一个自然数幂和,$\sum _ {i = 0} ^ {n - 1} i ^ d$ ,可以用伯努利数构建为一个 $d + 1$ 次的多项式。记 $i$ 次的系数为 $f _ i$ ,那么有:
$$
\begin {aligned}
\sum _ {i = 1} ^ n i ^ d [(i, n) = 1] & = \sum _ {k | n} k ^ d \mu(k) \sum _ {i = 1} ^ {\frac n k} i ^ d \\
& = \sum _ {k | n} k ^ d \mu(k) ((\frac n k) ^ d + \sum _ {i = 1} ^ {d + 1} f _ i (\frac n k) ^ i)\\
& = \sum _ {k | n} k ^ d \mu(k) \sum _ {i = 1} ^ {d + 1} f _ i (\frac n k) ^ i + \sum _ {k | n} k ^ d \mu(k) (\frac n k) ^ d\\
& = \sum _ {i = 1} ^ {d + 1} f _ i n _ i \sum _ {k | n}\mu(k) k ^ {d - i} + \sum _ {k | n} \mu(k) n ^ d
\end {aligned}
$$
对于 $\sum _ {k | n} \mu(k) n ^ d$ ,答案为 $[n = 1]$ ,在本题中恒为 $0$ 。
对于 $ \sum _ {i = 1} ^ {d + 1} f _ i n _ i \sum _ {k | n}\mu(k) k ^ {d - i}$ ,容易发现,后面是一个积性函数,可以将所有质数的值乘起来即可,又注意到,对于质数的大于 $1$ 的次幂,$\mu(k) = 1$ ,没有贡献。因此只有质数处有 $-p ^ {d - i}$ 的贡献。于是可以在 $O(dw)$ 的时间内完成。
查看代码
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
| #include <cstdio> 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 = 110, mod = 1e9 + 7; int d, w, p[N], c[N]; int inv[N], B[N], C[N][N], f[N]; int binpow (int b, int k) { if (k < 0) k += mod - 1; int res = 1; for (; k; k >>= 1, b = (LL)b * b % mod) if (k & 1) res = (LL)res * b % mod; return res; } void init() { for (int i = 0; i <= d + 1; ++i) { C[i][0] = 1; for (int j = 1; j <= i; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; } inv[1] = 1; for (int i = 2; i <= d + 1; ++i) inv[i] = -(LL)(mod / i) * inv[mod % i] % mod; B[0] = 1; for (int i = 1; i <= d; ++i) { int t = 0; for (int j = 0; j < i; ++j) t = (t + (LL)C[i + 1][j] * B[j]) % mod; B[i] = -(LL)t * inv[i + 1] % mod; } for (int i = 0; i <= d; ++i) f[d + 1 - i] = (LL)C[d + 1][i] * B[i] % mod * inv[d + 1] % mod; } int calc (int x) { int res = 1; for (int i = 1; i <= w; ++i) res = res * (1ll - binpow(p[i], x)) % mod; return res; } int main () { read(d, w); init(); int n = 1; for (int i = 1; i <= w; ++i) { read(p[i], c[i]); n = (LL)n * binpow(p[i], c[i]) % mod; } int res = 0; for (int i = 1, t = n; i <= d + 1; ++i, t = (LL)t * n % mod) res = (res + (LL)f[i] * t % mod * calc(d - i)) % mod; write((res + mod) % mod); return 0; }
|