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;
}