[题解] CF622F The Sum of the k-th Powers
CF622F The Sum of the k-th Powers
题意:给\(n\)和\(k\),让你求\(\sum\limits_{i = 1} ^ n i^k \ mod \ 10^9 + 7\)。\((1 \le n \le 10^9,0 \le k \le 10^6)\)
好,我们先不看题,来补一些数学。
考虑这样一个序列
\]
我们定义它的一个差分序列(一阶)
\]
满足\(\Delta h_n = h_{n+1} - h_n \ (n \ge 0)\),换句话说(一阶)差分序列就是原序列相邻两项的差。
同样的,我们还能在原序列的一阶差分序列上再做一次差分,得到二阶差分序列:
\]
可以把前面的\(\Delta\)写成\(\Delta ^2\),就是\(\Delta^2 h_0, \Delta^2 h_1,\dots , \Delta^2 h_n, \dots\)
根据定义,有\(\Delta^2 h_n = \Delta(\Delta h_n) = \Delta h_{n+1} - \Delta h_n\)
推广一下,我们还能得到原序列的\(p\)阶差分序列
\]
其中\(\Delta^p h_n = \Delta(\Delta^{p-1} h_n) = \Delta^{p-1} h_{n+1} - \Delta^{p-1} h_n\)。
特别的\(p = 0\)时,\(\Delta^0 h_n = h_n\)。
其中,\(p\)阶差分在第\(p\)行上(从第\(0\)行开始)。
考虑一个序列\(h\),通项是一个关于\(n\)的\(p\)次多项式,即\(h_n = a_{p}n^{p} + a_{p-1}n^{p-1} + \cdots + a_0n^0\)。
那么这个序列的一阶差分$\Delta h $通项就变为了
\]
观察\(\Delta h_n\)的\(p\)次项:\(a_p(n+1)^p - a_p n^p\),把\((n+1)^p\)用二项式定理展开后
\]
发现了什么?\(n^p\)和\(n^p\)刚好抵消!即
\]
换句话说,每做一次差分,原序列的通项多项式的次数至少会减少\(1\)。
好了来看题吧。
题目让我们求\(\sum\limits_{i = 1} ^ n i^k\),我们不妨设\(h_n = \sum\limits_{i = 1} ^ n i^k\),那把这样的序列\(h\),做一次差分得到的\(\Delta h\)长啥样呢?
根据定义\(\Delta h_n = h_{n+1} - h_{n} = \sum\limits_{i = 1}^{n+1} i^k - \sum\limits_{i = 1}^{n} i^k = (n+1)^k\)。显然他的差分序列的通项是一个\(k\)次多项式,那么\(h_n\)就是一个\(k+1\)次多项式。
我们不设\(h\)序列的通项是一个\(k+1\)次多项式\(f(x)\),满足\(f(n) = \sum\limits_{i = 1}^{n} i^{k}\),现在我们要求\(f(n)\)(这里是题目的\(n\),上面懒得改了。。。)
观察到\(k\)只有\(10^6\),所以我们可以算出\(f(1), f(2), f(3), \cdots, f(k+2)\),然后拉格朗日差值求出\(f(n)\)就可以了。
但拉格朗日的复杂度是\(O(k^2)\)的。。。还要加一些优化。
我们有公式
\]
(这里的\(x_i = i, y_i = f(x_i)\))。
考虑化简后面的乘法。
我们先看分母。\(\prod\limits_{j \not= i} \frac{1}{x_i - x_j}\)这个显然是\((-1)^{k+2-i} \frac{1}{(i-1)!(k+2-i)!}\),(后半段是\(-1,-2,...,-(k+2-i)\)的积,直接把\(-1\)提到前面就好了)题目说了对\(10^9 + 7\)取模,预处理阶乘逆元就好了。
分子\(\prod\limits_{j \not= i} x - x_j\)。写开来后就是\((x-1)(x-2)(x-3)\cdots(x-(i-1)) \cdot (x-(k+2))(x-(k+1))\cdots(x-(i+1))\)。
维护两个数组\(pre[i] = (x-1)(x-2)(x-3)\cdots(x-i),suf[i]=(x-(k+2))(x-(k+1))\cdots(x-i)\)就好了。
前面\(y_i\)的话可以在枚举\(i\)的时候带着算,用快速幂。
总复杂度\(O(k \ log \ k)\)(快速幂还要\(log\)啊。。。)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P=1e9+7;
const int N=1e6+10;
void update(int &x,int y){
x+=y; if (x>=P) x-=P;
}
int fpow(int a,int b){
int ret=1; for (;b;b>>=1,a=1ll*a*a%P) if (b&1)ret=1ll*ret*a%P;
return ret;
}
#define normal(x) (((x)%P+P)%P)
int pre[N],suf[N],ifac[N],inv[N];
int main(){
int n,K; scanf("%d%d",&n,&K);
if (K==0){printf("%d\n",n);return 0;} // 特判一下吧,很稳。
ifac[0]=1,inv[1]=1,ifac[1]=1; // 阶乘的定义,注意0!=1
for (int i=2;i<=K+2;i++){
inv[i]=1ll*inv[P%i]*(P-P/i)%P;
ifac[i]=1ll*ifac[i-1]*inv[i]%P;
}
pre[0]=1; // 特殊处理i==0的时候
for (int i=1;i<=K+2;i++) pre[i]=1ll*pre[i-1]*normal(n-i)%P;
suf[K+3]=1; // 同上,特殊对待
for (int i=K+2;i>=0;i--) suf[i]=1ll*suf[i+1]*normal(n-i)%P;
int yi=0,ans=0; for (int i=1;i<=K+2;i++){
update(yi,fpow(i,K));
ll tmp=1ll*yi*ifac[i-1]%P*ifac[K+2-i]%P*pre[i-1]%P*suf[i+1]%P;
if ((K+2-i)%2==0) update(ans,tmp); else update(ans,P-tmp);
}
printf("%d\n",ans);
return 0;
}
[题解] CF622F The Sum of the k-th Powers的更多相关文章
- [CF622F]The Sum of the k-th Powers
题目大意:给你$n,k(n\leqslant10^9,k\leqslant10^6)$,求:$$\sum\limits_{i=1}^ni^k\pmod{10^9+7}$$ 题解:可以猜测是一个$k+1 ...
- [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 ...
- 解题:CF622F The Sum of the k-th Powers
题面 TJOI2018出CF原题弱化版是不是有点太过分了?对,就是 TJOI2018 教科书般的亵渎 然而我这个问题只会那个题的范围的m^3做法 回忆一下1到n求和是二次的,平方求和公式是三次的,立方 ...
- LeetCode862. 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 ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- 862. 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 ...
- [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
随机推荐
- zookeeper加Kafka集群配置
官方 https://zookeeper.apache.org/doc/r3.5.6/zookeeperStarted.html#sc_Prerequisites https://www.cnblog ...
- 一 Hibernate入门
Hibernate环境搭建 Hibernate的API Hibernate的CRUD EE三层结构: web层 业务逻辑层 持久层 jdbc,DBUTils,Hibernate Hib ...
- Linux下四种安装软件方式
1.yum源安装 可以解决依赖关系,但不确定安装的位置 2.rpm 基础安装 要自己解决依赖问题 rpm -ivh 安装 rpm -uvh 更新 rpm -e --nodeps 卸载 取消依赖 ...
- js 转换时间戳为时间格式并且按指定格式输出
/** * 时间戳转换为日期 */ function convertTimestamp(timestamp){ // 时间戳转换为日期 var d = new Date(timestamp); // ...
- 图片索引 lire
1:定义 LIRE( Lucene Image Retrieval)相似图像索引和搜索机制 2:资料来源 LIRE官网:http://www.semanticmetadata.net/lir ...
- 2-10 就业课(2.0)-oozie:11、hadoop的federation(联邦机制,了解一下)
==================================================== Hadoop Federation 背景概述 单NameNode的架构使得HDFS在集群扩展性 ...
- 使用Spring Data JPA的Spring Boot
本文教你开始使用Spring Data JPA.来自优锐课JAVA架构专业讲师精心整理. 欢迎使用带有Spring Data JPA的Spring Boot教程!在本教程中,我们将看到Spring D ...
- android studio 入门坑
安装 android studio,碰到下面这个图片,直接跳过. 安装时候,选择自定义设置,里面可以配置 sdk 的存放位置. 新建工程后,gradle sync 比较慢,可以 修改工程中的 buil ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-cog
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- LeetCode1029 两地调度(贪心+java自定义排序回顾)
题目: 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵 ...