「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 ...
随机推荐
- 内网渗透神器xerosploit
项目地址:https://github.com/LionSec/xerosploit 安装完成后直接在终端输入xerosploit打开 显示了本机的内网ip,mac地址,网关,网卡,输入help查看帮 ...
- Python Twisted架构英文版
原作出处:twisted-intro 作者:Dave 转载声明:版权归原作出处所有,转载只为让更多人看到这部优秀作品合集,如果侵权,请留言告知 Twisted Introduction This mu ...
- LCD 调试总结
转自:http://blog.csdn.net/qikaibinglan/article/details/5630246 (1) 液晶显示模式 并行:MCU接口.RGB接口.Vysnc接口 串行:SP ...
- 1 任务管理 --转载于电子工程世界
uC/OS-II 中最多可以支持64 个任务,分别对应优先级0-63,其中0 为最高优先级.63为最低级,系统保留了4个最高优先级的任务和4个最低优先级的任务,所有用户可以使用的任务数有56个. uC ...
- apk安装包信息
String archiveFilePath="sdcard/DangDang.apk";//安装包路径 PackageManager pm = getPacka ...
- objc变量的获取
[objc变量的获取] C++成员变量通过偏移来寻找,速度极快.But Objc中的变量通过方法调用来寻找,方法首先根据变量名,找到ivar_t,然后在ivar_t对象中取出偏移,再用此偏移来取值(这 ...
- json_encode和json_decode和isset和array_key_exists
1.json_decode() json_decode — 对 JSON 格式的字符串进行编码 说明 mixed json_decode ( string $json [, bool ...
- vware 中 red hat linux NAT模式上网配置
NAT模式的具体配置NAT方式:虚拟机可以上外网,可以访问宿主计算机所在网络的其他计算机(反之不行). 未修改之前的eth0
- opennebula 发送序列化ID,构造json格式错误
- 简单Factory模式
#pragma once #include "student.h" #include "Teacher.h" typedef enum _EPersonType ...