There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees.
Find the value of the sum modulo 109 + 7 (so you should find the remainder after dividing the answer by the value 109 + 7).
1 #include"bits/stdc++.h"
2 using namespace std;
3
4 #define int long long
5
6 int n,k;
7 const int mod = 1e9+7;
8 const int N = 2e6;
9 int fac[N];
10
11 int ksm(int a,int b)
12 {
13 int ans = 1;
14 a%=mod;
15 for(; b; b>>=1,a*=a,a%=mod)
16 if(b&1)ans*=a,ans%=mod;
17 return ans;
18 }
19 void lg()
20 {
21
22 int ans = 0;
23 fac[1]=1; fac[0]=1;
24 for(int i=2; i<=k+10; i++)fac[i]=(fac[i-1]*i%mod);
25 /// 预处理阶乘
26 int now=0;
27 int fz=1;
28 for(int i=1; i<=k+2; i++)
29 fz*=(n-i),fz%=mod; /// 处理分子
30
31 for(int i=1; i<=k+2; i++)
32 {
33 now += ksm(i,k); /// 每次的yi
34 now%=mod;
35 int inv1=ksm(n-i,mod-2);///少的分子的逆元
36 int inv2=ksm(fac[i-1]*fac[k+2-i],mod-2);
37 ///分母的逆元,分母可以看成是两段阶乘的乘积
38
39 int f=1;
40 if((k+2-i)%2==1)f=-1;
41 ans+=f*fz*inv1%mod*inv2%mod*now%mod;
42 ans%=mod;
43
44 // cout<<i<<" "<<ans<<endl;
45 }
46 cout<<(ans+mod)%mod;
47
48
49 }
50
51 signed main()
52 {
53 //cout<<ksm(2,5);
54 cin>>n>>k;
55 if(k==0)
56 {
57 cout<<n;
58 return 0;
59 }
60 if(n<=k+2)
61 {
62 int now=0;
63 for(int i=1; i<=n; i++)
64 now += ksm(i,k),now%=mod;
65 cout<<now;
66 return 0;
67
68 }
69 lg();
70
71 }
Input
The only line contains two integers n, k (1 ≤ n ≤ 109, 0 ≤ k ≤ 106).
OutputPrint the only integer a — the remainder after dividing the value of the sum by the value 109 + 7.
Examples
Input
4 1
Output
10
Input
4 2
Output
30
Input
4 3
Output
100
Input
4 0
Output
4
当板子用了