题意

求\(\sum_{i=1}^n i^k\),\(n \leq 10^9,k \leq 10^6\)

题解

观察可得答案是一个\(k+1\)次多项式,我们找\(k+2\)个值带进去然后拉格朗日插值

\(n+1\)组点值\((x_i,y_i)\),得到\(n\)次多项式\(f\)的拉格朗日插值方法:

\[f(x) = \sum_{i = 0}^n y_i\prod_{j\not =i} \frac{x-x_j}{x_i-x_j}
\]

时间复杂度为\(O(n^2)\).

现在考虑这题,我们把\(1\)到\(k+2\)带入,有很好的性质:对于每个\(i\),分母是\(1\)乘到\(i-1\)再乘上\(-1\)乘到\(i-k-2\),这可以预处理阶乘\(O(1)\)处理。分子可以预处理前后缀积来\(O(1)\)得到

于是时间复杂度为\(O(n)\),可以通过

#include <algorithm>
#include <cstdio>
using namespace std; const int mo = 1e9 + 7;
const int N = 1e6 + 10; int pl[N], pr[N], fac[N]; int qpow(int a, int b) {
int ans = 1;
for(; b >= 1; b >>= 1, a = 1ll * a * a % mo)
if(b & 1) ans = 1ll * ans * a % mo;
return ans;
} int main() {
int n, k, y = 0, ans = 0;
scanf("%d%d", &n, &k);
pl[0] = pr[k + 3] = fac[0] = 1;
for(int i = 1; i <= k + 2; i ++)
pl[i] = 1ll * pl[i - 1] * (n - i) % mo;
for(int i = k + 2; i >= 1; i --)
pr[i] = 1ll * pr[i + 1] * (n - i) % mo;
for(int i = 1; i <= k + 2; i ++)
fac[i] = 1ll * fac[i - 1] * i % mo;
for(int i = 1; i <= k + 2; i ++) {
y = (y + qpow(i, k)) % mo;
int a = pl[i - 1] * 1ll * pr[i + 1] % mo;
int b = fac[i - 1] * ((k - i) & 1 ? -1ll : 1ll) * fac[k + 2 - i] % mo;
ans = (ans + 1ll * y * a % mo * qpow(b, mo - 2) % mo) % mo;
}
printf("%d\n", (ans + mo) % mo);
return 0;
}

「CF622F」The Sum of the k-th Powers「拉格朗日插值」的更多相关文章

  1. Codeforces D. The Sum of the k-th Powers(拉格朗日插值)

    题目描述: The Sum of the k-th Powers time limit per test 2 seconds memory limit per test 256 megabytes i ...

  2. CF622F-The Sum of the k-th Powers【拉格朗日插值】

    正题 题目链接:https://www.luogu.com.cn/problem/CF622F 题目大意 给出\(n,k\),求 \[\sum_{i=1}^ni^k \] 解题思路 很经典的拉格朗日差 ...

  3. CF622F The Sum of the k-th Powers(拉格朗日插值)

    题意 给出 \(n,k\) , \(n\le10^9,k\le10^6\) ,求 \(\sum_{i=1}^n i^k(mod\;10^9+7)\) 题解 自然数幂次和,是一个\(k+1\)次多项式, ...

  4. 「AGC030D」Inversion Sum

    「AGC030D」Inversion Sum 传送门 妙啊. 由于逆序对的个数最多只有 \(O(n^2)\) 对,而对于每一个询问与其相关的逆序对数也最多只有 \(O(n)\) 对,我们可以对于每一对 ...

  5. Note -「Lagrange 插值」学习笔记

    目录 问题引入 思考 Lagrange 插值法 插值过程 代码实现 实际应用 「洛谷 P4781」「模板」拉格朗日插值 「洛谷 P4463」calc 题意简述 数据规模 Solution Step 1 ...

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

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

  8. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  9. 862. 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 ...

随机推荐

  1. jQuery中获取相对文档的坐标的方法是什么?

    offset(),获取匹配元素在当前视口的相对偏移.返回的对象包含两个整型属性:top 和 left,以像素计.此方法只对可见元素有效. position()获取匹配元素相对定位父级的偏移.没有定位父 ...

  2. PHP字符串的处理(一)-字符串初识和比较

    在PHP中,字符和字节一样,共有256种不同字符的可能性,PHP对Unicode没有本地支持,一个GB2312编码的汉字占2字节,一个UTF-8编码的汉字占3字节字符串看作字符集和时,并不是真正的数组 ...

  3. vue 构建前端项目并关联github

    这几天尝试用node开发一个网站,后端的接口已经初步开发完成,现在开始构建前端的项目,记录下过程,在学习下吧. 用vue-cli 构建项目,myproject.(构架过程略过) 每次在本地构建项目后和 ...

  4. 【知识碎片】Asp.Net 篇

    51.app.config 连接字符串 <?xml version="1.0" encoding="utf-8"?> <configurati ...

  5. Leetcode:Task Scheduler分析和实现

    题目大意:提供k个任务,这些任务没有依赖关系(即可以任意调度).CPU完成一个任务需要耗时一个时间片段,当执行完一个任务后,相同的任务必须在n个时间片段才能得以执行.请问CPU通过调度最快能在多少时间 ...

  6. 【HDU6024】Building Shops

    题意 有n个教室排成一排,每个教室都有一个坐标,现在,小Q想建一些糖果商店,在这n个教室里面.总的花费有两部分,在教室i建一个糖果屋需要花费ci,对于没有任何糖果屋的P,需要的花费为这个教室到它左边有 ...

  7. 【bzoj3940】[Usaco2015 Feb]Censoring

    [题目描述] FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词 记为t_1...t_N.他希望从S中删除这些单词.  FJ每次 ...

  8. linux上搭建图片服务器

    之前写过一个搭建图片服务器的随笔:https://www.cnblogs.com/xujingyang/p/7163290.html   ,现在回头看看,我去,感觉写的好乱,现在再整一个吧.o(╯□╰ ...

  9. win7安装linux双系统

    整体流程大概就是下载启动盘制作工具以及linux镜像,这些步骤网上很多,我就不再重复了 这里以centos举例说几个我踩到的坑吧 1.选择开始安装后提示 Warning: /dev/root does ...

  10. vs2008评估期已过的解决方法[win7]

    以下是网上提供的方法(对win7无效): 启动visual studio 2008后显示对话框:visual studio的试用版评估期已结束.下面有两个按钮,点第一个链接到微软网页,第二个直接关闭. ...