CodeForces - 622F: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
4 1
10
4 2
30
4 3
100
4 0
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 (拉格朗日插值法求自然数幂和)的更多相关文章
- 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 ...
- codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法
题目链接 求sigma(i : 1 to n)i^k. 为了做这个题这两天真是补了不少数论, 之前连乘法逆元都不知道... 关于拉格朗日插值法, 我是看的这里http://www.guokr.com/ ...
- Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )
题目链接 题意 : 就是让你求个自然数幂和.最高次可达 1e6 .求和上限是 1e9 分析 : 题目给出了最高次 k = 1.2.3 时候的自然数幂和求和公式 可以发现求和公式的最高次都是 k+1 ...
- Codeforces 622F The Sum of the k-th Powers
Discription There are well-known formulas: , , . Also mathematicians found similar formulas for high ...
- 求自然数幂和 B - The Sum of the k-th Powers CodeForces - 622F
题解: 很多方法 斯特林数推导略麻烦但是不依赖于模数 代码: 拉格朗日插值 由于可以证明这是个K+1次多项式于是可以直接用插值 #include <bits/stdc++.h> using ...
- Codeforces 622F The Sum of the k-th Powers(数论)
题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...
- 【CF622F】The Sum of the k-th Powers (拉格朗日插值法)
用的dls的板子,因为看不懂调了好久...果然用别人的板子就是这么蛋疼- -|| num数组0~k+1储存了k+2个值,且这k+2个值是自然数i的k次方而不是次方和,dls的板子自己帮你算和的...搞 ...
- 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 ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
随机推荐
- JS 原生JS 判断滚动条滑动到底部
window.addEventListener("scroll", function(event) { var scrollTop = document.documentEleme ...
- CROS跨域 解决方案 之 tomcat 做过滤处理解决
摘自:http://www.cnblogs.com/liuwenhao-1/articles/6963540.html 1 .在项目中常常遇到本地访问服务器上的链接数据访问不到,并出现如下问题: 这是 ...
- java springboot整合zookeeper入门教程(增删改查)
java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...
- Kali Linux学习笔记
1. 重装grub 重装windows后MBR被覆盖导致linux无法启动,通过U盘进入live后执行以下命令: mount /dev/sda5 /mnt //此处是将linux系统根目录挂载到mnt ...
- java处理图片base64编码的相互转换
转载自http://www.cnblogs.com/libra0920/p/5754356.html 直接上代码 import sun.misc.BASE64Decoder; import sun.m ...
- hdu 2126 Buy the souvenirs 二维01背包方案总数
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- QSS设置字体不起作用
正确的QSS QLabel{ font-family: "Microsoft YaHei"; font-weight:bold; font-size:14px; color: #3 ...
- 报错HTTP Status 500 - HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; nested exception is org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.itcast.entity.
报错 type Exception report message HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; ...
- ddt读取json文件测试用例的执行顺序
一. 源码的说明 在源码中,ddt的file_data函数下有这样一段话 意思是说,如果json文件的内容是字典,字典的键名将会作为测试用例名的后缀,字典的值将会作为测试数据,如果这样的话,如果键名字 ...
- LightOJ - 1341唯一分解定理
唯一分解定理 先分解面积,然后除2,再减去面积%长度==0的情况,注意毯子不能是正方形 #include<map> #include<set> #include<cmat ...