「CF622F」The Sum of the k-th Powers「拉格朗日插值」
题意
求\(\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\)的拉格朗日插值方法:
\]
时间复杂度为\(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「拉格朗日插值」的更多相关文章
- 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 ...
- CF622F-The Sum of the k-th Powers【拉格朗日插值】
正题 题目链接:https://www.luogu.com.cn/problem/CF622F 题目大意 给出\(n,k\),求 \[\sum_{i=1}^ni^k \] 解题思路 很经典的拉格朗日差 ...
- 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\)次多项式, ...
- 「AGC030D」Inversion Sum
「AGC030D」Inversion Sum 传送门 妙啊. 由于逆序对的个数最多只有 \(O(n^2)\) 对,而对于每一个询问与其相关的逆序对数也最多只有 \(O(n)\) 对,我们可以对于每一对 ...
- Note -「Lagrange 插值」学习笔记
目录 问题引入 思考 Lagrange 插值法 插值过程 代码实现 实际应用 「洛谷 P4781」「模板」拉格朗日插值 「洛谷 P4463」calc 题意简述 数据规模 Solution Step 1 ...
- [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 ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- 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 ...
随机推荐
- 屏蔽sql server 2008的智能提示
1. 去掉这个勾 2. 如果你安装了Sql complete, 接着下面的
- 012. MVC5中Razor引擎使用模板页
1.文件→新建项目→框架选择.NET Framework 4.5 2.确定后选择ASP.NET 4.5 模板→MVC→为以下项添加文件夹和核心引用→MVC, 在vs 2015中默认就使用的Razor引 ...
- 使用原生js创建自定义标签
使用原生js创建自定义标签 效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- windows 10 服务组件安装出现0x80070422错误解决方法
问题描述: windows 10通过控制面板->程序->启用或关闭Windows 功能安装Windows服务或组件时,出现0x80070422错误,提示服务禁止或未启用 问题产生原因: ...
- 在.jsp中非表单请求action的几种方式总结
转自:https://www.jb51.net/article/35621.htm 1 一: 复制代码 代码如下: <a href="userAction.do?flag=user_r ...
- entityframework.extended 配置mysql
entityframework.extended 这个是个很好的扩展,不过由于默认是配置成MSSQL的,今天在github上面 看到一个解决方案,亲测可用,下面贴代码 1.在DbContext 修改默 ...
- FPGA和CPLD的比较
1 FPGA的集成度比CPLD高,具有更复杂的布线结构和逻辑实现. 2 CPLD更适合触发器有限而乘积丰富的结构,更适合完成复杂的组合逻辑:FPGA更适合于触发器丰富的结构,适合完成时序逻辑. 3 c ...
- linux上的第一个c语言程序
1.编辑源文件 输入命令如下: root@ubuntu:/home/amy# vi hello.c 文件内容如下: #include<stdio.h> int main() { print ...
- 766. Toeplitz Matrix斜对角矩阵
[抄题]: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...
- IFM设备 Linux方面资料
Github: https://github.com/lovepark/ifm3d