洛谷P5591 小猪佩奇学数学
\[\large\begin{aligned}
&\sum_{i=0}^n\binom{n}{i}p^i\left \lfloor \frac{i}{k} \right \rfloor \\
=&\sum_{i=0}^n\binom{n}{i}p^i \frac{i-i \bmod k}{k} \\
=&\frac{1}{k}\left ( \sum_{i=0}^n\binom{n}{i}p^i i-\sum_{i=0}^n\binom{n}{i}p^i (i \bmod k)\right ) \\
\end{aligned}
\]
先化简前一项:
\[\large\begin{aligned}
&\sum_{i=0}^n\binom{n}{i}p^i i \\
=&n\sum_{i=0}^n\binom{n-1}{i-1}p^i \\
=&n\sum_{i=0}^{n-1}\binom{n-1}{i}p^{i+1} \\
=&np(p+1)^{n-1}
\end{aligned}
\]
再化简后一项:
\[\large\begin{aligned}
&\sum_{i=0}^n\binom{n}{i}p^i (i \bmod k) \\
=&\sum_{d=0}^{k-1}d\sum_{i=0}^n\binom{n}{i}p^i [k \mid i-d] \\
=&\sum_{d=0}^{k-1}d\sum_{i=0}^n\binom{n}{i}p^i \frac{1}{k}\sum_{j=0}^{k-1} \omega_k^{ij-dj}\\
=&\frac{1}{k}\sum_{j=0}^{k-1}\sum_{i=0}^n\binom{n}{i}(p \omega_k^j)^i\sum_{d=0}^{k-1}d\omega_k^{-dj}\\
=&\frac{1}{k}\sum_{j=0}^{k-1}(p \omega_k^j+1)^n\sum_{d=0}^{k-1}d\omega_k^{-dj}\\
\end{aligned}
\]
发现后一项式子形如 \(\sum\limits_{i=0}^{n-1}ix^i\),应用扰动法得:
\[\large\begin{aligned}
S=&\sum_{i=0}^{n-1}ix^i \\
=&\sum_{i=0}^{n-1}(i+1)x^{i+1}-nx^n \\
=&x\sum_{i=0}^{n-1}ix^i+\sum_{i=0}^{n-1}x^{i+1}-nx^n \\
S=&xS+x\frac{1-x^n}{1-x}-nx^n \\
=&\frac{x\frac{1-x^n}{1-x}-nx^n}{1-x}
\end{aligned}
\]
代入得:
\[\large\begin{aligned}
&\frac{1}{k}\sum_{j=0}^{k-1}(p \omega_k^j+1)^n\sum_{d=0}^{k-1}d\omega_k^{-dj}\\
=&\frac{1}{k}\left(\sum_{j=1}^{k-1}(p \omega_k^j+1)^n\frac{k}{\omega_k^{-j}-1}+(p+1)^n\frac{k(k-1)}{2}\right)\\
\end{aligned}
\]
然后就能快速计算了。
#include<bits/stdc++.h>
#define maxn 4000010
#define p 998244353
#define g 3
using namespace std;
typedef long long ll;
template<typename T> inline void read(T &x)
{
x=0;char c=getchar();bool flag=false;
while(!isdigit(c)){if(c=='-')flag=true;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
if(flag)x=-x;
}
ll n,x,k,w,wn,val;
ll qp(ll x,ll y)
{
ll v=1;
while(y)
{
if(y&1) v=v*x%p;
x=x*x%p,y>>=1;
}
return v;
}
int main()
{
read(n),read(x),read(k),w=1,wn=qp(g,(p-1)/k);
for(int i=0;i<k;++i)
{
ll v=qp(x*w%p+1,n);
if(i) val=(val+v*k%p*qp((qp(w,p-2)-1+p)%p,p-2)%p)%p;
else val=(val+v*(((k-1)*k/2)%p)%p)%p;
w=w*wn%p;
}
printf("%lld",(n*x%p*qp(x+1,n-1)%p-val*qp(k,p-2)%p+p)%p*qp(k,p-2)%p);
return 0;
}