题目: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. Flash如何为文字描边

    可以使用墨水瓶工具,但是要先把文字打散(可以打散之后再组合起来)粗细和颜色都可以调,粗细就是笔触,颜色就是前景色(边框颜色)  

  2. Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  3. sonar + ieda实现提交代码前代码校验

    代码风格不同一直是一件停头疼的事情,因为不同的工作经验,工作经历,每个人的代码风格不尽相同,造成一些代码在后期的维护当中难以维护, 查阅一些资料之后发现 idea + sonar 的方式比较适合我,实 ...

  4. C#数据类型与数据库字段类型对应

    数据库 C#程序 int int32 text string bigint int64 binary System.Byte[] bit Boolean char string datetime Sy ...

  5. JS 常用字符串操作

    Js字符串操作函数大全 /*******************************************                        字符串函数扩充              ...

  6. Java多态特性:重载和覆写的比較

    Java重载: 在同一个类中 方法具有同样的名字,同样或不同的返回值,但參数不同的多个方法(參数个数或參数类型) public class MethoDemo{ public static void ...

  7. Mvc Autofac构造器注入

    新建MVC项目,添加程序集引用 定义接口ILog public interface ILog { string Save(string message); } 类TxtLog实现接口ILog publ ...

  8. LeetCode(83)题解: Remove Duplicates from Sorted List

    https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, de ...

  9. android-async-http框架

    android-async-http 简单介绍:An asynchronous, callback-based Http client for Android built on top of Apac ...

  10. esri和ArcGIS

    1 esri esri是environment system research institute,环境系统研究所.总部在美国加州.它是世界上最大的GIS技术提供商. 主要产品有ArcGIS.ArcV ...