F. The Sum of the k-th Powers

题目连接:

http://www.codeforces.com/contest/622/problem/F

Description

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.

Sample Input

4 1

Sample Output

10

Hint

题意

让你计算1k+2k+....+n^k

题解:

拉格朗日插值法

答案等于$${P}{x} = \sum{i}^{k+2}({P}{i}\prod{j=1,j\neq i}^{k+2}\frac{n-j}{i-j})$$

最后的答案就等于P(n)

我们预处理(n-j)的阶乘,再预处理下面的阶乘就好了

对于这样,对于每一个i,我们都能够O(logn)来计算了(logn拿来求逆元)

代码

#include<bits/stdc++.h>
using namespace std; const int mod = 1e9+7;
const int maxn = 1e6+7;
long long quickpow(long long m,long long n,long long k)//返回m^n%k
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
long long p[maxn];
long long fac[maxn];
int n,k;
int main()
{
fac[0]=1;
for(int i=1;i<maxn;i++)
fac[i]=(fac[i-1]*i)%mod;
scanf("%d%d",&n,&k);
p[0]=0;
for(int i=1;i<=k+2;i++)
p[i]=(p[i-1]+quickpow(i,k,mod))%mod;
if(n<=k+2)
{
printf("%d\n",p[n]);
return 0;
}
long long cur = 1;
for(int i=1;i<=k+2;i++)
cur=(cur*(n-i))%mod;
long long ans = 0;
for(int i=1;i<=k+2;i++)
{
long long tmp = quickpow(fac[k+2-i]%mod*fac[i-1]%mod,mod-2,mod);
long long tmp2 = quickpow(n-i,mod-2,mod);
if((k+2-i)%2)tmp=-tmp;
ans =(ans + p[i]*cur%mod*tmp%mod*tmp2)%mod;
}
cout<<ans<<endl;
}

最新文章

  1. c++对象池使用
  2. 【转】Linux查看机器负载
  3. canvas 画字
  4. CentOS 7 用户怎样安装 LNMP(Nginx+PHP+MySQL)
  5. 在MVC中使用Json.Net序列化和反序列化Json对象
  6. PHP 依赖注入,从此不再考虑加载顺序
  7. MySQL数据库InnoDB存储引擎多版本控制(MVCC)实现原理分析
  8. C#学习笔记(十一):动态类型
  9. 二维卷积c代码
  10. ORACLE 数据库简单测试
  11. jBPM4.4与SSH2整合
  12. N种方法妙讲LIS算法
  13. wcf中的使用全双工通信
  14. CSS3的动画属性
  15. Canvas引入跨域的图片导致toDataURL()报错的问题的解决
  16. [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]
  17. Mac中selenium使用出现错误
  18. vue render function &amp; dataset
  19. Android批量图片加载经典系列——使用LruCache、AsyncTask缓存并异步加载图片
  20. C# 解构

热门文章

  1. ImportError: libQtTest.so.4: cannot open shared
  2. Load balancer does not have available server for client:xxx
  3. LightOJ 1282
  4. LCA离线算法Tarjan的模板
  5. ibatis中的符号#跟$区别
  6. js表单提交回调函数
  7. Linux上安装MongoDB
  8. OpenStack 安装数据库和rabbitmq消息队列 (三)
  9. 【剑指offer】面试题 29. 顺时针打印矩阵
  10. PHP 边执行边输出