CF 622F The Sum of the k-th Powers——拉格朗日插值
题目:http://codeforces.com/problemset/problem/622/F
发现 sigma(i=1~n) i 是一个二次的多项式( (1+n)*n/2 ),sigma(i=1~n) i^2 是一个三次的多项式,所以 sigma(i=1~n) i^k 是一个k+1次的多项式。用拉格朗日插值就能做了。
注意别弄成 n^2 的。其实就是移动一个位置的时候乘一个数除以一个数,这样的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e6+,mod=1e9+;
int n,k,a[N],inv[N],ans;
int pw(int x,int k)
{int ret=;while(k){if(k&)ret=(ll)ret*x%mod;x=(ll)x*x%mod;k>>=;}return ret;}
void upd(int &x){x>=mod?x-=mod:;}
int main()
{
scanf("%d%d",&n,&k); int lm=k+;
if(n<=lm)
{
for(int i=;i<=n;i++)a[i]=a[i-]+pw(i,k),upd(a[i]);
printf("%d\n",a[n]); return ;
}
for(int i=;i<=lm;i++)inv[i]=pw(i,mod-);
int s0=;
for(int i=;i<=lm;i++)s0=(ll)s0*(n-i)%mod;
int s1=,fx=((lm-)&?-:);
for(int i=;i<=lm;i++)s1=(ll)s1*(i-)%mod;
a[]=;
ans=(ll)s0*pw(n-,mod-)%mod*fx*pw(s1,mod-)%mod*a[]%mod;
for(int i=;i<=lm;i++)
{
a[i]=a[i-]+pw(i,k);upd(a[i]);
fx=-fx;
s1=(ll)s1*(i-)%mod*inv[lm-i+]%mod;
ans=(ans+(ll)s0*pw(n-i,mod-)%mod*fx*pw(s1,mod-)%mod*a[i])%mod;
}
printf("%d\n",ans<?ans+mod:ans);
return ;
}
CF 622F The Sum of the k-th Powers——拉格朗日插值的更多相关文章
- 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 ...
- Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值
The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...
- [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 ...
- [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- 在树莓派上用Python控制LED
所需材料 一个已经安装配置好了的树莓派 连接控制树莓派所用的其他必须设备 200Ω电阻 x 8 led x 8 面包板及连接线若干 电路连接 电路图 按照电路图所示,在面包板上进行连接. 编写程序 安 ...
- android驱动USB摄像头
http://blog.csdn.net/mirkerson/article/details/50764314 多亏了stackoverflow看到的一篇帖子,其中有几句关键的话,然后顺藤摸瓜解决了问 ...
- P4501 [ZJOI2018]胖
题目 P4501 [ZJOI2018]胖 官方口中的送分题 做法 我们通过手玩(脑补),\(a_i\)所作的贡献(能更新的点)为:在\(a_i\)更新\(\forall x\)更新前前没有其他点能把\ ...
- awk中使用shell变量
方法:使用-v参数. 对于多个shell变量使用多个-v 有个关于shell变量中的空格问题:
- MySQL-LRU_List Free_List Flush_List
关于 LRU_List ,Free_List,Flush_List的介绍: LRU算法:(Latest Recent Used)最近最少使用 数据库的缓冲池通过LRU算法来进行管理. ...
- JAVAWeb学习总结(3)
JavaWeb学习总结(三)——Tomcat服务器学习和使用(二) 一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命 ...
- 算法总结之 数组的partition调整
给定一个有序数组arr, 调整arr使得这个数组的左半部分没有重复元素且升序,而且不用保证右边是否有序 分区就ok了 u区是 无重复且升序的 u是这个区域的最后位置,初始u=0 i做从左到右的遍历, ...
- Shell中数学计算/运算
shell中的赋值和操作默认都是字符串处理. 1)使用let(只能进行整数运算)var=1let "var+=1"echo $var输出结果为2 注意:a)let几乎支持所有的运算 ...
- Qt 自定义PushButton
http://blog.csdn.net/zddblog/article/details/11116191 功能:鼠标弹起并在按键区域内时,按键响应.并实现normal.hover.pressed效果 ...
- httpClient的post方法使用form格式数据调用接口
Http使用post调用接口,接口只接受form表单数据格式问题? 这个问题的关键是form表单提交的是数据格式是什么样子的,使用httpClient工具类时Header信息如何写. 会影响解决问题思 ...