51nod-1228: 序列求和
【传送门:51nod-1228】
简要题意:
求出$\sum_{i=1}^{n}i^{k}\mod p$
题解:
要用伯努利数来做(学了一上午。。)
伯努利数,$B_{0}=1$
因为$\sum_{j=0}^iC_{i+1}^jB_j=0$
所以$B_i=-\frac{1}{i+1}\sum_{j=0}^{i-1}C_{i+1}^{j}B_j$
预处理B数组
对于每个n,k,答案为$\sum_{i=1}^{n}i^k={1\over{k+1}}\sum_{i=1}^{k+1}C_{k+1}^i*B_{k+1-i}*(n+1)^i$
参考代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; LL Mod=1e9+7; LL ny[2100],jc[2100],B[2100]; LL C(int n,int m) { return jc[n]*ny[m]%Mod*ny[n-m]%Mod; } LL p_mod(LL a,int b) { LL ans=1; while(b!=0) { if(b%2==1) ans=ans*a%Mod; a=a*a%Mod;b/=2; } return ans; } int main() { ny[0]=1;jc[0]=1; for(int i=1;i<=2000;i++) jc[i]=jc[i-1]*i%Mod,ny[i]=p_mod(jc[i],Mod-2); B[0]=1; for(int i=1;i<=2000;i++) { B[i]=0; for(int j=0;j<i;j++) B[i]=(B[i]+B[j]*C(i+1,j)%Mod)%Mod; B[i]=(-B[i]*p_mod(i+1,Mod-2))%Mod; B[i]=(B[i]+Mod)%Mod; } int T; scanf("%d",&T); while(T--) { LL n;int k; scanf("%lld%d",&n,&k); n%=Mod; LL ans=0,mul=(n+1)%Mod; for(int i=1;i<=k+1;i++) { ans=(ans+C(k+1,i)*B[k+1-i]%Mod*mul%Mod)%Mod; mul=mul*(n+1)%Mod; } ans=ans*p_mod(k+1,Mod-2)%Mod; printf("%lld\n",ans); } return 0; }
渺渺时空,茫茫人海,与君相遇,幸甚幸甚