求自然数幂和 B - The Sum of the k-th Powers CodeForces - 622F
题解:
很多方法
斯特林数推导略麻烦但是不依赖于模数
代码:
拉格朗日插值
由于可以证明这是个K+1次多项式于是可以直接用插值
#include <bits/stdc++.h> using namespace std; const int mo=1e9+7; #define IL inline #define ll long long #define rint register int #define rep(i,h,t) for (rint i=h;i<=t;i++) #define dep(i,t,h) for (rint i=t;i>=h;i--) const int N=2e6; ll f[N],jc[N]; ll fst(ll x,ll y) { if (y==0) return(1); if (y==1) return(x); ll kk=fst(x,y/2); kk=(kk*kk)%mo; if (y%2) kk=(kk*x)%mo; return kk; } int main() { freopen("1.in","r",stdin); freopen("1.out","w",stdout); ios::sync_with_stdio(false); ll n,k; cin>>n>>k; rep(i,1,k+2) f[i]=(f[i-1]+fst(i*1ll,k))%mo; if (n<=k+2) { cout<<f[n]<<endl; return 0; } jc[0]=1; rep(i,1,k+2) jc[i]=(jc[i-1]*i)%mo; ll now=1,ans=0; rep(i,1,k+2) now=(now*(n-i))%mo; rep(i,1,k+2) { ll inv1=fst(n-i,mo-2); ll inv2=fst((jc[i-1]*jc[k+2-i])%mo,mo-2)%mo; ll sign=(k+2-i)%2?-1:1; ans=(ans+sign*inv1*inv2%mo*f[i]%mo*now%mo)%mo; } cout<<(ans+mo)%mo; return 0; }