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. js原型 作用域

    了解JavaScript原型链之前首先肯定要知道什么是原型. JavaScript中,原型是一个对象,通过原型可以实现属性的继承.既然原型是一个对象,那么任何一个对象都可以称为原型吗?是,记住它.什么 ...

  2. sql中null 和 ‘’(空字符串)

    sql 中 null  和 空字符串的区别方式 在Silverlight中  数据库 需要与实体类进行映射, 假如实体类不允许为null,则 select '' as 列名  from  表名字:   ...

  3. jQuery垂直缩略图相册插件 支持鼠标滑动翻页

    在线演示 本地下载

  4. jQuery多级联动美化版Select下拉框

    在线演示 本地下载

  5. 多线程、多进程、协程、IO多路复用请求百度

    最近学习了多线程.多进程.协程以及IO多路复用,那么对于爬取数据来说,这几个方式哪个最快呢,今天就来稍微测试一下 普通方式请求百度5次 import socket import time import ...

  6. PAT1076. Forwards on Weibo (30)

    使用DFS出现超时,改成bfs DFS #include <iostream> #include <vector> #include <set> using nam ...

  7. Windows下搭建Apache+Django+Python Web服务环境

    最近在学Django,想用Apache搭建一个服务器环境,因此在网上看了好多资料,很多都是用Python2.6和Apache2.2搭建的环境,不过我还是想用Python35和Apache24来搭建,具 ...

  8. java-登陆界面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. WPF的外观装饰类——Border

    public class Border : System.Windows.Controls.Decorator 说明:在另一个元素的周围绘制边框.背景或同时绘制二者.

  10. [转载]Java给word中的table赋值

    一.准备工作: 下载PageOffice for  Java:http://www.zhuozhengsoft.com/dowm/ 二. 实现方法: 要调用PageOffice操作Word中的tabl ...