数学题
\(\mathcal{Description}\)
给\(n,b,k\),求
\(\begin{aligned}\sum_{i=1}^n\begin{pmatrix}i\\k \end{pmatrix}b^i\end{aligned}\)
答案对\(998244353\)取模
\(1\leq k\leq 500000,1\leq n\leq 10^{18},2\leq b\leq 998244353\)
\(\mathcal{Solution}\)
设
\(\begin{aligned}f_{k}=\sum ^{n}_{i=1}\begin{pmatrix} i \\ k \end{pmatrix}b^{i}\end{aligned}\)
则有
\(\begin{aligned}f_{k-1}=\sum ^{n}_{i=1}\begin{pmatrix} i \\ k-1 \end{pmatrix}b^{i}\end{aligned}\)
\(\begin{aligned}bf_{k}=\sum ^{n+1}_{i=2}\begin{pmatrix} i-1 \\ k \end{pmatrix}b^{i}\end{aligned}\)
\(\begin{aligned}bf_{k-1}=\sum ^{n+1}_{i=2}\begin{pmatrix} i-1 \\ k-1 \end{pmatrix}b^{i}\end{aligned}\)
\(\begin{aligned}bf_{k}+bf_{k-1}=\sum ^{n+1}_{i=2}\left(\begin{pmatrix} i-1 \\ k \end{pmatrix}+\begin{pmatrix} i-1 \\ k-1 \end{pmatrix}\right)b^{i}=\sum ^{n+1}_{i=2}\begin{pmatrix} i \\ k \end{pmatrix}b^{i}=f_k+\begin{pmatrix}n+1\\ k\end{pmatrix}b^{n+1}-\begin{pmatrix}1\\k\end{pmatrix}b\end{aligned}\)
移项得
\(\left(b-1\right)f_k=\begin{pmatrix}n+1\\k\end{pmatrix}b^{n+1}-\begin{pmatrix}1\\k\end{pmatrix}b-bf_{k-1}\)
\(f_k=\dfrac{\begin{pmatrix}n+1\\k\end{pmatrix}b^{n+1}-\begin{pmatrix}1\\k\end{pmatrix}b-bf_{k-1}}{b-1}\)
当\(k=0\)时,要求的就是一个等比数列,我们用等比数列求和公式直接算就可以了
\(f_0=\dfrac{b^{n+1}-b}{b-1}\)
总复杂度\(O(k)\)
\(\mathcal{Code}\)
/*******************************
Author:Morning_Glory
LANG:C++
Created Time:2019年09月17日 星期二 10时54分50秒
*******************************/
#include <cstdio>
#include <fstream>
#define ll long long
using namespace std;
const int mod = 998244353;
const int maxn = 500005;
ll n,b,k,ans;
ll invb,mi,C;
ll inv[maxn];
//{{{ksm
ll ksm (ll a,ll b)
{
a%=mod;
ll s=1;
for (;b;b>>=1,a=a*a%mod)
if (b&1) s=s*a%mod;
return s;
}
//}}}
int main()
{
scanf("%lld%lld%lld",&n,&b,&k);
C=(n+1)%mod,inv[1]=1,invb=ksm(b-1,mod-2);
for (int i=2;i<=k;++i) inv[i]=(-mod/i*inv[mod%i]%mod+mod)%mod;
ans=(ksm(b,n+1)-b+mod)%mod*invb%mod;
mi=ksm(b,n+1);
if (n<k){ printf("0\n");return 0;}
if (n==k){ printf("%lld\n",ksm(b,n));return 0;}
for (int i=1;i<=k;++i){
ll t=0;
if (i<=1) t=b;
ans=((C*mi%mod-t-b*ans%mod)%mod+mod)%mod*invb%mod;
C=(n-i+1)%mod*C%mod*inv[i+1]%mod;
}
printf("%lld\n",ans);
return 0;
}
如有哪里讲得不是很明白或是有错误,欢迎指正
如您喜欢的话不妨点个赞收藏一下吧