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 ...
随机推荐
- Storm消费Kafka提交集群运行
1.创建拓扑,配置KafkaSpout.Bolt KafkaTopologyBasic.java: package org.mort.storm.kafka; import org.apache.ka ...
- 设计模式:备忘录模式(Memento)
个人比较喜欢玩单机游戏,什么仙剑.古剑.鬼泣.使命召唤.三国无双等等一系列的游戏我都玩过(现在期待凡人修仙传),对于这些游戏除了剧情好.场面大.爽快之外,还可以随时存档,等到下次想玩了又可以从刚开始的 ...
- 使用mvn archetype:generate快速建立Maven项目目录结构
1.mvn archetype:generate 按照提示进行选择,默认选的话可以直接按回车键 2.mvn archetype:generate -DgroupId=组织名,公司网址的反写+项目名 ...
- [Bzoj1001][BeiJing2006]狼抓兔子(网络流/对偶图)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 看到大佬们都是对偶图过的,写了个最大流水过去了QAQ,网络流的无向图直接建双向边( ...
- nginx+gunicorn/uwsgi+python web 的前世今生
我们在部署 flask.django 等 python web 框架时,网上最多的教程就是 nginx+gunicorn/uwsgi 的部署方式,那为什么要这么部署呢,本文就来系统地解释这个问题. 必 ...
- 牛客 545C 出题人的数组 (贪心)
出题人有两个数组A,B,请你把两个数组归并起来使得$cost=\sum i c_i$最小. 归并要求原数组的数的顺序在新数组中不改变. 贪心水题 对于一段序列$A_i,A_{i+1},...,A_r$ ...
- win10上面快捷方式如何放到桌面上
1.win+I 快捷方式调出windows设置 ,点击个性化. 2.进入个性化设置界面,点击主题 3.进入主题设置后,点击右边的相关的设置下的桌面图标设置 4.在弹出的窗口中,将我们希望看到 ...
- 在set中放入自定义类型
这件事情的起因是在学习背包问题时突然想到了一种算法,分析了一下应该是n^2logn复杂度的,当然比dp慢.但是既然想到了就实现了下: #include<bits/stdc++.h> usi ...
- 对ArrayList中的Person对象按照先年龄从大到小,相同年龄的再按照姓名(姓名是英文的)的字母顺序进行排序.
ListDemo2.java ----------------- package com.fs.test; import java.util.ArrayList; import java.util.C ...
- Django框架——基础之模型系统(ORM的介绍和字段及字段参数)
1.ORM简介 1.1 ORM的概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM ...