CF622F The Sum of the k-th Powers (natural number and Power)

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     ///Pretreatment factorial 
26 is      int now = 0 ;
 27      int FZ = . 1 ;
 28      for ( int I = . 1 ; I <= K + 2 ; I ++ )
 29          FZ * = (Ni), FZ = MOD%; /// Processing molecules 
30  
31 is      for ( int I = . 1 ; I <= K + 2 ; I ++ )
 32      {
 33 is          now KSM + = (I, K);     /// each of Yi 
34 is          now% = MOD;
 35          int INV1 KSM = (Ni, mod- 2); /// small molecule inverse element 
36          int INV2 = KSM (FAC [I- . 1 ] * FAC [K + 2 -i], mod- 2 );
 37 [          /// denominator inverse of the denominator can be seen is the product of two factorial 
38 is          
39          int F = . 1 ;
 40          IF ((K + 2 -i)% 2 == . 1 ) F = - . 1 ;
 41 is          ANS + FZ = F * * * INV1 INV2% MOD% MOD% * now MOD;
 42 is          ANS% = MOD;
 43 is  
44 is       //    COUT I << << "" << endl << ANS; 
45      }
 46 is      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).

Output

Print 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





When the board took





























Guess you like

Origin www.cnblogs.com/zhangbuang/p/11073311.html