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~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——拉格朗日插值的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- 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 ...
- [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 ...
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- java基础—— Collections.sort的两种用法
package com.jabberchina.test; import java.util.ArrayList; import java.util.Collections; import java. ...
- HTTP学习笔记02-HTTP报文格式之概述
HTTP学习笔记02-HTTP报文格式之概述 HTTP学习笔记02-HTTP报文格式之概述 HTTP报文格式 报文的语法 起始行 首部 实体部分 学习一个协议感觉最有意思的就是看包结构…在我看来这是唯 ...
- HTML table元素
搬运,内容来自HTML Dog. 简单示例 <!DOCTYPE html> <html> <body> <table> <tr> <t ...
- ubuntu centos macos 配置上网代理
因为我国强大的GFW,导致很多国外的应用无法安装,因为需要在系统中配置http/https代理. Ubuntu代理配置 配置方式非常简单,在~/.bashrc文件中增加: echo "exp ...
- HMM简单理解(来自quora&其他网上资料)
转载自quora: 连接:https://www.quora.com/What-is-a-simple-explanation-of-the-Hidden-Markov-Model-algorithm ...
- codevs1796 社交网络
Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之间有不同程度的关系.我们将这 ...
- rsync工具
rsync工具 一.介绍 1.可以实现 本地数据 <----------> 远程数据/本地数据 的传输 2.两种通信方式(man rsync) (1)remote shell(一个冒号 ...
- java: BufferedReader简单的菜单
BufferedReader简单的菜单 Menu boolean flag; public Menu() { while(true) { this.show(); } } public void sh ...
- Page 生命周期阶段 以及 生命周期事件
MSDN 关于 页面生命周期事件的详细介绍: http://msdn.microsoft.com/zh-cn/library/ms178472.aspx ASP.NET 页面生命周期,当IIS接收访 ...
- Python列表:元素的修改、添加、删除和排序
本文参考自<Python编程:从入门到实践>,作者:Eric Matthes,译者:袁国忠 操作 语法 举例 结果 修改元素 motocycles = ['honda', 'yamah ...