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 ...
随机推荐
- Oracle数据库安全(一)用户管理
一.预定义用户 用户管理是Oracle数据库管理的核心和基础. 在创建Oracle数据库时,系统预定义创建的用户根据作用不同又可以分为以下3类 管理员用户 实例方案用户 内置用户 此外Oracle数据 ...
- COS-7设备管理
操作系统(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行. 操作系 ...
- Spark Configuration配置
Spark可以通过三种方式配置系统: 通过SparkConf对象, 或者Java系统属性配置Spark的应用参数 通过每个节点上的conf/spark-env.sh脚本为每台机器配置环境变量 通过lo ...
- 《Computational Statistics with Matlab》硬译
第1章 从随机变量采样 研究者提出的概率模型对于分析方法来说通常比较复杂,研究者处理复杂概率模型时越来越依赖计算.数值方法,通过使用计算方法,研究者就不用对一些分析技术做一些不现实的假设(如正态性和独 ...
- Java中的比较运算符
比较运算符用于判断两个数据的大小,例如:大于.等于.不等于.比较的结果是一个布尔值( true 或 false ). Java 中常用的比较运算符如下表所示: 注意哦: 1. > . < ...
- Dynamo概述
Dynamo 是Amazon提供的一款高可用的分布式Key-Value存储系统,其满足可伸缩性.可用性.可靠性. CAP原理满足:通过一致性哈希满足P,用复制满足A,用对象版本与向量时钟满足C.用牺牲 ...
- FTP的安装配置使用
///////////////////////////////FTP///////////////////////////////////////////////////写在前面:在linux 环境下 ...
- 在myeclipse中安装svn
首先下载site.zip,然后解压.在myeclipse的安装目录中的dropins文件夹中新建svn文件夹,把site中的features和plugins文件夹复制到svn中即可.然后重启Myecl ...
- void与其他类型的转化
#include<stdio.h> void f(void *a) { printf("%d\n",a); } int main() { int b=10; f(b); ...
- 阿里云上如何利用yum安装jenkins
一. 安装jdk 确保安装jenkins前jdk已经安装,如何安装见<如何在阿里云上部署war包到tomcat服务器> 二. 安装jenkins 使用以下命令安装jenkins: wget ...