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. 谁说程序员不懂爱情【Her】

    2014.8.17日 我和我女朋友恋爱两周年的日子.走到今天很不容易,我很珍惜. 就想趁这机会,尽自己能力做一个特别的礼物. 这款应用两周前就开始计划了.也熬了几个晚上.接触wp开发时间不长,第一款应 ...

  2. javascript 的dateObj.getTime() 在为C#的获取方式

    public string GetTime(DateTime dt) { Int64 retval = 0; DateTime st = new DateTime(1970, 1, 1); TimeS ...

  3. @@fetch_status

    @@fetch_status是MicroSoft SQL SERVER的一个全局变量 其值有以下三种,分别表示三种不同含义:[返回类型integer] 0 FETCH 语句成功 -1 FETCH 语句 ...

  4. Django-JS实现的ajax

    JS实现的ajax ajax的优缺点 AJAX使用Javascript技术向服务器发送异步请求 AJAX无须刷新整个页面 因为服务器响应内容不再是整个页面,而是页面中的局部,所以AJAX性能高 小练习 ...

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

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

  6. Maven:如何在eclipse里新建一个Maven的java项目和web项目

    如何在eclipse里新建一个Maven的java项目和web项目: 一:java项目 New-->Other-->Maven 右击项目-->properties,修改以下文件: ① ...

  7. ThinkPHP3.2添加scws中文分词

    前言 前一段时间,公司网站做站内搜索,只简单针对输入的文字进行搜索,作全匹配检索,搜索出来的内容很少.如何达到模糊搜索,匹配到更多的内容成了需要解决的问题.于是,今天想到可以做分词检索,如何对输入的一 ...

  8. SQLite 插入大量数据慢的解决方法

    sqlite 插入数据很慢的原因:sqlite在没有显式使用事务的时候会为每条insert都使用事务操作,而sqlite数据库是以文件的形式存在磁盘中,就相当于每次访问时都要打开一次文件,如果对数据进 ...

  9. 爬虫第六篇:scrapy框架爬取某书网整站爬虫爬取

    新建项目 # 新建项目$ scrapy startproject jianshu# 进入到文件夹 $ cd jainshu# 新建spider文件 $ scrapy genspider -t craw ...

  10. AtCoder ARC097C Sorted and Sorted:dp

    传送门 题意 有 $ 2n $ 个球排成一行,其中恰好有 $ n $ 个白球和 $ n $ 个黑球.每个球上写着数字,其中白球上的数字的并集为 $ \lbrace 1 \dots n\rbrace $ ...