Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )
题意 : 就是让你求个自然数幂和、最高次可达 1e6 、求和上限是 1e9
分析 :
题目给出了最高次 k = 1、2、3 时候的自然数幂和求和公式
可以发现求和公式的最高次都是 k+1
那么大胆猜测幂为 k 的自然数幂和肯定可以由一个最高次为 k+1 的多项式表示
不会证明,事实也的确如此
此时就变成了一个拉格朗日插值的模板题了
只要先算出 k+2 个值、然后就能确定最高次为 k+1 的多项式
套模板求值即可
当然自然数幂和不止这一种做法、例如伯努利数、斯特林数等
详细可参考 ==> Click here
#include<bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define scs(i) scanf("%s", i)
#define sci(i) scanf("%d", &i)
#define scd(i) scanf("%lf", &i)
#define scIl(i) scanf("%I64d", &i)
#define scii(i, j) scanf("%d %d", &i, &j)
#define scdd(i, j) scanf("%lf %lf", &i, &j)
#define scIll(i, j) scanf("%I64d %I64d", &i, &j)
#define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
#define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
#define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
#define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
#define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
#define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define lowbit(i) (i & (-i))
#define mem(i, j) memset(i, j, sizeof(i))
#define fir first
#define sec second
#define VI vector<int>
#define ins(i) insert(i)
#define pb(i) push_back(i)
#define pii pair<int, int>
#define VL vector<long long>
#define mk(i, j) make_pair(i, j)
#define all(i) i.begin(), i.end()
#define pll pair<long long, long long>
#define _TIME 0
#define _INPUT 0
#define _OUTPUT 0
clock_t START, END;
void __stTIME();
void __enTIME();
void __IOPUT();
using namespace std;
;
;
LL pow_mod(LL a, LL b)
{
a %= mod;
LL ret = ;
while(b){
) ret = (ret * a) % mod;
a = ( a * a ) % mod;
b >>= ;
}
return ret;
}
namespace polysum
{
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
;
LL a[D],f[D],g[D],p[D],p1[D],p2[D],b[D],h[D][],C[D];
LL powmod(LL a,LL b){LL res=;a%=mod;assert(b>=);){)res=res*a%mod;a=a*a%mod;}return res;}
LL calcn(int d,LL *a,LL n) { // a[0].. a[d] a[n]
if (n<=d) return a[n];
p1[]=p2[]=;
rep(i,,d+) {
LL t=(n-i+mod)%mod;
p1[i+]=p1[i]*t%mod;
}
rep(i,,d+) {
LL t=(n-d+i+mod)%mod;
p2[i+]=p2[i]*t%mod;
}
LL ans=;
rep(i,,d+) {
LL t=g[i]*g[d-i]%mod*p1[i]%mod*p2[d-i]%mod*a[i]%mod;
) ans=(ans-t+mod)%mod;
else ans=(ans+t)%mod;
}
return ans;
}
void init(int M) {
f[]=f[]=g[]=g[]=;
rep(i,,M+) f[i]=f[i-]*i%mod;
g[M+]=powmod(f[M+],mod-);
per(i,,M+) g[i]=g[i+]*(i+)%mod;
}
LL polysum(LL m,LL *a,LL n) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]
;i<=m;i++) b[i]=a[i];
b[m+]=calcn(m,b,m+);
rep(i,,m+) b[i]=(b[i-]+b[i])%mod;
,b,n-);
}
LL qpolysum(LL R,LL n,LL *a,LL m) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]*R^i
) return polysum(n,a,m);
a[m+]=calcn(m,a,m+);
LL r=powmod(R,mod-),p3=,p4=,c,ans;
h[][]=;h[][]=;
rep(i,,m+) {
h[i][]=(h[i-][]+a[i-])*r%mod;
h[i][]=h[i-][]*r%mod;
}
rep(i,,m+) {
LL t=g[i]*g[m+-i]%mod;
) p3=((p3-h[i][]*t)%mod+mod)%mod,p4=((p4-h[i][]*t)%mod+mod)%mod;
]*t)%mod,p4=(p4+h[i][]*t)%mod;
}
c=powmod(p4,mod-)*(mod-p3)%mod;
rep(i,,m+) h[i][]=(h[i][]+h[i][]*c)%mod;
rep(i,,m+) C[i]=h[i][];
ans=(calcn(m,C,n)*powmod(R,n)-c)%mod;
) ans+=mod;
return ans;
}
}
LL arr[maxn];
int main(void){__stTIME();__IOPUT();
int n, k;
scii(n, k);
) return !printf("%d\n", n);
polysum::init(k+);
; i<=k+; i++)
arr[i] = pow_mod((LL)i, (LL)k);
LL res = polysum::polysum(k+, arr, n+) - arr[];
printf("%I64d\n", res);
__enTIME();;}
void __stTIME()
{
#if _TIME
START = clock();
#endif
}
void __enTIME()
{
#if _TIME
END = clock();
cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
#endif
}
void __IOPUT()
{
#if _INPUT
freopen("in.txt", "r", stdin);
#endif
#if _OUTPUT
freopen("out.txt", "w", stdout);
#endif
}
Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )的更多相关文章
- codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法
题目链接 求sigma(i : 1 to n)i^k. 为了做这个题这两天真是补了不少数论, 之前连乘法逆元都不知道... 关于拉格朗日插值法, 我是看的这里http://www.guokr.com/ ...
- 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 ...
- The Sum of the k-th Powers(Educational Codeforces Round 7F+拉格朗日插值法)
题目链接 传送门 题面 题意 给你\(n,k\),要你求\(\sum\limits_{i=1}^{n}i^k\)的值. 思路 根据数学知识或者说题目提示可知\(\sum\limits_{i=1}^{n ...
- 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 拉格朗日插值法
F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...
- 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 ...
- [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 ...
随机推荐
- mybatis批量更新update-设置多个字段值allowMultiQueries=true
mybatis由于简单易用性得到大家的认可和使用 但是在批量更新操作中,网上介绍的貌似不全,正好今天做个记录,大家一起进步 在实际项目开发过程中,常有这样的需求:根据ids更新表的某一个字段值,这时的 ...
- 【基本优化实践】【1.1】IO优化——把文件迁移到不同物理磁盘
[1]概念 把不同数据文件移动到不同的物理磁盘,无疑是一个提高IO的有效办法 在资源可以的情况下,尽量把 temp .数据库的主数据文件(mdf).数据库的从数据数据(ndf).数据库的事务日志文件( ...
- MySQL Explain命令详解--表的读取顺序,数据读取操作的类型等
表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度(key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的) 不损失精确 ...
- JS中的继承(下)
JS中的继承(下) 在上一篇 JS中的继承(上) 我们介绍了3种比较常用的js继承方法,如果你没看过,那么建议你先看一下,因为接下来要写的内容, 是建立在此基础上的.另外本文作为我个人的读书笔记,才疏 ...
- Zabbix 系统概述与部署
Zabbix是一个非常强大的监控系统,是企业级的软件,来监控IT基础设施的可用性和性能.它是一个能够快速搭建起来的开源的监控系统,Zabbix能监视各种网络参数,保证服务器系统的安全运营,并提供灵活的 ...
- C# 面向对象8 值类型和引用类型
值类型和引用类型 概念 示意图: 1.值类型,在栈中开辟一块空间,存储 2.引用类型,在堆中开辟一块空间,存储数据,然在栈中开辟一块空间存储堆中的数据的地址
- Linux小知识:sudo su和su的区别
Linux小知识:sudo su和su的区别 本文是学习笔记,视频地址:https://www.bilibili.com/video/av62836363 su是申请切换root用户,需要申请root ...
- 教你用python爬取网站美女图(附代码及教程)
我前几篇文章都是说一些python爬虫库的用法,还没有说怎样利用好这些知识玩一些好玩的东西.那我今天带大家玩好玩又刺激的,嘻嘻!对了,requests库和正则表达式很重要的,一定要学会!一定要学会!! ...
- reduce一些方法对数组进行的处理
reduce方法我之前都整理了知识点,不懂的可以看一下我之前的知识点,这次我们是整理了一些关于用reduce方法进行的一些对于数组的处理 1. reduce()求数组项之和 var arr = [3, ...
- spring 在web.xml 里面如何使用多个xml配置文件
1, 在web.xml中定义 contextConfigLocation参数.spring会使用这个参数加载.所有逗号分割的xml.如果没有这个参数,spring默认加载web-inf/applica ...