【拉格朗日求自然数幂和】cf622F

F. The Sum of the k-th Powers


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).

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



代码:

#include
#include
#include
#include
using namespace std;
typedef long long ll;

const ll mod = 1e9+7;
const int maxn = 1e6+5;
ll f[maxn], fac[maxn];
ll pow(ll a, ll b) {                         //快速幂取模
    ll ret = 1; 
    while(b) {
        if(b&1)
            ret = (ret*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return ret;
}

int main()
{
    ll n, k;
    cin>>n>>k;
    for(int i = 1; i<=k+2; i++) {            //求自然数幂和的前k+2项; 
        f[i] = (f[i-1]+pow(i*1LL, k))%mod;
    }

    if(n<=k+2) {
        cout<



你可能感兴趣的:(【拉格朗日求自然数幂和】cf622F)