单位根反演
一个等式:[n∣a]=1n∑k=0n−1wnak[n \mid a] = \frac{1}{n} \sum\limits_{k = 0} ^{n - 1}w_n ^{ak}[n∣a]=n1k=0∑n−1wnak
证明:
wnaw_n ^ awna是nnn次单位根的aaa次方,所以这里是一个公比为wnaw_n ^ awna的等比数列,
考虑公比不为111,等比数列求和1nwn0(wnan−1)wna−1\frac{1}{n} \frac{w_n ^ 0(w_n ^ {an} - 1)}{w_n ^ a - 1}n1wna−1wn0(wnan−1), 有wnan−1=w1a−1=0w_n ^{an} - 1 = w_1 ^ a - 1 = 0wnan−1=w1a−1=0,所以该等比数列求和为000,
如果n∣an \mid an∣a,有wnak=w1ank=1w_n ^ {ak} = w_{1} ^{\frac{a}{n}k} = 1wnak=w1nak=1,所以有[n∣a]=1n∑k=0n−11=1[n \mid a] = \frac{1}{n} \sum\limits_{k = 0} ^{n - 1} 1 = 1[n∣a]=n1k=0∑n−11=1。
而有下面的推论:
a≡b(modn)a \equiv b \pmod{ n}a≡b(modn),有a−b≡0(modn)a - b \equiv 0 \pmod{n}a−b≡0(modn),[n∣a−b]=1n∑k=0n−1wnkawn−kb[n \mid a - b] = \frac{1} {n} \sum\limits_{k = 0} ^{n - 1} w_n ^{ka} w_{n} ^{-kb}[n∣a−b]=n1k=0∑n−1wnkawn−kb。
#6485. LJJ 学二项式定理
∑i=0n(in)siaimod4∑i=0n(in)si∑j=03aj[i≡j(mod4)]∑i=0n(in)si∑j=03aj14∑k=03w4k(i−j)14∑j=03aj∑k=03w4−kj∑i=0n(in)siw4ki14∑j=03aj∑k=03w4−kj(sw4k+1)n\sum_{i = 0} ^{n} (_i ^ n) s ^ i a_{i\mod4}\\ \sum_{i = 0} ^{n} (_i ^ n) s ^ i \sum_{j = 0} ^{3} a_j[i \equiv j \pmod{4}]\\ \sum_{i = 0} ^{n} (_i ^ n) s ^ i \sum_{j = 0} ^{3} a_j \frac{1}{4} \sum_{k = 0} ^{3} w_{4} ^{k(i - j)}\\ \frac{1}{4} \sum_{j = 0} ^{3} a_j \sum_{k = 0} ^{3} w_4 ^{-kj} \sum_{i = 0} ^{n} (_i ^ n) s ^ i w_4 ^{ki}\\ \frac{1}{4} \sum_{j = 0} ^{3} a_j \sum_{k = 0} ^{3} w_4 ^{-kj} (sw_4 ^ k + 1) ^ n\\ i=0∑n(in)siaimod4i=0∑n(in)sij=0∑3aj[i≡j(mod4)]i=0∑n(in)sij=0∑3aj41k=0∑3w4k(i−j)41j=0∑3ajk=0∑3w4−kji=0∑n(in)siw4ki41j=0∑3ajk=0∑3w4−kj(sw4k+1)n
有998244353998244353998244353下的单位根是333,所以w41=3mod−14w_4 ^ 1 = 3 ^{\frac{mod - 1}{4}}w41=34mod−1,(w41)4=3mod−1=1(w_4 ^ 1) ^ 4 = 3 ^{mod - 1} = 1(w41)4=3mod−1=1,快速幂即可。
#include <bits/stdc++.h>using namespace std;const int mod = 998244353, w[4] = {1, 911660635, 998244352, 86583718};int quick_pow(int a, int n) {int ans = 1;while (n) {if (n & 1) {ans = 1ll * ans * a % mod;}a = 1ll * a * a % mod;n >>= 1;}return ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);int T, inv4 = quick_pow(4, mod - 2);scanf("%d", &T);while (T--) {long long n, ans = 0, sum, s, a[10];scanf("%lld %lld %lld %lld %lld %lld", &n, &s, &a[0], &a[1], &a[2], &a[3]);n %= mod - 1;for (int j = 0; j < 4; j++) {sum = 0;for (int k = 0; k < 4; k++) {int cur = (4 - j * k % 4) % 4;sum = (sum + 1ll * w[cur] * quick_pow(1ll * s * w[k] % mod + 1, n) % mod) % mod;}ans = (ans + sum * a[j] % mod) % mod;}printf("%lld\n", ans * quick_pow(4, mod - 2) % mod);}return 0;
}
P5591 小猪佩奇学数学
∑i=0n(in)×pi×⌊ik⌋⌊ik⌋=i−i%kk1k∑i=0n(in)×pi×(i−i%k)1k∑i=0n(in)×pi×i−1k∑i=0n(in)×pi(imodk)\sum_{i = 0} ^{n} (_i ^ n) \times p ^ i \times \lfloor \frac{i}{k} \rfloor\\ \lfloor \frac{i}{k} \rfloor = \frac{i - i \% k}{k}\\ \frac{1}{k} \sum_{i = 0} ^{n} (_i ^ n) \times p ^ i \times (i - i \% k)\\ \frac{1}{k} \sum_{i = 0} ^{n} (_i ^ n) \times p ^ i \times i - \frac{1}{k} \sum_{i = 0} ^{n} (_i ^ n) \times p ^ i (i \mod k)\\ i=0∑n(in)×pi×⌊ki⌋⌊ki⌋=ki−i%kk1i=0∑n(in)×pi×(i−i%k)k1i=0∑n(in)×pi×i−k1i=0∑n(in)×pi(imodk)
考虑前项化简:
1k∑i=0n(in)×pi×i∑i=0n(in)×i=∑i=0nn!i!(n−i)!i=∑i=1nn!(i−1)!(n−i)!n∑i=1n(n−1)!(i−1)!(n−i)!=n∑i=1n(i−1n−1)原式npk∑i=1n(i−1n−1)pi−1=npk(p+1)n−1\frac{1}{k} \sum_{i = 0} ^{n} (_i ^ n) \times p ^ i \times i\\ \sum_{i = 0} ^{n} (_i ^ n) \times i = \sum_{i = 0} ^{n} \frac{n!}{i!(n - i)!} i = \sum_{i = 1} ^{n} \frac{n!}{(i - 1)!(n - i)!}\\ n\sum_{i = 1} ^{n} \frac{(n - 1)!}{(i - 1)!(n - i)!} = n \sum_{i = 1} ^{n} (_{i - 1} ^{n - 1})\\ 原式\frac{np}{k} \sum_{i = 1} ^{n} (_{i - 1} ^{n - 1}) p ^{i - 1} = \frac{np}{k} (p + 1) ^{n - 1}\\ k1i=0∑n(in)×pi×ii=0∑n(in)×i=i=0∑ni!(n−i)!n!i=i=1∑n(i−1)!(n−i)!n!ni=1∑n(i−1)!(n−i)!(n−1)!=ni=1∑n(i−1n−1)原式knpi=1∑n(i−1n−1)pi−1=knp(p+1)n−1
考虑后项化简:
1k∑d=0k−1d∑i=0n(in)pi[k∣i−d]对[k∣i−d]进行单位根反演1k∑d=0k−1d∑i=0n(in)×pi×1k∑j=0k−1wkj(i−d)1k2∑d=0k−1d∑j=0k−1wk−jd∑i=0n(in)piwkij1k2∑d=0k−1d∑j=0k−1wk−jd(pwkj+1)n1k2∑j=0k−1(p×wkj+1)n∑d=0k−1d×wk−jd\frac{1}{k} \sum_{d = 0} ^{k - 1} d \sum_{i = 0} ^{n} (_i ^ n) p ^ i[k \mid i - d]\\ 对[k \mid i - d]进行单位根反演\\ \frac{1}{k} \sum_{d = 0} ^{k - 1} d \sum_{i = 0} ^{n} (_i ^ n) \times p ^ i \times \frac{1}{k} \sum_{j = 0} ^{k - 1} w_k ^{j(i- d)}\\ \frac{1}{k ^ 2}\sum_{d = 0} ^{k - 1} d \sum_{j = 0} ^{k - 1} w_{k} ^{-jd} \sum_{i = 0} ^{n} (_i ^ n) p ^ i w_{k} ^{ij}\\ \frac{1}{k ^ 2} \sum_{d = 0} ^{k - 1} d \sum_{j = 0} ^{k - 1} w_{k} ^{-jd} (pw_k ^{j} + 1) ^{n}\\ \frac{1}{k ^ 2} \sum_{j = 0} ^{k - 1} (p \times w_k ^ j + 1) ^n \sum_{d = 0} ^{k - 1} d \times w_{k} ^{-jd}\\ k1d=0∑k−1di=0∑n(in)pi[k∣i−d]对[k∣i−d]进行单位根反演k1d=0∑k−1di=0∑n(in)×pi×k1j=0∑k−1wkj(i−d)k21d=0∑k−1dj=0∑k−1wk−jdi=0∑n(in)piwkijk21d=0∑k−1dj=0∑k−1wk−jd(pwkj+1)nk21j=0∑k−1(p×wkj+1)nd=0∑k−1d×wk−jd
∑d=0k−1d×wk−jd\sum\limits_{d = 0} ^{k - 1} d \times w_k ^{-jd}d=0∑k−1d×wk−jd形如∑i=0n−1iri\sum\limits_{i = 0} ^{n - 1} i r ^ ii=0∑n−1iri,考虑形如∑i=0n−1(i+1)ri+1\sum\limits_{i = 0} ^{n - 1} (i + 1) r ^ {i + 1}i=0∑n−1(i+1)ri+1这样的式子化简:
∑i=0n−1(i+1)ri+1=r∑i=0n−1iri+∑i=0n−1ri+1=r∑i=0n−1iri+r(rn−1)r−1有∑i=0n−1iri=∑i=0n−1(i+1)ri+1−0r0−nrn所以∑i=0n−1iri=r∑i=0n−1iri+r(rn−1)r−1−nrn整理可得∑i=0n−1iri=nrn−r(rn−1)r−1r−1=nrn(r−1)−r(rn−1)(r−1)2=nrnr−1−r(rn−1)(r−1)2\sum_{i = 0} ^{n - 1} (i + 1) r ^{i + 1} = r\sum_{i = 0} ^{n - 1} i r ^i + \sum_{i = 0} ^{n - 1} r ^{i + 1} = r \sum_{i = 0} ^{n - 1} i r ^ i + \frac{r(r ^ n - 1)}{r - 1}\\ 有\sum_{i = 0} ^{n - 1} i r ^ i = \sum_{i = 0} ^{n - 1} (i + 1) r ^{i + 1} - 0 r ^ 0 - n r ^ n\\ 所以\sum_{i = 0} ^{n - 1} i r ^ i = r \sum_{i = 0} ^{n - 1} i r ^ i + \frac{r(r ^ n - 1)}{r - 1} - n r ^ n\\ 整理可得\sum_{i = 0} ^{n - 1} i r ^ i = \frac{n r ^ n - \frac{r(r ^ n - 1)}{r - 1}}{r - 1} = \frac{n r ^ n(r - 1) - r (r ^ n - 1)}{(r - 1) ^ 2} = \frac{nr ^ n}{ r - 1} - \frac{r(r ^ n - 1)}{(r - 1) ^ 2}\\ i=0∑n−1(i+1)ri+1=ri=0∑n−1iri+i=0∑n−1ri+1=ri=0∑n−1iri+r−1r(rn−1)有i=0∑n−1iri=i=0∑n−1(i+1)ri+1−0r0−nrn所以i=0∑n−1iri=ri=0∑n−1iri+r−1r(rn−1)−nrn整理可得i=0∑n−1iri=r−1nrn−r−1r(rn−1)=(r−1)2nrn(r−1)−r(rn−1)=r−1nrn−(r−1)2r(rn−1)
特殊情况r=1r = 1r=1时∑i=0n−1iri=n(n−1)2\sum\limits_{i = 0} ^{n - 1} i r ^ i = \frac{n(n - 1)}{2}i=0∑n−1iri=2n(n−1),对后项再进行化简:
∑d=0k−1d×wk−jd考虑j≠0时,也就是wkj不为1∑d=0k−1d×wk−jd=kwk−jkwk−j−1−wk−j(wk−jk−1)(wk−j−1)2=kwk−j−1后项整体有:1k2∑j=0k−1(p×wkj+1)nkwk−j−1\sum_{d = 0} ^{k - 1} d \times w _k ^{-jd}\\ 考虑j \neq 0时,也就是w_{k} ^{j}不为1\\ \sum_{d = 0} ^{k - 1} d \times w_k ^{-jd} = \frac{k w_k ^ {-jk}}{w_k ^{-j} - 1} - \frac{w_k ^{-j}(w_k ^{-jk} - 1)}{(w_k ^{-j} - 1) ^ 2} = \frac{k}{w_k ^{-j} - 1}\\ 后项整体有:\frac{1}{k ^ 2} \sum_{j = 0} ^{k - 1} (p \times w_k ^ j + 1) ^ n \frac{k}{w_k ^{-j} - 1}\\ d=0∑k−1d×wk−jd考虑j=0时,也就是wkj不为1d=0∑k−1d×wk−jd=wk−j−1kwk−jk−(wk−j−1)2wk−j(wk−jk−1)=wk−j−1k后项整体有:k21j=0∑k−1(p×wkj+1)nwk−j−1k
最后答案为:
npk(p+1)n−1−(k(k−1)2(p+1)nk2+∑j=1k−1(p×wkj+1)nk(wk−j−1))npk(p+1)n−1−((k−1)(p+1)n2k+∑j=1k−1(p×wkj+1)nk(wk−j−1))\frac{np}{k}(p + 1) ^ {n - 1} - \left(\frac{k(k - 1)}{2}\frac{(p + 1) ^ n}{k ^ 2} + \sum\limits_{j = 1} ^{k - 1} \frac{(p \times w_k ^ j + 1) ^ n}{k(w_{k} ^{-j} - 1)}\right)\\ \frac{np}{k}(p + 1) ^ {n - 1} - \left(\frac{(k - 1)(p + 1) ^ n}{2k} + \sum\limits_{j = 1} ^{k - 1} \frac{(p \times w_k ^ j + 1) ^ n}{k(w_{k} ^{-j} - 1)}\right)\\ knp(p+1)n−1−(2k(k−1)k2(p+1)n+j=1∑k−1k(wk−j−1)(p×wkj+1)n)knp(p+1)n−1−(2k(k−1)(p+1)n+j=1∑k−1k(wk−j−1)(p×wkj+1)n)
#include <bits/stdc++.h>using namespace std;const int mod = 998244353, N = 2e6 + 10;int w[N], n, p, k;int quick_pow(int a, int n) {int ans = 1;while (n) {if (n & 1) {ans = 1ll * ans * a % mod;}a = 1ll * a * a % mod;n >>= 1;}return ans;
}int inv(int x) {return quick_pow(x, mod - 2);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);scanf("%d %d %d", &n, &p, &k);int wn = quick_pow(3, (mod - 1) / k);int ans = 1ll * n * p % mod * quick_pow(p + 1, n - 1) % mod * inv(k) % mod;int res = 1ll * (k - 1) * quick_pow(p + 1, n) % mod * inv(2 * k) % mod;w[0] = 1;for (int i = 1; i < k; i++) {w[i] = 1ll * w[i - 1] * wn % mod;}for (int i = 1; i < k; i++) {res = (res + 1ll * quick_pow(1ll * p * w[i] % mod + 1, n) * inv(1ll * k * (w[k - i] - 1) % mod) % mod) % mod;}ans = (ans - res + mod) % mod;printf("%d\n", ans);return 0;
}