Codeforces 622 F. The Sum of the k-th Powers
## [$>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers
题目大意 : 给出 \(n, k\),求 \(\sum_{i=1}^{n} i^k\) 对 \(10^9 +7\) 取模的值
\(1 \leq n \leq 10^9, 0 \leq k \leq 10^6\)
解题思路 :
考虑 \(k\) 比较大,且模数不太好 \(NTT\),于是考虑插值做法, 学习了一波插值之后,发现就是个板子题 (雾)
拉格朗日插值:
对于 \(n\) 个点值表达,可以确定一个 \(n - 1\) 次的多项式,考虑已知点值怎么构造这个多项式
如果对于第 \(i\) 个点值,能够构造出这么一个多项式 \(f_i(x)\) ,将每一个点值表达 \(j\) 带进去的结果如下
\[f_i (x) = \begin{cases} y_i , & \text{if $ i= j$} \\ 0. \, & \text{otherwise} \end{cases}
\]那么最终的多项式 \(F(x) = \sum_{i=1}^{n} f_i(x)\)
通过贺资料发现当 $f_i(x) = y_i \times \prod_{j=1, \ j \neq i}^{n} \frac{x-x_j}{x_i-x_j} $ 时即可满足条件,不妨证明一下为什么
考虑分子部分,如果带进去的 \(x \neq x_i\) 那么当 \(j = i\) 的时候分子是 \(0\),结果也是 \(0\)
考虑分母部分,如果 \(x = x_i\) 那么分子就等于分母,且分子分母都不存在 \(0\) ,所以结果就是 \(y_i\)
大力观察题面发现,\(k\) 次幂的前缀和一定能用一个 \(k + 1\) 次的多项式来表示,不妨递推出 \(x = 1\ .. \ (k+2)\) 时的点值表示直接做插值
那么第 \(n\) 项的答案就等于 $F(n) = \sum y_i \times \prod_{j=1, \ j \neq i}^{k+2} \frac{n-x_j}{x_i-x_j} $
因为 \(x_i = i\) ,所以 \(F(n) = \sum y_i \times \prod_{j=1, \ j \neq i}^{k+2} \frac{n-j}{i-j}\), 后面部分也就是 \(\frac{(n-1)\times(n-2)\times..\times(n-k-2)}{(n-i)\times(i-1)!\times(n-i)!\times (-1)^{n-i}}\)
那么阶乘与处理一下枚举一下 \(i\) 即可,注意特判一下 \(i = n\) 的情况不能直接除,应该记一个没有 \((n-i)\) 这一项的答案
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
int js[2000005], n, k;
const int Mod = 1000000007;
inline int Pow(int a, int b){
int ans = 1;
for(; b; b >>= 1, a = a * a % Mod)
if(b & 1) ans = ans * a % Mod;
return ans;
}
inline int Inv(int x){ return Pow((x + Mod) % Mod, Mod - 2); }
main(){
int res = 1, res2 = 1; read(n), read(k), js[0] = 1;
for(int i = 1; i <= k + 2; i++){
js[i] = (js[i-1] * i) % Mod;
res = (res * (n - i) + Mod) % Mod;
if(i != n) res2 = (res2 * (n - i) % Mod + Mod) % Mod;
}
int now = 0, ans = 0;
for(int i = 1; i <= k + 2; i++){
now = (now + Pow(i, k)) % Mod;
int A = (i != n) ? res * Inv(n - i) % Mod : res2;
int B = js[i-1] * js[k+2-i] % Mod;
if((k + 2 - i) & 1) B = (-B + Mod) % Mod;
ans = (ans + now * (A * Inv(B) % Mod) % Mod) % Mod;
}
cout << (ans % Mod + Mod) % Mod;
return 0;
}
Codeforces 622 F. The Sum of the k-th Powers的更多相关文章
- CF 622 F The Sum of the k-th Powers —— 拉格朗日插值
题目:http://codeforces.com/contest/622/problem/F 设 f(x) = 1^k + 2^k + ... + n^k 则 f(x) - f(x-1) = x^k ...
- 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~ ...
- Codeforces 1043 F - Make It One
F - Make It One 思路: dp + 容斥 首先, 答案不会超过7, 因为前7个质数的乘积大于3e5(最坏的情况是7个数, 每个数都缺少一个不同的因子) 所以从1到7依次考虑 dp[i][ ...
- Codeforces 963 A. Alternating Sum(快速幂,逆元)
Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- LeetCode862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
随机推荐
- 【51nod】1238 最小公倍数之和 V3 杜教筛
[题意]给定n,求Σi=1~nΣj=1~n lcm(i,j),n<=10^10. [算法]杜教筛 [题解]就因为写了这个非常规写法,我折腾了3天…… $$ans=\sum_{i=1}^{n}\s ...
- js父页面和子页面相互取值
iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同. 一.同域下父子页面的通信 父页面parent.html <html> <head& ...
- http://www.cnblogs.com/kkdn/
/*** PHP保留两位小数的几种方法* @link http://www.phpddt.com*/$num = 10.4567; //第一种:利用round()对浮点数进行四舍五入echo roun ...
- Mac 10.9x下安装配置phonegap3.0开发环境 (涉及android sdk配置)
最近突然想弄一下phonegap,之前一直是听说,没亲自配置开发过.结果配置过程非常艰难啊.特别是android平台的配置,那叫一个麻烦,网上搜了半天都没找到非常好的资料.文章也都是抄来抄去,最烦的就 ...
- jquery 生成二维码
jquery的二维码生成插件qrcode,在页面中调用该插件就能生成对应的二维码 <!DOCTYPE html> <html> <head> <meta ch ...
- android内存回收顺序
最近做项目的时候,经常会考虑到系统回收进程,释放资源等问题.特别查找了相关资料,了解下android内存回收顺序以及回收场景. 下面内容都为网络查找资料,若有错误,欢迎指出. 以下顺序,依次被回收的可 ...
- Web安全的三个攻防姿势
原文地址:https://segmentfault.com/a/1190000011601837 作者: zwwill_木羽 关于Web安全的问题,是一个老生常谈的问题,作为离用户最近的一层,我们大前 ...
- Linux内核同步原语之原子操作【转】
转自:http://blog.csdn.net/npy_lp/article/details/7262388 避免对同一数据的并发访问(通常由中断.对称多处理器.内核抢占等引起)称为同步. ——题记 ...
- 做php网站后台开发,在Linux系统上进行更好吗?【转载】
1. PHP是开源软件,它在bsd/linux/win下都有很好的正式版及孪生版.并非开发php就必须要在linux下进行.主机服务商们习惯性的把asp与php分为两个主机系列几进行销售.由于asp只 ...
- #题目:有10 台被监控主机、一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次
#题目:有10 台被监控主机.一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次 #测试机器:虚拟机Linux ...