Discription

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.

Example

Input
4 1
Output
10
Input
4 2
Output
30
Input
4 3
Output
100
Input
4 0
Output
4

拉格朗日差值裸题。。。
为什么我以前都用高斯消元做自然幂数和2333333
拉格朗日差值的构造原理和中国剩余定理类似,这里就不在赘述,网上也有很多讲的。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000010;
const int ha=1000000007;
int jc[maxn+5],TT,l;
int n,k,ans=0,base; inline int add(int x,int y){
x+=y;
return x>=ha?x-ha:x;
} inline int ksm(int x,int y){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} inline void init(){
jc[0]=1;
for(int i=1;i<=maxn;i++) jc[i]=jc[i-1]*(ll)i%ha;
l=k+2,base=1;
} inline void solve(){
if(n<=l){
for(int i=1;i<=n;i++) ans=add(ans,ksm(i,k));
}
else{
for(int i=1;i<=l;i++) base=base*(ll)(n-i)%ha;
for(int i=1,now;i<=l;i++){
TT=add(TT,ksm(i,k));
now=base*(ll)ksm(n-i,ha-2)%ha*(ll)ksm(jc[i-1],ha-2)%ha*(ll)ksm(jc[l-i],ha-2)%ha;
if((l-i)&1) now=now*(ll)(ha-1)%ha;
ans=add(ans,TT*(ll)now%ha);
}
}
} int main(){
scanf("%d%d",&n,&k);
init();
solve();
printf("%d\n",ans);
return 0;
}

  

 

Codeforces 622F The Sum of the k-th Powers的更多相关文章

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

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

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

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

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

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

  4. Codeforces 396B On Sum of Fractions 数论

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

  5. codeforces 963A Alternating Sum

    codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...

  6. [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  7. Codeforces 631E Product Sum 斜率优化

    我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移.对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优. 我们设最优值为val,   val = max(a[ j ] ...

  8. LeetCode862. Shortest Subarray with Sum at Least K

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  9. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

随机推荐

  1. linux学习-用户的特殊 shell 与 PAM 模块

    特殊的 shell, /sbin/nologin 『无法登入』指的是:『这个使用者无法使用 bash 或其他 shell 来登入系统』而已, 并不是说这个账号就无法使用其他的系统资源! 让某个具有 / ...

  2. Prolog&Epilog

    这篇博客会简单介绍一下Prolog&Epilog 然后再简单介绍下我对于程序在计算机中到底如何运行的一些理解(因为自己之前也从来没有接触过这些方面的知识,所以如果有讲的不对的地方希望大家能够帮 ...

  3. Leetcode6--->Zigzag Conversion(将给定字符串按照Z字排列,输出结果)

    题目:给定一个字符串s,一个整数numRows, 将字符串s按照竖Z的方式排列,然后输出结果: 举例:String s = "PAYPALISHIRING"; 排列后为: P A ...

  4. python - 接口自动化测试实战 - case1 - 再次优化版

    本次优化: 1.  各级分Package 2.  封装[ReadExcel]类 3.  封装[ReadConfig]类 4.  封装[GetLog]类 5.  引入ddt数据驱动测试,优化测试用例代码 ...

  5. TensorFlow——交互式使用会话:InteractiveSession类

    目的是在交互式环境下(如jupyter),手动设定当前会话为默认会话,从而省去每次都要显示地说明sess的繁琐,如:Tensor.ecal(session=sess)或sess.Operation.r ...

  6. 如何理解logistic函数?

    作者:煎挠橙链接:https://www.zhihu.com/question/36714044/answer/78680948来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  7. 【Luogu】P2801教主的魔法(分块)

    题目链接 激动qwq.这是我A的第一道分块. 分块之后对块内元素暴力sort.修改的时候对于整块打个标记,查询的时候只需要查C-tag就行了 对于非整块,暴力修改,改完之后sort 对于查询……非整块 ...

  8. 微信小程序页面跳转传参

    1.传递参数方法   使用navigatior组件 <navigator url="/pages/pull/pull?title=lalla&name=cc" hov ...

  9. java面试题之osi七层网络模型,五层网络模型,每层分别有哪些协议(阿里面试题)

    OSI七层网络模型 TCP/IP五层网络模型 对应网络协议 应用层 应用层 HTTP.TFTP.FTP.NFS.WAIS.SMTP 表示层 应用层 Telnet.Rlogin.SNMP.Gopher ...

  10. TransactionProxyFactoryBean 配置问题

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...