Problem I. Delightful Formulas
大概就是照着题解抄了一遍吧,这道题太神仙了……
ai=ik,si=∑j=1iajcalc∑i=1nsi[gcd(i,n)=1]∑d∣nμ(d)∑i=1ndsida_i = i ^ k, s_i = \sum_{j = 1} ^{i} a_j\\ calc\ \sum_{i = 1} ^{n} s_i[\gcd(i, n) = 1]\\ \sum_{d \mid n} \mu(d) \sum_{i = 1} ^{\frac{n}{d}} s_{id}\\ ai=ik,si=j=1∑iajcalc i=1∑nsi[gcd(i,n)=1]d∣n∑μ(d)i=1∑dnsid
伯努利数对自然幂次求和有:
Sk(n)=∑i=0nik=1k+1∑i=0kCk+1iBi(n+1)k+i−1不难发现其为一个k+1阶多项式,设Sk(x)=∑i=0k+1aixiSk(n)=1k+1∑i=1k+1Ck+1iBk+1−i(n+1)i1k+1∑i=0k+1Ck+1iBk+1−i(n+1)i−1k+1Bk+11k+1∑i=0k+1Ck+1iBk+1−i∑j=0iCijnj−1k+1Bk+11k+1∑j=0k+1nj∑i=jk+1Ck+1iBk+1−iCij−1k+1Bk+1S_k(n) = \sum_{i = 0} ^{n} i ^ k = \frac{1}{k + 1} \sum_{i = 0} ^{k}C_{k + 1} ^{i} B_i (n + 1) ^{k + i - 1}\\ 不难发现其为一个k + 1阶多项式,设S_k(x) = \sum_{i = 0} ^{k + 1} a_i x ^{i}\\ S_k(n) = \frac{1}{k + 1} \sum_{i = 1} ^{k + 1} C_{k + 1} ^{i} B_{k + 1 - i} (n + 1) ^{i}\\ \frac{1}{k + 1} \sum_{i = 0} ^{k + 1} C_{k + 1} ^{i} B_{k + 1 - i} (n + 1) ^{i} - \frac{1}{k + 1} B_{k + 1}\\ \frac{1}{k + 1} \sum_{i = 0} ^{k + 1} C_{k + 1} ^{i} B_{k + 1 - i} \sum_{j = 0} ^{i} C_{i} ^{j} n ^j - \frac{1}{k + 1} B_{k + 1}\\ \frac{1}{k + 1} \sum_{j = 0} ^{k + 1} n ^ j \sum_{i = j} ^{k + 1} C_{k + 1} ^{i} B_{k + 1 - i} C_i ^ j - \frac{1}{k + 1} B_{k + 1}\\ Sk(n)=i=0∑nik=k+11i=0∑kCk+1iBi(n+1)k+i−1不难发现其为一个k+1阶多项式,设Sk(x)=i=0∑k+1aixiSk(n)=k+11i=1∑k+1Ck+1iBk+1−i(n+1)ik+11i=0∑k+1Ck+1iBk+1−i(n+1)i−k+11Bk+1k+11i=0∑k+1Ck+1iBk+1−ij=0∑iCijnj−k+11Bk+1k+11j=0∑k+1nji=j∑k+1Ck+1iBk+1−iCij−k+11Bk+1
有a0=0,j≥1a_0 = 0, j \geq 1a0=0,j≥1时有:
1k+1∑i=jk+1Ck+1iBk+1−iCij1k+1∑i=0k+1−jck+1iBiCk+1−ij1k+1∑i=0k+1−j(k+1)!i!(k+1−i)!(k+1−i)!j!(k+1−i−j)!Bi1k+1∑i=0k+1−j(k+1)!(k+1−j)!i!(k+1−j)!j!(k+1−i−j)!Bi1k+1∑i=0k+1−jCk+1−jiCk+1jBi1k+1Ck+1j∑i=0k+1−jCk+1−jiBi1k+1Ck+1j(∑i=0k−jCk−j+1iBi+Bk+1−j)有∑i=0kCk+1iBi=0,k≥1上式子为1k+1Ck+1jBk+1−j\frac{1}{k + 1} \sum_{i = j} ^{k + 1} C_{k + 1} ^{i} B_{k + 1 - i} C_{i} ^ {j}\\ \frac{1}{k + 1} \sum_{i = 0} ^{k + 1 - j}c_{k + 1} ^{i} B_{i} C_{k + 1 - i} ^{j}\\ \frac{1}{k + 1} \sum_{i = 0} ^{k + 1 - j} \frac{(k + 1)!}{i!(k + 1 - i)!} \frac{(k + 1 - i)!}{j! (k + 1 -i - j)!} B_i\\ \frac{1}{k + 1} \sum_{i = 0} ^{k + 1 - j} \frac{(k + 1)! (k + 1 - j)!}{i!(k + 1 - j)!j!(k + 1 - i - j)!} B_i\\ \frac{1}{k + 1} \sum_{i = 0} ^{k + 1 - j} C_{k + 1 - j} ^{i} C_{k + 1} ^{j} B_i\\ \frac{1}{k + 1} C_{k + 1} ^{j} \sum_{i = 0} ^{k + 1 - j} C_{k + 1 - j} ^{i} B_i\\ \frac{1}{k + 1} C_{k + 1} ^{j}\left(\sum_{i = 0} ^{k - j} C_{k - j + 1} ^{i} B_i + B_{k + 1 - j}\right)\\ 有\sum_{i = 0} ^{k} C_{k + 1} ^{i} B_i = 0, k \geq 1\\ 上式子为\frac{1}{k + 1} C_{k + 1} ^{j} B_{k + 1 - j}\\ k+11i=j∑k+1Ck+1iBk+1−iCijk+11i=0∑k+1−jck+1iBiCk+1−ijk+11i=0∑k+1−ji!(k+1−i)!(k+1)!j!(k+1−i−j)!(k+1−i)!Bik+11i=0∑k+1−ji!(k+1−j)!j!(k+1−i−j)!(k+1)!(k+1−j)!Bik+11i=0∑k+1−jCk+1−jiCk+1jBik+11Ck+1ji=0∑k+1−jCk+1−jiBik+11Ck+1j(i=0∑k−jCk−j+1iBi+Bk+1−j)有i=0∑kCk+1iBi=0,k≥1上式子为k+11Ck+1jBk+1−j
综上有:
{a0=0ai=1k+1Ck+1iBk+1−i1≤i<kak=B1+B0ak+1=1k+1\left\{ \begin{aligned} a_0 & = 0\\ a_i & = \frac{1}{k + 1}C_{k + 1} ^{i} B_{k + 1 - i} & 1 \leq i < k\\ a_k & = B_1 + B_0 \\ a_{k + 1} & = \frac{1}{k + 1}\\ \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎧a0aiakak+1=0=k+11Ck+1iBk+1−i=B1+B0=k+111≤i<k
再考虑对原式化简:
ak,ja_{k, j}ak,j为kkk次自然数幂之和的多项式的第jjj项,也就是上面化简的。
∑d∣nμ(d)∑i=1ndsid∑d∣nμ(d)∑i=1nd∑j=0k+1ak,jijdj∑j=0k+1ak,j∑d∣nμ(d)dj∑i=1ndij∑j=0k+1ak,j∑d∣nμ(d)dj∑i=0j+1aj,inid−i∑d∣nμ(d)∑i=−1k+1di∑x=0k+1∑y=0k+1[x−y=i]ak,xax,yny∑d∣nμ(d)∑i=−1k+1diFi+1∑i=−1k+1Fi+1∑d∣nμ(d)di\sum_{d \mid n} \mu(d) \sum_{i = 1} ^{\frac{n}{d}} s_{id}\\ \sum_{d \mid n} \mu(d) \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 0} ^{k + 1} a_{k, j} i ^{j} d ^{j}\\ \sum_{j = 0} ^{k + 1} a_{k, j} \sum_{d \mid n} \mu(d) d ^ j \sum_{i = 1} ^{\frac{n}{d}} i ^ j\\ \sum_{j = 0} ^{k + 1} a_{k, j} \sum_{d \mid n} \mu(d) d ^ j \sum_{i = 0} ^{j + 1} a_{j, i} n ^{i} d ^{-i}\\ \sum_{d \mid n} \mu(d) \sum_{i = -1} ^{k + 1} d ^{i} \sum_{x = 0} ^{k + 1} \sum_{y = 0} ^{k + 1}[x - y = i] a_{k, x}a_{x, y}n ^{y}\\ \sum_{d \mid n} \mu(d) \sum_{i = -1} ^{k + 1} d ^{i} F_{i + 1}\\ \sum_{i = -1} ^{k + 1} F_{i + 1} \sum_{d \mid n} \mu(d) d ^{i}\\ d∣n∑μ(d)i=1∑dnsidd∣n∑μ(d)i=1∑dnj=0∑k+1ak,jijdjj=0∑k+1ak,jd∣n∑μ(d)dji=1∑dnijj=0∑k+1ak,jd∣n∑μ(d)dji=0∑j+1aj,inid−id∣n∑μ(d)i=−1∑k+1dix=0∑k+1y=0∑k+1[x−y=i]ak,xax,ynyd∣n∑μ(d)i=−1∑k+1diFi+1i=−1∑k+1Fi+1d∣n∑μ(d)di
对f(n)=∑d∣nμ(d)dif(n) = \sum\limits_{d \mid n} \mu(d) d ^ if(n)=d∣n∑μ(d)di,考虑其积性f(1)=1,f(p)=μ(1)+μ(p)pi=1−pi,f(pk)=f(p)f(1) = 1, f(p) = \mu(1) + \mu(p) p ^ i = 1 - p ^ i, f(p ^ k) =f(p)f(1)=1,f(p)=μ(1)+μ(p)pi=1−pi,f(pk)=f(p),
所以答案为∑i=−1k+1Fi+1∑d∣nμ(d)di\sum\limits_{i = -1} ^{k + 1} F_{i + 1} \sum\limits_{d \mid n} \mu(d) d ^ ii=−1∑k+1Fi+1d∣n∑μ(d)di,考虑如何求解Fi+1F_{i + 1}Fi+1。
Fi=∑x=0k+1∑y=0k+1[x−y=i−1]ak,xax,yny∑x=0k+1ak,xax,x−i+1nx−i+1∑x=1k+1ak,xax,x−i+1nx−i+1∑x=0kak,x+1,ax+1,x−i+2nx−i+2∑x=0k1k+1Ck+1x+1Bk−x1x+2Cx+2iBinx−i+2∑x=0k1k+1(k+1)!(x+1)!(k−x)!Bk−x1x+2(x+2)!i!(x+2−i)!Binx−i+2k!Bii!∑x=0kBk−x(k−x)!nx−i+2(x−i+2)!F_i = \sum\limits_{x = 0} ^{k + 1} \sum\limits_{y = 0} ^{k + 1}[x - y = i - 1] a_{k, x}a_{x, y} n ^ y\\ \sum_{x = 0} ^{k + 1} a_{k, x} a_{x, x - i + 1} n ^{x - i + 1}\\ \sum_{x = 1} ^{k + 1} a_{k, x} a_{x, x - i + 1} n ^{x - i + 1}\\ \sum_{x = 0} ^{k} a_{k, x + 1}, a_{x + 1, x - i + 2} n ^{x - i + 2}\\ \sum_{x = 0} ^{k} \frac{1}{k + 1}C_{k + 1} ^{x + 1} B_{k - x} \frac{1}{x + 2} C_{x + 2} ^{i} B_{i} n ^{x - i + 2}\\ \sum_{x = 0} ^{k} \frac{1}{k + 1} \frac{(k + 1)!}{(x + 1)!(k - x)!} B_{k - x} \frac{1}{x + 2} \frac{(x + 2)!}{i!(x + 2 - i)!} B_i n ^{x - i + 2}\\ \frac{k! B_i}{i!} \sum_{x = 0} ^{k} \frac{B_{k - x}}{(k - x)!} \frac{n ^{x - i + 2}}{(x - i + 2)!}\\ Fi=x=0∑k+1y=0∑k+1[x−y=i−1]ak,xax,ynyx=0∑k+1ak,xax,x−i+1nx−i+1x=1∑k+1ak,xax,x−i+1nx−i+1x=0∑kak,x+1,ax+1,x−i+2nx−i+2x=0∑kk+11Ck+1x+1Bk−xx+21Cx+2iBinx−i+2x=0∑kk+11(x+1)!(k−x)!(k+1)!Bk−xx+21i!(x+2−i)!(x+2)!Binx−i+2i!k!Bix=0∑k(k−x)!Bk−x(x−i+2)!nx−i+2
由此我们得到一个卷积得形式,可O(klogk)O(k \log k)O(klogk)求解。
#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10, mod = 998244353;int r[N], t[N], B[N], b[N], fac[N], ifac[N];int quick_pow(int a, int n) {int ans = 1;while (n) {if (n & 1) {ans = 1ll * a * ans % mod;}a = 1ll * a * a % mod;n >>= 1;}return ans;
}void get_r(int lim) {for (int i = 0; i < lim; i++) {r[i] = (i & 1) * (lim >> 1) + (r[i >> 1] >> 1);}
}void NTT(int *f, int lim, int rev) {for (int i = 0; i < lim; i++) {if (i < r[i]) {swap(f[i], f[r[i]]);}}for (int mid = 1; mid < lim; mid <<= 1) {int wn = quick_pow(3, (mod - 1) / (mid << 1));for (int len = mid << 1, cur = 0; cur < lim; cur += len) {int w = 1;for (int k = 0; k < mid; k++, w = 1ll * w * wn % mod) {int x = f[cur + k], y = 1ll * w * f[cur + mid + k] % mod;f[cur + k] = (x + y) % mod, f[cur + mid + k] = (x - y + mod) % mod;}}}if (rev == -1) {int inv = quick_pow(lim, mod - 2);reverse(f + 1, f + lim);for (int i = 0; i < lim; i++) {f[i] = 1ll * f[i] * inv % mod;}}
}void polyinv(int *f, int *g, int n) {if (n == 1) {g[0] = quick_pow(f[0], mod - 2);return ;}polyinv(f, g, n + 1 >> 1);for (int i = 0; i < n; i++) {t[i] = f[i];}int lim = 1;while (lim < 2 * n) {lim <<= 1;}get_r(lim);NTT(t, lim, 1);NTT(g, lim, 1);for (int i = 0; i < lim; i++) {int cur = (2 - 1ll * g[i] * t[i] % mod + mod) % mod;g[i] = 1ll * g[i] * cur % mod;t[i] = 0;}NTT(g, lim, -1);for (int i = n; i < lim; i++) {g[i] = 0;}
}void init() {fac[0] = 1;for (int i = 1; i < N; i++) {fac[i] = 1ll * fac[i - 1] * i % mod;}ifac[N - 1] = quick_pow(fac[N - 1], mod - 2);for (int i = N - 2; i >= 0; i--) {ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;}int n = 1e5 + 10, len = 1;for (int i = 0; i < n; i++) {B[i] = ifac[i + 1];}polyinv(B, b, n);for (int i = 0; i < n; i++) {B[i] = 1ll * b[i] * fac[i] % mod;b[i] = 0;}B[1] = mod - B[1];
}int k, m, n, a[N], num[N], c[N], d[N], pw[N];void polymult(int *a, int n, int *b, int m) {int lim = 1;while (lim < n + m) {lim <<= 1;}get_r(lim);for (int i = n; i < lim; i++) {a[i] = 0;}for (int i = m; i < lim; i++) {b[i] = 0;}NTT(a, lim, 1), NTT(b, lim, 1);for (int i = 0; i < lim; i++) {a[i] = 1ll * a[i] * b[i] % mod;}NTT(a, lim, -1);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();int T;scanf("%d", &T);while (T--) {scanf("%d %d", &k, &m);n = 1;for (int i = 1; i <= m; i++) {scanf("%d %d", &a[i], &num[i]);n = 1ll * n * quick_pow(a[i], num[i]) % mod;}int lim = 1;while (lim < k * 2 + 10) {lim <<= 1;}for (int i = 0; i <= lim; i++) {c[i] = d[i] = 0;}for (int i = 0; i <= k; i++) {c[k + 1 - i] = 1ll * fac[k] * ifac[i] % mod * B[i] % mod;}int pow = 1;for (int i = 1; i <= k + 2; i++) {pow = 1ll * pow * n % mod;d[k + 2 - i] = 1ll * pow * ifac[i] % mod;}polymult(c, lim, d, lim);for (int i = 1; i <= m; i++) {pw[i] = quick_pow(a[i], mod - 2);}int ans = 0;for (int d = -1; d <= k; d++) {int gg = 1ll * c[d + k + 2] * B[d + 1] % mod * ifac[d + 1] % mod;for (int i = 1; i <= m; i++) {gg = 1ll * gg * (1 - pw[i] + mod) % mod;pw[i] = 1ll * pw[i] * a[i] % mod;}ans = (ans + gg) % mod;}printf("%d\n", ans);}return 0;
}