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 ...
随机推荐
- 【leetcode边做边学】二分查找应用
很多其它请关注我的HEXO博客:http://jasonding1354.github.io/ 简书主页:http://www.jianshu.com/users/2bd9b48f6ea8/lates ...
- JVM参数
下面给出各种可以用户设置堆大小的命令行参数.注释列提供了一些设置参数的初级或高级建议.此外,后面还会给出更多的详细建议. 参数 描述 注释 -Xms<size> 设置堆的最小值 在生产阶段 ...
- .NET读取Project 2007 MPP项目文件
Project文件读取: 方法1:Microsoft.Project.OLEDB.11.0 string strConn = "Provider=Microsoft.Project.OLED ...
- SQL练习之不反复执行相同的计算
下面是Demo所需要的代码: CREATE TABLE Fee ( Income ,), overhead ,) ) ,) ,) ,) ,) ,) ,) 现在有一个报表系统,需要根据Fee表获得以下数 ...
- Infragistics的介绍以及在ASP.net中使用的总结
Infragistics系列控件是一套很好,很强大的控件,.感觉很好..现在自己做项目也用..却发现网上没有一套中文的教程,中文资料都很少..在这里就把自己的研究心得写下来... 首先安装,一步一步装 ...
- 利用merge存储引擎来实现分表
我觉得这种方法比较适合,那些没有事先考虑,而已经出现了得,数据查询慢的情况.这个时候如果要把已有的大数据量表分开比较痛苦,最痛苦的事就是改代码,因为程序里面的sql语句已经写好了,现在一张表要分成几十 ...
- hadoop搭建杂记:Linux下ssh免密码登陆
关于ssh免密码登陆的问题 关于ssh免密码登陆的问题 linux下可以用ssh-keygen来生成公钥/私钥对 ①生成id_rsa和id_rsa.pub公钥/私钥对,自动在~/.ssh下生成文件(亦 ...
- ThinkPHP第二十二天(表单令牌、相对路径、扩展配置载入、$Think获取系统变量、$_SERVER('HTTP_REFERER')前页地址)
1.表单令牌开启配置 'TOKEN_ON'=>true 2.相对路径:在thinkphp中,存在单入口index.php,所以程序中的根目录都是以index.php所在的文件夹为根目录,故用./ ...
- Mysql笔记之 -- 小试MYSQL主从配置
mysql主从配置: 硬件: 两台服务器 1.Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-60-generic-pae i686) 2.Ubuntu 12.04.4 LT ...
- codevs 1028 花店橱窗布置 KM算法
题目链接 n个花, m个花瓶, 每个花放到一个花瓶里会产生一个值w[i][j], 一个花只能放到一个花瓶里, 一个花瓶只能放一个花, 求产生的最大值. 带权二分图模板. #include <io ...