题目: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. 数组全排列 knuth 分解质因数

    template<typename T> void swap(T* a, T* b) { T temp = *a; *a = *b; *b = temp; } //数组的全排列 void ...

  2. 第一个AngularJS Sample

    代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3. ...

  3. weblogic的几点配置

    2.在tomcat下写过滤器以后还有的地方需要手工转码<-->weglobic下也不用 eg:SubjectAction.java3.weblogic下anltr.jar有冲突,需要从外界 ...

  4. mysql字符太长警告

    用navicateclient,打开相应的数据库. 打开函数.找相应的val()函数,进行编辑,就能够!编辑范围为4000

  5. VS + Qt5Designer + Anaconda环境配置

    最近打算做一个模型训练工具,从来都不喜欢做UI的我,最终把目光放在了QtDesigner上.配环境的过程中在网上翻阅了不少博客,但大多是pycharm或者是VScode,使用VS的似乎不多.所以打算记 ...

  6. ORA-00600: internal error code, arguments: [2662], [0], [1106971], [0], [1107731], [12583040]

    今天是2014-06-06,在进行数据库恢复的时候出现了一个久违的ora-600 [2662]错误.特整理例如以下: 问题描写叙述: system及数据文件误删,採用恢复数据文件的方式将数据库恢复到開 ...

  7. disabled和readonly

    项目中,有一个input控件,input的值需要通过点击一个javascript链接,从弹出的对话框中所列出的项中选择.而不能从input框中直接输入. 刚开始将input的disabled属性设置为 ...

  8. Struts2 (三) (转载)

    前面一直在说Action可以是一个普通的Java类,与Servlet API完全分离,但是为了实现业务逻辑,Action需要使用HttpServletRequest内容.Struts 2设计的精巧之处 ...

  9. js比较3个数字的大小

    <script> var a = [1, 5, 2, 123, 34, 43, 7]; var i = j = t = 0; //sort方法, 推荐使用 a.sort(function( ...

  10. 学习某些API的方法

    学习某些 API 的方法 这里的 API 可能是某个系统平台,开发包,开发平台,开发工具等等,因为任何系统和技术方法提供给开发者的打包方式都是一系列 API . 无论你有在哪一层级开发,从硬件驱动到系 ...