Educational Codeforces Round 7 F - 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).
The only line contains two integers n, k (1 ≤ n ≤ 109, 0 ≤ k ≤ 106).
Print the only integer a — the remainder after dividing the value of the sum by the value 109 + 7.
4 1
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 拉格朗日插值的更多相关文章
- 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 ...
- [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 ...
随机推荐
- Vmware中为Mac Os安装vmtools
成功方法: 1. 在VMWare中点击edit this virtual machine 2. 添加CD/DVD,使用iso,找到那个darwin.iso 3. 在setting里面,点击CD/DVD ...
- java_jdbc_3层 解耦
Dao - 提供接口 DaoImpl - 实现 DaoFactory - 工厂模式获取实现 DaoExcetpion - jdbc异常处理 实现runtime exception类即可 TestDem ...
- How to Tune Java Garbage Collection--reference
reference:http://architects.dzone.com/articles/how-tune-java-garbage The Performance Zone is support ...
- ADO.Net 之 数据库连接池(一)
1. 什么是连接池? 我们都知道,建立一个数据库连接是一件非常耗时(消耗时间)耗力(消耗资源)的事情.之所以会这样,是因为连接到数据库服务器需要经历几个漫长的过程:建立物理通道(例如套接字或命名管道) ...
- DHCP服务自动分配IP地址原理
转载自:http://blog.csdn.net/lycb_gz/article/details/8499559 DHCP在提供服务时,DHCP客户端是以UDP 68号端口进行数据传输的,而DHCP服 ...
- 2013 ACM/ICPC 长沙现场赛 A题 - Alice's Print Service (ZOJ 3726)
Alice's Print Service Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is providing print ser ...
- MySQL flush privileges 명령어
INSERT나 UPDATE, DELETE문을 이용해서 MySQL의 사용자를 추가,삭제하거나, 사용자 권한 등을 변경하였을 때, MySQL에 변경사항을 적용하기 위해서 사용하는 명령 ...
- 使用PSSH批量SSH操作Linux服务器
http://www.opstool.com/article/266 服务器多了,有一个烦恼就是如何批量快速操作一堆服务器.这里我推荐一下经常使用利器pssh.这个工具给我的工作带来了莫大的帮助. 简 ...
- 第四章 jQuery中的事件
1.加载DOM jQuery中,在$(document).ready()方法内注册的事件,只要DOM就绪就会被执行,此时可能元素的关联文件未下载完. jQuery中的 load()方法,会在元素的on ...
- SQL server 的约束条件【转】
SQLServer - 约束 一.约束的分类 在SQLServer中,有3种不同类型的约束. 1.实体约束 实体约束是关于行的,比如某一行出现的值就不允许出现在其他行,例如主键. 2.域约束 域约束是 ...