题目: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. nand flash详解及驱动编写

    https://www.crifan.com/files/doc/docbook/linux_nand_driver/release/html/linux_nand_driver.html#nand_ ...

  2. Struts2笔记03——架构(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm 架构(很重要!尤其是图!) 从一个比较高的层次来看,St ...

  3. 【Head First Servlets and JSP】笔记12:URL重写

    1.会话管理的核心在于“交换会话ID”,来回传递cookie是最简单的方法,容器通过客户端发来的JSSESIONID查找匹配的对话. 2.如果浏览器禁用了cookie,那就意味着浏览器将忽略响应首部中 ...

  4. Django 路由、模板和模型系统

    一.路由系统 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个URL调用这 ...

  5. 修改和重置WAMP的phpMyAdmin密码

    WAMP是Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python,常用来搭建动态网站或者服务器的开源软件. 1.下载页面:http://www.wampserver ...

  6. What's the difference between using “let” and “var” to declare a variable in JavaScript?

    https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare ...

  7. Linux下Python科学计算包numpy和SciPy的安装

      系统环境: OS:RedHat5 Python版本:Python2.7.3 gcc版本:4.1.2 各个安装包版本: scipy-0.11.0 numpy-1.6.2 nose-1.2.1 lap ...

  8. html页面转JSP之后样式变化的问题

    html 保存为jsp   样式变化了  ,比如里面的一些input 获知是其他的一些样式变化的. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  9. EntityFramework 学习 一 Validate Entity

    可以为实体实现自定义验证,重写DBContext中的个ValidateEntity方法 protected override System.Data.Entity.Validation.DbEntit ...

  10. Spark 属性配置

    1.Spark1.x 属性配置方式 Spark属性提供了大部分应用程序的控制项,并且可以单独为每个应用程序进行配置. 在Spark1.0.0提供了3种方式的属性配置: SparkConf方式 Spar ...