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
因为差值是 k 次的,所以 f 的次数应该是 k+1;
算出 k+2 个值就可以用拉格朗日插值求解了;
但是 k^2 会 T;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=1e6+,mod=1e9+;
int n,k,f[xn];
int pw(ll a,int b)
{
ll ret=;
for(;b;b>>=,a=(a*a)%mod)
if(b&)ret=(ret*a)%mod;
return ret;
}
int upt(int x){while(x>=mod)x-=mod; while(x<)x+=mod; return x;}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=k+;i++)f[i]=upt(f[i-]+pw(i,k));//!k+1
int sum=;
for(int i=;i<=k+;i++)
{
ll s1=,s2=;
for(int j=;j<=k+;j++)
{
if(i==j)continue;
s1=s1*(n-j)%mod;
s2=s2*(i-j)%mod;
}
sum=(sum+s1*pw(s2,mod-)%mod*f[i])%mod;
}
printf("%d\n",upt(sum));
return ;
}
TLE
由于 x 都是连续的,所以可以直接预处理阶乘;
分母部分的阶乘要跳过0,比较麻烦...一开始准备先算一个值,然后每次修改,但是失败了...
于是参考了一下 TJ,原来可以分正负两部分计算!
分子万一乘到0怎么办?先特判一下 n<=k+2 即可;
别忘了阶乘数组是 ll 或局部开 ll !
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=1e6+,mod=1e9+;
int n,k,f[xn];
ll fac[xn];//ll
int pw(ll a,int b)
{
ll ret=;
for(;b;b>>=,a=(a*a)%mod)
if(b&)ret=(ret*a)%mod;
return ret;
}
int upt(int x){while(x>=mod)x-=mod; while(x<)x+=mod; return x;}
ll get(int i)
{
if(i==k+)return fac[i-];
return fac[i-]*fac[k+-i]%mod;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=k+;i++)f[i]=upt(f[i-]+pw(i,k));//!k+1
if(n<=k+){printf("%d\n",f[n]); return ;}//!!
int sum=; ll s1=; fac[]=;
for(int i=;i<=k+;i++)fac[i]=fac[i-]*i%mod;
for(int i=;i<=k+;i++)s1=s1*(n-i)%mod;
//for(int i=2;i<=k+2;i++)s2=s2*(1-i)%mod;
for(int i=;i<=k+;i++)
{
ll t1=s1*pw(n-i,mod-)%mod;
ll s2=pw(get(i),mod-);
if((k+-i)%)s2=-s2;
//if(i>1)s2=s2*pw(upt(i-1-k-2),mod-2)%mod*(i-1)%mod;
sum=(sum+t1*s2%mod*f[i])%mod;
}
printf("%d\n",upt(sum));
return ;
}
CF 622 F The Sum of the k-th Powers —— 拉格朗日插值的更多相关文章
- Codeforces 622 F. The Sum of the k-th Powers
\(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...
- 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 ...
- 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 ...
- 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 ...
- [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~ ...
- [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 ...
- CF 633 F. The Chocolate Spree 树形dp
题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...
- CF 868 F. Yet Another Minimization Problem
F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...
- 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 ...
随机推荐
- favico是针对网页图标内容更改
favico.js源码 (function () { var Favico = function (opt) { "use strict"; opt = opt ? opt : { ...
- MySQL高可用之——keepalived+互为主从
目标:配置一个keepalived双机热备架构,并配置主从复制 规划: master1 zlm177 192.168.17.177 master2 zlm188 192 ...
- mysql用户修改登录密码及授予用户远程登录权限
一.修改用户登录密码: mysql> show databases;ERROR 1820 (HY000): You must SET PASSWORD before executing this ...
- Controller//控制器
#include<opencv2\core\core.hpp> #include<opencv2\imgproc\imgproc.hpp> #include<opencv ...
- sublime 快捷键 汇总--长期
Ctrl+P 输入当前项目中的文件名,快速搜索文件 Ctrl+G 输入数字跳转到该行代码 Ctrl+R 输入关键字,查找文件中的函数名 Ctrl+: 输入关键字,查找文件中的变量名.属性名等 Ctrl ...
- align="absmiddle" 图片的中间上下对齐
align=absmiddle表示图像的中间与同一行中最大元素的中间对齐 AbsBottom 图像的下边缘与同一行中最大元素的下边缘对齐.AbsMiddle 图像的中间与同一行中最大元素的中间对齐.B ...
- mnesia练习及基本操作
Mnesia基本用法 查看表结构 查看mnesia表的结构: mnesia:info(). 查看此表的基本信息: mnesia:table_info(<tableName>, all). ...
- apktool + eclipse 动态调试APK
用了会AndBug,尽管挺强大的可是作为习惯了OD.EDB作为动态调试工具的人,自然有些不习惯,于是乎寻求新的动态调试解决方式.但大多数都是NetBeans + apktool.想着还得多下一个IDE ...
- RS485总线典型电路介绍
一.RS485总线介绍: RS485总线是一种常见的串行总线标准,采用平衡发送与差分接收的方式,因此具有抑制共模干扰的能力.在一些要求通信距离为几十米到上千米的时候,RS485总线是一种应用最为广泛的 ...
- TControl,TWinControl和TGraphicControl的显示函数
-------------------------- 显示隐藏刷新 -------------------------- TControl = class(TComponent)procedure S ...