题目: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. eclipse下的ssh框架整合过程及測试

    最近在搭建Stuts2+hibernate+spring的框架,网上看的教程,大部分都是非常easy的步骤.没有比較具体的步骤以及每一个步骤完毕之后怎样检查是否配置成功.下面是笔者依据自己搭建的过程进 ...

  2. 百科知识 kux文件如何打开

    即使是官方自带的浏览器也无法打开   你可以直接复制文件名   然后在百度里搜即可   你自己下载的东西还是能转码的      

  3. 网站无法显示logo?

    那是因为你没有配置favicon.ico,每个网站根目录都会有一个favicon.ico,因为每个服务器都会请求根目录下的它.

  4. Android_自己定义切换控件SwitchView

    1.示意图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemltbzIwMTM=/font/5a6L5L2T/fontsize/400/fill/I0JB ...

  5. C语言-多重背包问题

    多重背包问题 问题:有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 分 ...

  6. python 写数据到txt 文件

    # coding=utf-8 import codecs # list = [[1,2],[3,4]] list = ['{"PN":"34VT123",&qu ...

  7. Can&#39;t open named pipe to host: . pipe: MySQL

    今天遇到mysql连接odbc时报例如以下错误:Can't open named pipe to host: . pipe: MySQL 错误截图例如以下: 依照网上说的方法包含mysql的官方说法都 ...

  8. nanoporetech/nanonet

    nanoporetech/nanonet CodeIssues 7Pull requests 0Projects 0Wiki Insights  First generation RNN baseca ...

  9. mvc 发送QQ邮件

    试图部分代码: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } ...

  10. libcurl理解和使用

    1 libcurl是一个很好的客户端库 2 CURLOPT_URL 就是普通的url. 3 CURLOPT_HTTPHEADER 3.1 http get 4 CURLOPT_WRITEFUNCTIO ...