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<bits/stdc++.h>
#define ll long long
const int maxn=;
const int mod=;
using namespace std;
ll f[maxn],fac[maxn],inv[maxn];
ll P(ll a,ll b)
{
ll ans=;
while(b) {
if(b&) ans=ans*a%mod;
b>>=; a=a*a%mod;
}
if(ans<) ans+=mod;
return ans;
}
void init(int tot)
{
fac[]=;
for(int i=;i<=tot;i++)
fac[i]=fac[i-]*i%mod;
inv[tot]=P(fac[tot],mod-);
inv[]=; //求阶乘逆元
for(int i=tot-;i>=;i--)
inv[i]=inv[i+]*(i+)%mod;
}
ll Lagrange(ll n,ll k)
{
int tot=k+;
init(tot);
ll ans=,now=;
for(int i=;i<=tot;i++) now=now*(n-i)%mod;
for(int i=;i<=tot;i++) {
ll inv1=P(n-i,mod-);
ll inv2=inv[i-]*inv[tot-i]%mod;
if((tot-i)&) inv2=mod-inv2;
ll temp=now*inv1%mod;
temp=temp*f[i]%mod*inv2%mod;
ans+=temp;
if(ans>=mod) ans-=mod;
}
return ans;
}
int main()
{
ll n,k;
cin>>n>>k;
for(int i=;i<=k+;i++) f[i]=(f[i-]+P(i,k))%mod;
if(n<=k+) cout<<f[n]<<endl;
else cout<<Lagrange(n,k+)<<endl;
return ;
}

CodeForces - 622F:The Sum of the k-th Powers (拉格朗日插值法求自然数幂和)的更多相关文章

  1. Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法

    F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...

  2. codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法

    题目链接 求sigma(i : 1 to n)i^k. 为了做这个题这两天真是补了不少数论, 之前连乘法逆元都不知道... 关于拉格朗日插值法, 我是看的这里http://www.guokr.com/ ...

  3. Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )

    题目链接 题意 : 就是让你求个自然数幂和.最高次可达 1e6 .求和上限是 1e9 分析 :  题目给出了最高次 k = 1.2.3 时候的自然数幂和求和公式 可以发现求和公式的最高次都是 k+1 ...

  4. Codeforces 622F The Sum of the k-th Powers

    Discription There are well-known formulas: , , . Also mathematicians found similar formulas for high ...

  5. 求自然数幂和 B - The Sum of the k-th Powers CodeForces - 622F

    题解: 很多方法 斯特林数推导略麻烦但是不依赖于模数 代码: 拉格朗日插值 由于可以证明这是个K+1次多项式于是可以直接用插值 #include <bits/stdc++.h> using ...

  6. Codeforces 622F The Sum of the k-th Powers(数论)

    题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...

  7. 【CF622F】The Sum of the k-th Powers (拉格朗日插值法)

    用的dls的板子,因为看不懂调了好久...果然用别人的板子就是这么蛋疼- -|| num数组0~k+1储存了k+2个值,且这k+2个值是自然数i的k次方而不是次方和,dls的板子自己帮你算和的...搞 ...

  8. Codeforces D. The Sum of the k-th Powers(拉格朗日插值)

    题目描述: The Sum of the k-th Powers time limit per test 2 seconds memory limit per test 256 megabytes i ...

  9. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

随机推荐

  1. elastic job配置

    zookeeper注册中心配置 1 package com.zwh.pay.account.worker; import com.dangdang.ddframe.job.reg.zookeeper. ...

  2. HTML,CSS,font-family:中文字体的英文名称

    宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...

  3. jQuery图片列表拖拽排序布局

    在线演示 本地下载

  4. jQuery/CSS3 3D焦点图动画

    在线演示 本地下载

  5. 20145109《Java程序设计》第一周学习总结

    20145109 <Java程序设计>第一周学习总结 教材学习内容总结 About JVM, JRE, JDK JVM包含于JRE中,用于运行Java程序.JDK用于开发Java程序,包含 ...

  6. [Android]AndFix使用说明

    AndFix使用说明 AndFix,全称是Android hot-fix.是阿里开源的一个热补丁框架,允许APP在不重新发布版本的情况下修复线上的bug.支持Android 2.3 到 6.0,并且支 ...

  7. 互联网开放平台API安全设计

    互联网开放平台设计1.需求:现在A公司与B公司进行合作,B公司需要调用A公司开放的外网接口获取数据,如何保证外网开放接口的安全性.2.常用解决办法:2.1 使用加签名方式,防止篡改数据2.2 使用Ht ...

  8. ubuntu 16.04 vscode + php debug

    1.vscode 安装PHP Debug扩展: 2.php环境配置: 1.安装xdebug扩展: sudo apt-get install php-xdebug 2.找到扩展的路径: chq@chq- ...

  9. 谷歌SEO老域名注册完全攻略

    老域名有优势,不管在百度和谷歌都是一样的. 我们查看搜索结果就能发现,谷歌里面很多排前十的网站都是N年前的,一零年以后的算是比较新的,很多产品网站域名是零几年,甚至很多排名更好的域名是九几年的. 谷歌 ...

  10. tomcat集群基于Nginx——共享同一个应用

    1.首先准备两个tomcat,也可以一个复制两个.和一个Nginx tomcat官方下载连接——安装版&绿色版 Nginx官网下载链接:http://nginx.org/download/ 博 ...