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)\)


好,我们先不看题,来补一些数学。

考虑这样一个序列

\[h_0,h_1,\dots,h_n,\dots
\]

我们定义它的一个差分序列(一阶)

\[\Delta h_0, \Delta h_1, \dots , \Delta h_n, \dots
\]

满足\(\Delta h_n = h_{n+1} - h_n \ (n \ge 0)\),换句话说(一阶)差分序列就是原序列相邻两项的差。

同样的,我们还能在原序列的一阶差分序列上再做一次差分,得到二阶差分序列

\[\Delta(\Delta h_0), \Delta(\Delta h_1),\dots , \Delta(\Delta h_n), \dots
\]

可以把前面的\(\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_0, \Delta^p h_1, \dots , \Delta^p h_n, \dots
\]

其中\(\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 $通项就变为了

\[\begin{aligned}\Delta h_n &= h_{n+1} - h_{n} \\&= (a_p (n+1)^p + a_{p-1} (n+1)^{p-1} + \cdots + a_0 (n+1)^0) - (a_pn^p + a_{p-1}n^{p-1} + \cdots + a_0 n^0)\end{aligned}
\]

观察\(\Delta h_n\)的\(p\)次项:\(a_p(n+1)^p - a_p n^p\),把\((n+1)^p\)用二项式定理展开后

\[\begin{aligned}a_p(n+1)^p - a_p n^p &= a_p(n^p + \binom{p}{1} n^{p-1} + \binom{p}{2}n^{p-2} + \cdots + 1) - a_pn^p\end{aligned}
\]

发现了什么?\(n^p\)和\(n^p\)刚好抵消!即

\[a_p(n+1)^p - a_p n^p = \binom{p}{1} a_pn^{p-1} + \binom{p}{2}a_pn^{p-2} + \cdots + 1
\]

换句话说,每做一次差分,原序列的通项多项式的次数至少会减少\(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)\)的。。。还要加一些优化。

我们有公式

\[f(x) = \sum\limits_{i = 1}^{k+2} y_i \prod\limits_{j \not= i} \frac{x - x_j}{x_i - x_j}
\]

(这里的\(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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 解题:CF622F The Sum of the k-th Powers

    题面 TJOI2018出CF原题弱化版是不是有点太过分了?对,就是 TJOI2018 教科书般的亵渎 然而我这个问题只会那个题的范围的m^3做法 回忆一下1到n求和是二次的,平方求和公式是三次的,立方 ...

  4. 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 ...

  5. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  6. 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 ...

  7. [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 ...

  8. 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...

  9. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

随机推荐

  1. redis5.5官方文档

    https://www.cnblogs.com/zsl-find/articles/11780974.html 博客 https://redis.io/topics/cluster-tutorial ...

  2. 数据结构 c++ 广义表

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  3. linux7下nenux3.14的maven私服搭建和配置使用

    为什么用私服,各种帖子很多,话不多说,直接奉上本博主的搭建过程. 一.环境准备:确保jdk和maven安装完成 二.下载nenux:地址:https://www.sonatype.com/downlo ...

  4. R 创建一个空的数据框

    k = 16 #数据框的行数 z = data.frame( a = numeric(k), b = numeric(k), c = numeric(k), d = numeric(k) )

  5. 实验吧-web-Once More(php ereg()漏洞)!!!

    题目:啊拉?又是php审计.已经想吐了. hint:ereg()函数有漏洞哩:从小老师就说要用科学的方法来算数. 给我们提示:1)ereg()函数漏洞 2)科学计数法 viewsource: < ...

  6. linux安装postgresql数据库

    本文提供数据库安装脚本,有部分需要优化,就是脚本中的方法执行存在前后依赖,但是代码里面没有对上一个执行结果进行判断,如果提供的路径和安装包没有问题,脚本能够正常执行 #!/bin/bash # ins ...

  7. linux后台运行jar

    1. 在linux服务器上运行Jar文件时通常的方法是: $ java -jar test.jar 这种方式当ssh窗口关闭时,程序中止运行,或者是运行时没法切出去执行其他任务 2. 保持程序后台运行 ...

  8. jmeter简单压测、下载文件

    一.jmeter做简单压测(单机) 1.添加需要压测的HTTP请求 2.添加聚合报告 3.设置压测场景 4.查看聚合报告 二.多机同时进行压测 1.在需要连接的电脑上打开jmeter  bin目录下的 ...

  9. 1-VCP 框架

    VMware 硬件兼容性网址: 立即同步时间,修改/etc/ntp.conf 文件,增加一行 tos maxdist 30

  10. 虚拟机安装centos6.5出现Error processing drive:pci-0000:00:10-scsi-0:0:0:0问题

    vmware安装linux系统出现Error processing drive:pci-0000:00:10-scsi-0:0:0:0问题 问题出现原因:我给虚拟机的内存太小了,只给了512M 解决办 ...