## [$>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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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~ ...

  4. Codeforces 1043 F - Make It One

    F - Make It One 思路: dp + 容斥 首先, 答案不会超过7, 因为前7个质数的乘积大于3e5(最坏的情况是7个数, 每个数都缺少一个不同的因子) 所以从1到7依次考虑 dp[i][ ...

  5. Codeforces 963 A. Alternating Sum(快速幂,逆元)

    Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...

  6. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  7. [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 ...

  8. 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 ...

  9. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

随机推荐

  1. How to reset XiaoMi bluetooth headphone Youth edition.

    To reset the speaker 1. Long press the phone call button to shut off the speaker 2. Connect the char ...

  2. Android中注册获取验证码倒计时按钮

    public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...

  3. 014 JVM面试题

    转自:http://www.importnew.com/31126.html 本文从 JVM 结构入手,介绍了 Java 内存管理.对象创建.常量池等基础知识,对面试中 JVM 相关的基础题目进行了讲 ...

  4. angular项目中使用ngSemantic

    npm install ng-semantic --save npm install jquery --save 下载 Official Semantic UI bundle ( .zip ) fro ...

  5. serialVersionUID的作用(转)

    本文系转载,原文链接:http://swiftlet.net/archives/1268 serialVersionUID适用于Java的序列化机制.简单来说,Java的序列化机制是通过判断类的ser ...

  6. Webcollector应用(二)

    先吐槽一句哀家的人品,总在写好代码之后,网站默默的升级,没有一点点防备... 一.加代理 爬取一个网站的时候,爬了不到一半,IP被封了,整个内部局域网的所有电脑都不能访问网站了. public cla ...

  7. PCA算法和SVD

    如果矩阵对某一个向量或某些向量只发生伸缩变换,不对这些向量产生旋转的效果,那么这些向量就称为这个矩阵的特征向量,伸缩的比例就是特征值.这里可以将特征值为负,特征向量旋转180度,也可看成方向不变,伸缩 ...

  8. SilverLight 浏览器出现滚动条

    照网上说的很多解决方案要不得,最后想了下,直接在body上面加 style="overflow:hidden"解决问题,真觉得微软管理混乱,很多它自己的东西都不支持了.

  9. 窗口生效函数UpdateData

    Invalidate()使整个窗口客户区无效.窗口的客户区无效意味着需要重绘,例如,如果一个被其它窗口遮住的窗口变成了前台窗口,那么原来被遮住的部分就是无效的,需要重绘.这时Windows会在应用程序 ...

  10. MyBatis3-与Spring MVC 4集成

    继前一篇的例子http://www.cnblogs.com/EasonJim/p/7052388.html,已经集成了Spring框架,现在将改造成Spring MVC的项目,并实现如下功能: 1.不 ...