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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int P=1e9+7;
  5. const int N=1e6+10;
  6. void update(int &x,int y){
  7. x+=y; if (x>=P) x-=P;
  8. }
  9. int fpow(int a,int b){
  10. int ret=1; for (;b;b>>=1,a=1ll*a*a%P) if (b&1)ret=1ll*ret*a%P;
  11. return ret;
  12. }
  13. #define normal(x) (((x)%P+P)%P)
  14. int pre[N],suf[N],ifac[N],inv[N];
  15. int main(){
  16. int n,K; scanf("%d%d",&n,&K);
  17. if (K==0){printf("%d\n",n);return 0;} // 特判一下吧,很稳。
  18. ifac[0]=1,inv[1]=1,ifac[1]=1; // 阶乘的定义,注意0!=1
  19. for (int i=2;i<=K+2;i++){
  20. inv[i]=1ll*inv[P%i]*(P-P/i)%P;
  21. ifac[i]=1ll*ifac[i-1]*inv[i]%P;
  22. }
  23. pre[0]=1; // 特殊处理i==0的时候
  24. for (int i=1;i<=K+2;i++) pre[i]=1ll*pre[i-1]*normal(n-i)%P;
  25. suf[K+3]=1; // 同上,特殊对待
  26. for (int i=K+2;i>=0;i--) suf[i]=1ll*suf[i+1]*normal(n-i)%P;
  27. int yi=0,ans=0; for (int i=1;i<=K+2;i++){
  28. update(yi,fpow(i,K));
  29. ll tmp=1ll*yi*ifac[i-1]%P*ifac[K+2-i]%P*pre[i-1]%P*suf[i+1]%P;
  30. if ((K+2-i)%2==0) update(ans,tmp); else update(ans,P-tmp);
  31. }
  32. printf("%d\n",ans);
  33. return 0;
  34. }

[题解] 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. Java中很少用的CopyOnWriteArrayList

    类注释 /** * A thread-safe variant of {@link java.util.ArrayList} in which all mutative * operations ({ ...

  2. 在React中随机生成图形验证码

    各个方法 在输入框中定义一个位置存放图形 完整代码 方便复制粘贴 import React, { Component } from 'react'; import styles from './lef ...

  3. P1066 图像过滤

    P1066 图像过滤 转跳点:

  4. 阿里云安装mysql,初始化密码修改

    阿里云服务器,centos7, rpm包安装MySQL,初始化了个奇葩密码 登陆不上, 修改配置文件/etc/my.cnf,在[mysqld]下面添加一行代码:skip-grant-tables se ...

  5. mysql 添加索引语句

    1.PRIMARY  KEY(主键索引)        mysql>ALTER  TABLE  `table_name`  ADD  PRIMARY  KEY (  `column`  ) 2. ...

  6. LabVIEW面向对象的ActorFramework(2)

    二.为什么要学习面向编程? 面向对象编程,如果将上文推荐的两本书读完后,基本上也就有了答案.从自我产品开发的经验中,理解为可以迅速解决中大型程序需求变化时,在不影响其他程序功能的情况下,能够实现新增功 ...

  7. NO11 SSH故障排查思路和netstat命令

    本章知识相关考试:1.企业场景面试题:Linux系统如何优化?2.企业场景面试题:SSH服务连不上,如何排查?记住回答技巧: 1 ping  2 telnet 客户端ssh工具:SecureCRT,x ...

  8. Mac安装软件提示破损

    安装提示破损 zhong终端输入 sudo spctl --master-disable 就可以顺利打开啦

  9. 2.9 学习总结 之 【Android】体温统计APP

    一.说在前面 昨天 学习了JQ的相关知识 今天 编写体温统计APP 我的工程源码:https://github.com/xiaotian12-call/Take-body-temperature 二. ...

  10. SSM文件上传要点总结

    文件的上传要点: 1.表单方面:enctype="multitype/form-data" 编码方式选择混编码 input 类型采用file 2.实体类一定要进行序列化,也就是im ...