codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法
求sigma(i : 1 to n)i^k。
为了做这个题这两天真是补了不少数论, 之前连乘法逆元都不知道...
关于拉格朗日插值法, 我是看的这里http://www.guokr.com/post/456777/, 还挺有趣...
根据题目给出的例子我们可以发现, k次方的通项公式的最高次是k+1次, 根据拉格朗日插值法, 构建一个k+1次的方程需要k+2项。
然后公式是
, 对于这个题, p[i]就是i^k+(i-1)^k+(i-2)^k+.....+1^k, 这部分可以预处理出来。 自己不会搞公式 , 从http://www.cnblogs.com/qscqesze/p/5207132.html这里盗的(雾
我们发现上面就是(n-1)*(n-2)......*(n-k-2)/(n-i), 上面的那部分预处理出来, 除(n-i)相当于乘(n-i)的乘法逆元。
下面那部分就是(i-1)*(i-2)*...(i-i+1) * (i-i-1)*(i-i-2)*......(i-k-2), 相当于两个阶乘相乘, 阶乘预处理出来, 然后注意一下后面的正负号就可以了。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e6+;
ll f[maxn], fac[maxn];
ll pow(ll a, ll b) {
ll ret = ;
while(b) {
if(b&)
ret = (ret*a)%mod;
a = (a*a)%mod;
b>>=;
}
return ret;
}
int main()
{
ll n, k;
cin>>n>>k;
for(int i = ; i<=k+; i++) {
f[i] = (f[i-]+pow(i*1LL, k))%mod;
}
fac[] = ;
for(int i = ; i<maxn; i++) {
fac[i] = (fac[i-]*i)%mod;
}
if(n<=k+) {
cout<<f[n]<<endl;
return ;
}
ll cur = , ans = ;
for(int i = ; i<=k+; i++) {
cur = (cur*(n-i))%mod;
}
for(int i = ; i<=k+; i++) {
ll tmp = pow(n-i, mod-)%mod;
ll tmp1 = pow(fac[i-]%mod*fac[k+-i]%mod, mod-)%mod;
int sign = (k+-i)%?-:;
ans = (ans + sign*tmp*tmp1%mod*f[i]%mod*cur%mod)%mod;
}
ans = (ans+mod)%mod;
cout<<ans<<endl;
return ;
}
codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法的更多相关文章
- 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 ...
- Codeforces 622F The Sum of the k-th Powers
Discription There are well-known formulas: , , . Also mathematicians found similar formulas for high ...
- Codeforces 622F The Sum of the k-th Powers(数论)
题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...
- Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )
题目链接 题意 : 就是让你求个自然数幂和.最高次可达 1e6 .求和上限是 1e9 分析 : 题目给出了最高次 k = 1.2.3 时候的自然数幂和求和公式 可以发现求和公式的最高次都是 k+1 ...
- 【CF622F】The Sum of the k-th Powers (拉格朗日插值法)
用的dls的板子,因为看不懂调了好久...果然用别人的板子就是这么蛋疼- -|| num数组0~k+1储存了k+2个值,且这k+2个值是自然数i的k次方而不是次方和,dls的板子自己帮你算和的...搞 ...
- Codeforces D. The Sum of the k-th Powers(拉格朗日插值)
题目描述: The Sum of the k-th Powers time limit per test 2 seconds memory limit per test 256 megabytes i ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- codeforces 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^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 ...
随机推荐
- 基于注解的EF
首先得你的ef dll版本在4.1以上 第一步贴第一个类 由于字段太多就写一部分 [Table("NavF")]//设置表名称 public class NavF ...
- 响应式(css_media)
开始研究响应式web设计,CSS3 Media Queries是入门. Media Queries,其作用就是允许添加表达式用以确定媒体的环境情况,以此来应用不同的样式表.换句话说,其允许我们在不改变 ...
- ubuntu FTP服务安装
//安装vsftp apt-get install vsftpd -y //增加账号 //1 查找 nologin位置 /usr/sbin/nologin useradd -d /alidata/ww ...
- Bower前端包管理器
bower 前端包管理器 为什么要用它 一个新的web项目开始,我们总是很自然地去下载需要用到的js类库文件,比如jQuery.随着项目的进展,我们可能又需要Bootstrap3,其中每个框架之间的依 ...
- html 中的name,id ,value,class,list 作用与区别
name: 单独一个网页中,一个控件是否设置name不会影响这个网页功能的实现.当我们需要把这个控件 所关联的数据传递到数据库时,就必须设置name属性,否则这个值是没办法传到服务器保存的: id: ...
- HTML系列(四):编辑图像
一.图像的基本概念 1.矢量图:文件占用空间小,放大后图像不会失真,和分辨率无关.适用于图形设计.文字设计.标志设计.版式设计等. 2.位图:由像素点组成,文件较大,放大和缩小图像会失真. 3.有损压 ...
- Jar包下载地址
Download Apache log4j 1.2.17下载: http://logging.apache.org/log4j/1.2/download.html jsoup http://jsoup ...
- Oracle操作数据库oracleHelper
1 Oracle OracleHelper //============================================================================ ...
- mysql中中文字符串的截取
SET NAMES 'utf8'; SET v_length = character_length(citys); SET citys = left(citys COLLATE utf8_bin, v ...
- html回车事件
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...