【BZOJ】【3157】&【BZOJ】【3516】国王奇遇记
数论
题解:http://www.cnblogs.com/zhuohan123/p/3726933.html
copy一下推导过程:
令$$S_i=\sum_{k=1}^{n}k^im^k$$
我们有$$ \begin{aligned} (m-1)S_i &= mS_i-S_i \\&=\sum_{k=1}^n k^im^{k+1}-\sum_{k=1}^n k^i m^k \\&=\sum_{k=2}^{n+1}(k-1)^i m^k-\sum_{k=1}^n k^i m^k \\&=n^i m^{n+1}+\sum_{k=1}^n m^k ( (k-1)^i-k^i ) \\&=n^i m^{n+1}+\sum_{k=1}^n \big( \sum_{j=1}^{i-1}(-1)^{i-j} \binom{i}{j}k^jm^k\big) \\&=n^i m^{n+1}+\sum_{j=0}^{i-1}(-1)^{i-j}\binom{i}{j} S_j \end{aligned} $$
1 /************************************************************** 2 Problem: 3516 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:372 ms 7 Memory:1300 kb 8 ****************************************************************/ 9 10 //BZOJ 3157 11 #include<cstdio> 12 #include<cstring> 13 #include<cstdlib> 14 #include<iostream> 15 #include<algorithm> 16 #define rep(i,n) for(int i=0;i<n;++i) 17 #define F(i,j,n) for(int i=j;i<=n;++i) 18 #define D(i,j,n) for(int i=j;i>=n;--i) 19 #define pb push_back 20 using namespace std; 21 typedef long long LL; 22 const int N=1010; 23 const LL P=1000000007; 24 /*******************template********************/ 25 #define sqr(x) (x)*(x) 26 LL n,m; 27 LL fac[N],inv[N],s[N]; 28 LL C(int a,int b){return fac[a]*inv[b]%P*inv[a-b]%P;} 29 LL Pow(LL a,LL b){ 30 LL r=1; 31 for(;b;b>>=1,a=a*a%P) if (b&1) r=r*a%P; 32 return r; 33 } 34 int main(){ 35 #ifndef ONLINE_JUDGE 36 freopen("3157.in","r",stdin); 37 freopen("3157.out","w",stdout); 38 #endif 39 scanf("%lld%lld",&n,&m); 40 if (m==1){ printf("%lld\n",(n+1)*n/2%P);return 0;} 41 fac[0]=1; F(i,1,m) fac[i]=fac[i-1]*i%P; 42 inv[m]=Pow(fac[m],P-2); 43 inv[0]=1; 44 D(i,m-1,1) inv[i]=inv[i+1]*(i+1)%P; 45 s[0]=((Pow(m,n+1)-m)%P+P)%P*Pow(m-1,P-2)%P; 46 F(i,1,m){ 47 s[i]=Pow(n,i)*Pow(m,n+1)%P; 48 rep(j,i) s[i]=((s[i]+((i-j)%2==1 ? -1 : 1)*C(i,j)*s[j])%P+P)%P; 49 s[i]=s[i]*Pow(m-1,P-2)%P; 50 } 51 printf("%lld\n",s[m]); 52 return 0; 53 }
3157: 国王奇遇记
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 357 Solved: 196
[Submit][Status][Discuss]
Description
Input
共一行包括两个正整数N和M。
Output
共一行为所求表达式的值对10^9+7取模的值。
Sample Input
5 3
Sample Output
36363
HINT
1<=N<=10^9,1<=M<=200