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
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;
}
Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法的更多相关文章
- Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值
The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...
- [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~ ...
- 【Educational Codeforces Round 37 F】SUM and REPLACE
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
- Educational Codeforces Round 14 - F (codeforces 691F)
题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...
- Educational Codeforces Round 1 A. Tricky Sum 暴力
A. Tricky Sum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem ...
- 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 ...
随机推荐
- perl6中函数参数(1)
sub F($number is copy){ $number++; say $number; } F(); #下面是错误的 sub F($number){ $number++; say $numbe ...
- openboot的项目
http://docs.oracle.com/cd/E19201-01/821-0901-10/OK_OBP.html https://www.openfirmware.info/OpenBIOS h ...
- vs 预编译命令行
xcopy "$(SolutionDir)\Transight_FY_DataExchange_UI\CuscapiUpdaterServer.xml" /i /d /y
- BZOJ 3656: 异或 (组合数取模 CRT)
http://www.lydsy.com/JudgeOnline/problem.php?id=3656 大意:经过一通推导,问题变成求\[\binom N M \mod P\],其中N,M<= ...
- vim的各种tips
centos系统,修改vim的配置文件 /etc/vimrc 添加如下内容: 1) 打开 vimrc ,添加以下语句来使得语法高亮显示: syntax on 2) 如果此时语法还是没有高亮显示,那么在 ...
- Leetcode 之Simplify Path(36)
主要看//之间的内容:如果是仍是/,或者是.,则忽略:如果是..,则弹出:否则压入堆栈.最后根据堆栈的内容进行输出. string simplifyPath(string const& pat ...
- php设计模式五----适配器模式
1.简介 适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 意图:将一个类的接口转换成客户希望的另外一个接口 ...
- Container With Most Water——双指针
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Java学习笔记(十四)——Java静态工厂
[前面的话] 每天过的还行,对我来说,只要让自己充实,生活就会是好的. 学习Java工场方法的原因是最近在使用Spring框架做一个系统,其中有一个注入的方法是使用静态工场方法注入,所以学习一下,基础 ...
- LoadRunner脚本回放日志中的Warning信息
关注LoadRunner脚本回放日志中的Warning信息 最近在与大家的讨论中发现了LoadRunner的很多问题,出于解决问题的出发点,我也就相关自己不理解的问题在Google中搜索了一番,并 ...