题目: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 —— 拉格朗日插值的更多相关文章

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

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

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

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

  5. [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~ ...

  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. CF 633 F. The Chocolate Spree 树形dp

    题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...

  8. CF 868 F. Yet Another Minimization Problem

    F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...

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

随机推荐

  1. angular md-toast 颜色

    How to show md-toast with background color https://codepen.io/neilkalman/pen/jWBqve <div ng-contr ...

  2. vForum 2014点滴随笔

    vForum2014 的口号:NO Limits 纵横无限 一条好消息:VMware 将在中国建立亚洲研究院,并在5年内投资10亿美元. VMware宋先生的演讲再次印证了Redhat会议上的趋势: ...

  3. 百度地图 创建 自定义控件(vue)

    1.组件代码 Bmap.vue <!-- 离线地图 组件 --> <template> <div id="map" :style="styl ...

  4. background-attachment

      CreateTime--2017年9月28日10:58:58 Author:Marydon background-attachment 1.定义 定义背景图片随滚动轴的移动方式(设置背景图像是否固 ...

  5. python(3)- 循环语句:从最内层跳出多层循环

    跳出多层循环:三层循环,最里层直接跳出3层 方法一: 在Python中,函数运行到return这一句就会停止,因此可以利用这一特性,将功能写成函数,终止多重循环 def work(): #定义函数 f ...

  6. Amazon SNS移动推送更新——新增百度云推送和Windows平台支持

    Amazon SNS(Simple Notification Service)是一种基于云平台的消息通知和推送服务. SNS提供简单的 Web 服务接口和基于浏览器的管理控制台让用户可以简易设置.执行 ...

  7. hdu4455 dp

    pid=4455">http://acm.hdu.edu.cn/showproblem.php?pid=4455 Substrings Time Limit: 10000/5000 M ...

  8. 在Fedora 23上安装多媒体解码器

    在Fedora 23上安装多媒体解码器 时间:2016-06-25来源:topspeedsnail.com 作者:斗大的熊猫   安装多媒体解码器允许你播放更多格式的音频和视频格式.大多数这些解码器都 ...

  9. 使用Apache Commons Chain(转载)

    原博客出处:http://phil-xzh.iteye.com/blog/321536 使用Commons Chain 作为程序开发人员,我们经常需要对一个实际上程序性的系统应用面向对象的方法.商业分 ...

  10. 第 3 章 第 1 题 精简冗余 if 语句问题 使用数组实现

    问题分析 输入:用户个人收入 输出:该用户要缴纳的个人所得税 约束:不允许使用过多的 if 语句 解答思路 最简单的方法是根据不同的税率区间,创建多个 if 语句来求解.但如此一来便会有 25 个 i ...