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. 十、Socket之UDP编程

    UDP基础知识 UDP(User Datagram Protocol,用户数据报协议)是一个简单的.面向数据报的无连接协议,提供了快速但不一定可靠的传输服务. UDP与TCP相比主要有以下区别. 1. ...

  2. Gaussian Discriminant Analysis

    如果在我们的分类问题中,输入特征$x$是连续型随机变量,高斯判别模型(Gaussian Discriminant Analysis,GDA)就可以派上用场了. 以二分类问题为例进行说明,模型建立如下: ...

  3. Mysql命令行连接

    mysql在线参考手册地址: http://dev.mysql.com/doc/refman/5.1/zh/tutorial.html#connecting-disconnecting 在linux平 ...

  4. PHP中$_POST,$_GET,$_REQUEST,$_FILES全局变量的全局指什么

    我一直担心,同一个表单,同时提交2次会发生什么事?在服务器端表单变量会不会彼此覆盖呢?也就是说假如我们在PHP中用$_REQUEST["name"]访问某个表单变量,会不会因为别人 ...

  5. SOAP 及其安全控制--转载

    原文地址:http://my.oschina.net/huangyong/blog/287791 目录[-] 1. 基于用户令牌的身份认证 2. 基于数字签名的身份认证 3. SOAP 消息的加密与解 ...

  6. Using zend-navigation in your Album Module

    Using zend-navigation in your Album Module In this tutorial we will use the zend-navigation componen ...

  7. Test complete测试工具介绍

    Test complete 是一款性价比比较高的测试工具,能够满足大多数用户的自动化测试的需求. Test complete 是近几年流行和发展起来的一款自动化测试工具,早期版本由Automated ...

  8. 0x80029C4A

    法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Exc ...

  9. ios快捷键

    分屏:cmd + option + return 退出分屏:cmd + return cmd + option + [ 代码上跳 cmd + [ 代码左移

  10. 关于XML的Schema文件讲解

    1 Schema概述 1.1 什么是Schema l  Schema是新的XML文档约束:DTD出现的比较早. l  Schema要比DTD强大很多: l  Schema本身也是XML文档,但Sche ...