The Sum of the k-th Powers

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

拉格朗日插值:本质就是通过给定函数的n个点,求未知自变量的函数值;万能公式
细节:题目中给了k前几项的n的通项公式,其中n的最高次为k + 1次;由拉格朗日插值的构造多项式知,当代入k个点时,得到的是k - 1次多项式,
所以要得到最终的k + 1次多项式就需要先求出在函数中的k+2,这样就可以按照得到的多项式代入n求出最终的结果;
 
pi就是已知点的函数值,原本得到的k + 1次多项式n是x才对,这里直接将n替换成了x,得到的就是最终的结果;因为我们知道最终的多项式的次数(关键)
实现细节: 对内层阶乘先预处理出来,但是里面并不是连续的阶乘,需要用到乘法逆元,即欧拉函数推导式;至于分母的正负,可以求完逆元之后在判断(这并没有证明)
时间复杂度:对于n小于maxn时,其实是可以直接求的,时间复杂度为O(maxn*log(maxn));但是当n接近1e9时,一定要用拉格朗日插值法,时间复杂度为O(klog(k));
#include<bits/stdc++.h>
using namespace std;
typedef __int64 ll;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
ll p[maxn],fac[maxn];
ll pow_mod(ll a,ll n)
{
ll ans = ;
while(n){
if(n & ) ans = ans*a%mod;
a = a*a%mod;
n >>= ;
}
return ans;
}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
p[] = ;
for(int i = ;i <= k + ;i++)
p[i] = (p[i - ] + pow_mod(i,k))%mod;
if(n <= k + )
return printf("%I64d",p[n]),;
fac[] = ;
for(int i = ;i <= k + ;i++)
fac[i] = fac[i - ]*i%mod;
ll t = ;
for(int i = ;i <= k+; i++)
t = (n - i)*t%mod;
ll ans = ;
for(int i = ;i <= k + ;i++){
ll t1 = pow_mod(fac[i-]*fac[k+-i]%mod,mod - );//求解逆元
ll t2 = pow_mod(n-i,mod - )%mod;
if((k+-i)&) t1 = -t1;
ans = (ans + p[i]*t%mod*t2%mod*t1%mod + mod)%mod;
}
cout<<ans;
}
 

Educational Codeforces Round 7 F - 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. [Educational Codeforces Round 7]F. The Sum of the k-th Powers

    FallDream dalao找的插值练习题 题目大意:给定n,k,求Σi^k (i=1~n),对1e9+7取模.(n<=10^9,k<=10^6) 思路:令f(n)=Σi^k (i=1~ ...

  3. 【Educational Codeforces Round 37 F】SUM and REPLACE

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...

  4. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  5. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  6. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

  7. Educational Codeforces Round 14 - F (codeforces 691F)

    题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...

  8. Educational Codeforces Round 1 A. Tricky Sum 暴力

    A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...

  9. Educational Codeforces Round 23 F. MEX Queries 离散化+线段树

    F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. Helpers\CSRF

    Helpers\CSRF CSRF Protection The CSRF helper is used to protect post request from cross site request ...

  2. A+B Problem III

    描述 求A+B是否与C相等. 输入 T组测试数据. 每组数据中有三个实数A,B,C(-10000.0<=A,B<=10000.0,-20000.0<=C<=20000.0) 数 ...

  3. Creader VIII VS. Creader VII+

    Launch x431 diagun is professional universal auto diagnostic tool which designed and developed by LA ...

  4. nginx配置文件特殊字符说明

    开发过程中经常重复配置nginx.conf,对里面的特殊字符始终不太明白具体的意义,今天百度nginx配置看到一篇不错的文章,转载记录下来,以备不时之需. nginx rewrite 正则表达式匹配 ...

  5. /lib /usr/lib /usr/local/lib 的区别

    /lib是内核级的,/usr/lib是系统级的,/usr/local/lib是用户级的. /lib/ — 包含许多被 /bin/ 和 /sbin/ 中的程序使用的库文件.目录 /usr/lib/ 中含 ...

  6. 哇!今天找到一个非常好用的自动补全插件-necomplete.vim

    看别人说的什么xpcomplete,snipte,拿来都不会用,这个necomplete.vim还挺好用的,不用去按C-X,C-O进行补全,把关键字自动的列出来,调用的是用户自定义补全,^u^n^p的 ...

  7. canvas实现“探照灯”共能

    简单的样式: body{ margin: 0; padding: 0;}#canvas{ display: block; position: relative; margin: auto;} 创建绘图 ...

  8. [Laravel] 获取执行的Sql

    获取数据库操作记录 $queries = DB::getQueryLog(); //取最后一条是 $lastSql = end($queries); 不过这样输出的,不是真正的sql,输出的是类似PD ...

  9. JavaScript高级程序设计(第三版)学习笔记13、14章

    第13章,事件 事件冒泡 IE的事件叫做事件冒泡:由具体到不具体 <!DOCTYPE html> <html> <head>      <title>E ...

  10. file_up

    一.接收数据 表单提交的数据会自动封装为数组 用$_GET, $_POST, 或$_REQUEST获得表单提交的数据;   二.文件上传的相关配置 1.表单设置: 要进行文件的上传,需要对form表单 ...