题目:http://codeforces.com/problemset/problem/622/F

发现 sigma(i=1~n) i 是一个二次的多项式( (1+n)*n/2 ),sigma(i=1~n) i^2 是一个三次的多项式,所以 sigma(i=1~n) i^k 是一个k+1次的多项式。用拉格朗日插值就能做了。

注意别弄成 n^2 的。其实就是移动一个位置的时候乘一个数除以一个数,这样的。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e6+,mod=1e9+;
int n,k,a[N],inv[N],ans;
int pw(int x,int k)
{int ret=;while(k){if(k&)ret=(ll)ret*x%mod;x=(ll)x*x%mod;k>>=;}return ret;}
void upd(int &x){x>=mod?x-=mod:;}
int main()
{
scanf("%d%d",&n,&k); int lm=k+;
if(n<=lm)
{
for(int i=;i<=n;i++)a[i]=a[i-]+pw(i,k),upd(a[i]);
printf("%d\n",a[n]); return ;
}
for(int i=;i<=lm;i++)inv[i]=pw(i,mod-);
int s0=;
for(int i=;i<=lm;i++)s0=(ll)s0*(n-i)%mod;
int s1=,fx=((lm-)&?-:);
for(int i=;i<=lm;i++)s1=(ll)s1*(i-)%mod;
a[]=;
ans=(ll)s0*pw(n-,mod-)%mod*fx*pw(s1,mod-)%mod*a[]%mod;
for(int i=;i<=lm;i++)
{
a[i]=a[i-]+pw(i,k);upd(a[i]);
fx=-fx;
s1=(ll)s1*(i-)%mod*inv[lm-i+]%mod;
ans=(ans+(ll)s0*pw(n-i,mod-)%mod*fx*pw(s1,mod-)%mod*a[i])%mod;
}
printf("%d\n",ans<?ans+mod:ans);
return ;
}

CF 622F The Sum of the k-th Powers——拉格朗日插值的更多相关文章

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

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

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

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

  5. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  6. 862. 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. [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组

    Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...

  8. 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...

  9. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

随机推荐

  1. TIJ读书笔记02-控制执行流程

      TIJ读书笔记02-控制执行流程 TIJ读书笔记02-控制执行流程 if-else 迭代 无条件分支 switch语句 所有条件语句都是以条件表达式的真假来决定执行路径,也就是通过布尔测试结果来决 ...

  2. awk的内置函数

    常见awk内置数值函数

  3. imx6q Xorg下Qt应用开机自启动及添加桌面快捷启动图标的方法

    启动scene3d例子: root@imx6qsabresd:/etc/X11/Xsession.d# ls -altotal 36drwxr-xr-x 2 root root 4096 Mar 12 ...

  4. 快乐学习 Ionic Framework+PhoneGap 手册1-4 {登录页面}

    编程的快乐和乐趣,来自于能成功运行程序并运用到项目中 有了面板然后加个登录页面,请看效果图和代码 Index HTML Code <!DOCTYPE html> <html ng-a ...

  5. class_alias--为一个类创建别名

    class_alias--为一个类创建别名 bool class_alias ( string $original , string $alias [, bool $autoload = TRUE ] ...

  6. Linux与Android 多点触摸协议【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7833277 一.Linux与Android 多点触摸协议 为了使用功能强大的多点触控设 ...

  7. 1.linux源码安装nginx

    从官网下载nginx.tar.gz源码包 拷贝至Linux系统下进行解压 tar -zxvf nginx.tar.gz 进入解压后的目录,需要./configure,此步骤会报多个错,比如没有安装gc ...

  8. 【计算机网络】OSI模型,TCPIP模型

    今天给大家分享的是两种模型的主要区别,夜视比较容易混淆的地方.我尽力用图形的方式来说问题,这样比较好理解~ (PS:画图真的不会,正在认真学,希望多多包含:)) 一.二者的模型对比 (这个图有点丑.. ...

  9. Flume架构及运行机制

    flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original generation),属于 clo ...

  10. jsp 内置对象---EL

    ServletRequest : java.lang.String      getParameter(java.lang.String name) 返回一个string           对应 n ...