D e s c r i p t i o n \mathcal{Description} Description
给 n , b , k n,b,k n,b,k,求
∑ i = 1 n ( i k ) b i \begin{aligned}\sum_{i=1}^n\begin{pmatrix}i\\k \end{pmatrix}b^i\end{aligned} i=1∑n(ik)bi
答案对 998244353 998244353 998244353取模
1 ≤ k ≤ 500000 , 1 ≤ n ≤ 1 0 18 , 2 ≤ b ≤ 998244353 1\leq k\leq 500000,1\leq n\leq 10^{18},2\leq b\leq 998244353 1≤k≤500000,1≤n≤1018,2≤b≤998244353
S o l u t i o n \mathcal{Solution} Solution
设
f k = ∑ i = 1 n ( i k ) b i \begin{aligned}f_{k}=\sum ^{n}_{i=1}\begin{pmatrix} i \\ k \end{pmatrix}b^{i}\end{aligned} fk=i=1∑n(ik)bi
则有
f k − 1 = ∑ i = 1 n ( i k − 1 ) b i \begin{aligned}f_{k-1}=\sum ^{n}_{i=1}\begin{pmatrix} i \\ k-1 \end{pmatrix}b^{i}\end{aligned} fk−1=i=1∑n(ik−1)bi
b f k = ∑ i = 2 n + 1 ( i − 1 k ) b i \begin{aligned}bf_{k}=\sum ^{n+1}_{i=2}\begin{pmatrix} i-1 \\ k \end{pmatrix}b^{i}\end{aligned} bfk=i=2∑n+1(i−1k)bi
b f k − 1 = ∑ i = 2 n + 1 ( i − 1 k − 1 ) b i \begin{aligned}bf_{k-1}=\sum ^{n+1}_{i=2}\begin{pmatrix} i-1 \\ k-1 \end{pmatrix}b^{i}\end{aligned} bfk−1=i=2∑n+1(i−1k−1)bi
b f k + b f k − 1 = ∑ i = 2 n + 1 ( ( i − 1 k ) + ( i − 1 k − 1 ) ) b i = ∑ i = 2 n + 1 ( i k ) b i = f k + ( n + 1 k ) b n + 1 − ( 1 k ) b \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} bfk+bfk−1=i=2∑n+1((i−1k)+(i−1k−1))bi=i=2∑n+1(ik)bi=fk+(n+1k)bn+1−(1k)b
移项得
( b − 1 ) f k = ( n + 1 k ) b n + 1 − ( 1 k ) b − b f k − 1 \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} (b−1)fk=(n+1k)bn+1−(1k)b−bfk−1
f k = ( n + 1 k ) b n + 1 − ( 1 k ) b − b f k − 1 b − 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} fk=b−1(n+1k)bn+1−(1k)b−bfk−1
当 k = 0 k=0 k=0时,要求的就是一个等比数列,我们用等比数列求和公式直接算就可以了
f 0 = b n + 1 − b b − 1 f_0=\dfrac{b^{n+1}-b}{b-1} f0=b−1bn+1−b
总复杂度 O ( k ) O(k) O(k)
C o d e \mathcal{Code} 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;
}
如有哪里讲得不是很明白或是有错误,欢迎指正
如您喜欢的话不妨点个赞收藏一下吧