19暑假拉格朗日插值A

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
设f(x)为1到x的k次方和,(高中数学讲过k次方和可以由k-1次方和推出来)所以f(x)最高次为k+1,所以需要求出来k+2个数字然后拉格朗日插值即可

#include<bits/stdc++.h>
#define ll long long 
#define mod 1000000007
#define maxn 1000005
using namespace std;
ll f[maxn],mul[maxn];
long long power(long long a,long long b)
{
    long long ans=1;
    while(b)
    {
        if(b&1)  
        {
            ans=(ans*a)%mod;  
            b--;
        }
        b/=2;   
        a=a*a%mod;    
    }
    return ans;    
}
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    f[0]=0;
    for(int i=1;i<=k+2;i++)
    f[i]=(f[i-1]+power(i,k))%mod;
    if(n<=k+2)
    {
    	printf("%lld",f[n]);
    	return 0;
	}   
    mul[0]=1;
    for(int i=1;i<=k+2;i++)
    mul[i]=mul[i-1]*i%mod;
    ll t=1;
    for(int i=1;i<=k+2;i++)
    t=(n-i)*t%mod;
    ll ans=0;
    for(int i=1;i<=k+2;i++)
	{
        ll tmp=power(mul[i-1]*mul[k+2-i]%mod*(n-i)%mod,mod-2);
        if((k+2-i)&1) 
		tmp=-tmp;
        ans=(ans+f[i]*t%mod*tmp%mod+mod)%mod;
    }
    printf("%lld\n",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值