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}i^k\)可以被一个\(k+1\)次多项式表示。
由拉格朗日插值法(推荐学习博客)的公式:\(L(x)=l(x)\sum\limits_{i=1}^{k+2}y_i\frac{w_i}{x-x_i},\text{其中}l(x)=\prod\limits_{i=1}^{k+2}(x-i),y_i=\sum\limits_{j=1}^{i}j^k,w_i=\prod\limits_{j=1,j\not= i}^{n}\frac{1}{x_i-x_j}\)可以得到结果。
由于本题的特殊性,可以将\(w_i\)进行化简:
w_i&=\prod\limits_{j=1,j\not= i}^{n}\frac{1}{x_i-x_j}&\\
&=\prod\limits_{j=1,j\not= i}^{n}\frac{1}{i-j}&\\
&=\frac{1}{(i-1)(i-2)*\dots*1*(i-(i+1))\dots(i-(k+2))}&\\
&=(-1)^{k+2-i}\frac{1}{(i-1)!(k+2-i)!}&
\end{aligned}
\]
因此我们可以通过\(O(k+2)\)的复杂度得到\(l(x),y_i,x-x_i\),然后通过预处理阶乘的逆元我们可以\(O((k+2)log(k+2))\)得到\(w_i\),所以总复杂度为在\(O((k+2)log(k+2)+(k+2))\)左右。
代码实现如下
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int n, k, pp;
int A[maxn], y[maxn], inv[maxn], w[maxn];
int qpow(int x, int n) {
int res = 1;
while(n) {
if(n & 1) res = 1LL * res * x % mod;
x = 1LL * x * x % mod;
n >>= 1;
}
return res;
}
void init() {
A[0] = pp = 1;
for(int i = 1; i <= min(n, k + 2); ++i) {
A[i] = 1LL * A[i-1] * i % mod;
inv[i] = qpow(n - i, mod - 2);
pp = (1LL * pp * (n - i) % mod + mod) % mod;
y[i] = (y[i-1] + qpow(i, k)) % mod;
}
for(int i = 1; i <= min(n, k + 2); ++i) {
w[i] = 1LL * A[i-1] * A[k+2-i] % mod;
if((k + 2 - i) & 1) w[i] = mod - w[i];
w[i] = qpow(w[i], mod - 2);
}
}
int main() {
scanf("%d%d", &n, &k);
init();
if(n <= k + 2) return printf("%d\n", y[n]) * 0;
int ans = 0;
for(int i = 1; i <= (k + 2); ++i) {
ans = (ans + 1LL * pp * y[i] % mod * w[i] % mod * inv[i] % mod) % mod;
}
printf("%d\n", ans);
return 0;
}
The Sum of the k-th Powers(Educational Codeforces Round 7F+拉格朗日插值法)的更多相关文章
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 5
616A - Comparing Two Long Integers 20171121 直接暴力莽就好了...没什么好说的 #include<stdlib.h> #include&l ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 26
Educational Codeforces Round 26 困到不行的场,等着中午显示器到了就可以美滋滋了 A. Text Volume time limit per test 1 second ...
- Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code
Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code 题目链接 题意: 给出\(n\)个俄罗斯套娃,每个套娃都有一个\( ...
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...
随机推荐
- istio1.0安装
1. istio1.0安装 创建 istio 目录 [root@centos-110 ~]# mkdir istio [root@centos-110 ~]# cd istio 1.1 获取安装包 链 ...
- Python的网页解析库-PyQuery
PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...
- [Go] 环境变量,模块化与基础语法
[环境变量] 安装完 go 之后,设置必要环境变量: export GOPATH=/home/wc/go-lab export GO111MODULE=on export GOPROXY=https: ...
- git中配置的.gitignore不生效的解决办法
通常我们希望放进仓库的代码保持纯净,即不要包含项目开发工具生成的文件,或者项目编译后的临时文件.但是,当我们使用git status查看工作区状态的时候,总会提示一些文件未被track.于是,我们想让 ...
- day12——生成器、推导式、简单内置函数
day12 生成器 迭代器:python中内置的一种节省空间的工具 生成器的本质就是一个迭代器 迭代器和生成器的区别:一个是pyhton自带的,一个是程序员自己写的 写一个生成器 基于函数 在函数中将 ...
- springboot2.x 使用redis (入门)
在使用之前先简单介绍一下,redis和mongoDB这两个nosql的区别以及使用场景. 1. redis redis是一个分布式缓存.高性能的key-value数据库.支持存储的value类型包括s ...
- powerful number求积性函数前缀和
算法原理 本文参考了 zzq's blog . \(\text{powerful number}\) 的定义是每个质因子次数都 \(\ge 2\) 的数,有个结论是 \(\ge n\) 的 \(\te ...
- 开启Telnet服务
在Win7系统中安装和启动Telnet服务非常简单:依次点击“开始”→“控制面板”→“程序”,“在程序和功能”找到并点击“打开或关闭Windows功能”进入Windows 功能设置对话框.找到并勾选“ ...
- java之mybatis之配置文件讲解
1.核心配置文件 <configuration> <!-- 它们都是外部化,可替代的属性.可以配置在一个典型的Java 属性文件中,或者通过 properties 元素的子元素进行配 ...
- BFC 到底是什么?
MDN 对 BFC 的描述: 块格式化上下文(Block Formatting Context,BFC) 是Web页面的可视化CSS渲染的一部分,是布局过程中生成块级盒子的区域,也是浮动元素与其他元素 ...