## [$>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. uefi模式下win10安装双系统ubuntu18.04LTS

    自己折腾了半天,血与泪啊(难得一个可爱的周末 wwww我一定要写下来 跟这个博客几乎一模一样了 https://blog.csdn.net/xrinosvip/article/details/8042 ...

  2. ES6 中 Array.from() 不能得到的数组元素是 undefined 或是空数组

    本文地址:http://www.cnblogs.com/veinyin/p/7944072.html  正确格式 let json = { '0': 'Waaaa~', '1': 'Hello,', ...

  3. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  4. 2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)

    题目链接 Problem Description You are given an array A , and Zhu wants to know there are how many differe ...

  5. 天梯赛 L2-009 抢红包

    题目链接 没有人没抢过红包吧-- 这里给出N个人之间互相发红包.抢红包的记录,请你统计一下他们抢红包的收获. 输入格式: 输入第一行给出一个正整数N(<= 104),即参与发红包和抢红包的总人数 ...

  6. 清理oracle的用户中的日志垃圾以及修改sys用户的密码

    清理oracle的用户中的日志垃圾1.进入:/opt/oracle/product/11g/network/admin目录2.注释掉listener.ora文件中的TRACE_LEVEL_LISTEN ...

  7. 窗口生效函数UpdateData

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

  8. AtCoder Non-decreasing(数学思维)

    题目链接:https://abc081.contest.atcoder.jp/tasks/arc086_b 题目大意:有n个数,最多可以执行2*n次操作,每次可以选择将ai加到aj上,最终使得该序列满 ...

  9. java中Property类的基本用法

    1 配置.properties文件 2 获取输入流的方法 1)FileInputStream fi = new FileInputStream(properties文件路径); 2)InputStre ...

  10. 两天撸一个天气应用微信小程序

    更新说明: I.气象数据由百度地图开放平台修改为了和风天气,需要注册账号获取 key: II.d0e51c8 版本之后为小程序云开发版本,若未开通云开发功能,为不影响小程序正常运行,可以将版本号回退到 ...