CF622F:The Sum of the k-th Powers

CF622F:The Sum of the k-th Powers

题意:

  • 求\(\sum_{i=1}^ni^k\),结果模\(1e9+7\)。

思路:

  • 拉格朗日插值法。
  • 看大部分题解发现说是\(\sum_{i=1}^ni^k\)是一个\(k+1\)次多项式,可是我实在是看不出来,所以接下来证明一下。
  • 首先看一个等差数列:
    • \(1,2,3,...,n\)。
  • 之后做差分操作:
    • \(2-1,3-2,...,n-(n-1)\).
    • \(1,1,1,...,1\).
  • 对于这样一个数列,我们称之为常数数列。
  • 所以我们现在有等差数列:
    • \(a_1,a_2,...,a_n\).
  • 做差分后
    • \(a_2-a_1,a_3-a_2,...,a_n-a_{n-1}\).
    • \(b_1,...,b_{n-1}\).
  • 我们知道\(b_i=d\),其中\(d\)为公差。
  • 那当然此时的\(\{a\}\)是等差数列,所以\(b_i\)会全部相等,但假如说\(\{a\}\)是一般的数列呢?
  • 同样做差,我们得到\(\{b\}\)序列,称之为数列\(\{a\}\)的阶差数列。
  • 那么再对数列\(\{b\}\)做差分,就可以得到二阶阶差数列。
  • 同样的我们可以有三阶,四阶...。
  • 如果\(\{a\}\)的\(p\)阶差数列是一个非\(0\)的常数数列,那么就称数列\(\{a\}\)为\(p\)阶等差数列。

定理:

  • 数列\(\{a\}\)是一个\(p\)阶等差数列的充要条件是通项\(a_x\)为\(x\)的一个\(p\)次多项式。
简单的证明:
  • 已知一个数列\(\{a\}\)是一个\(p\)阶等差数列,设\(\{a\}\)的通项\(a_x\)是一个关于\(x\)的\(v\)次多项式,即\(f(x)=\sum_{i=0}^vu_i*x^i\)。其中\(u_i\)是\(x^i\)的系数。
  • 做一阶差分可得:
    • \(\Delta f(x)=f(x+1)-f(x)=\sum_{i=0}^vu_i*(x+1)^i-\sum_{i=0}^vu_i*x^i\).
  • 只考虑\(x^v\)。
    • \(u_v*(x+1)^v-u_v*x^v\).
  • 二项式定理展开\((x+1)^v\),因为只考虑\(x^v\),所以\(x^v\)的系数为\(1\)。
    • \(u_v*x^v-u_v*x^v=0\).
  • 所以可知,在一次差分后,\(x^v\)项被消除了,所以我们知道每做一次差分,得到数列的通项的多项式次数会减一
  • 又因为\(\{a\}\)是一个\(p\)阶等差数列,所以数列\(\{a\}\)在做\(p\)次差分后,会得到一个\(0\)次多项式,也就是常数。

回到题目

  • 此时序列\(\{a\}\)为:

    • \(\sum_{i=1}^0i^k,\sum_{i=1}^1i^k,\sum_{i=1}^2i^k,...,\sum_{i=1}^ni^k\).
  • 差分后有:
    • \(\sum_{i=1}^1i^k-\sum_{i=1}^0i^k,\sum_{i=1}^2i^k-\sum_{i=1}^1i^k,...,\sum_{i=1}^ni^k-\sum_{i=1}^{n-1}i^k\).
  • 也就是\(1^k,2^k,...,n^k\)。
  • 这个数列的通项为\(f(x)=x^k\),是一个\(k\)次多项式。
  • 题目要求\(\sum_{i=1}^ni^k\),又知差分序列求前缀和为原序列。
  • 所以原序列为所求。
  • 我们又知每次差分后数列通项的多项式次数会减一,差分序列的次数为\(k\),所以数列\(\{a\}\)是一个关于\(n\)的\(k+1\)次多项式。
  • 所以我们只需要插值搞出这个\(k+1\)次多项式即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 10;
ll pre[maxn], suf[maxn], fac[maxn];
ll n, k, ans;
ll qmi(ll a, ll b)
{
    ll res = 1; res %= mod;
    while(b)
    {
        if(b&1) res = res*a%mod;
        a = (a*a)%mod;
        b >>= 1;
    } return res % mod;
}

void init()
{
    pre[0] = suf[k+3] = fac[0] = 1;
    for(int i = 1; i <= k+2; i++)
        pre[i] = pre[i-1]*(n-i) % mod;
    for(int i = k+2; i >= 1; i--)
        suf[i] = suf[i+1]*(n-i) % mod;
    for(int i = 1; i <= k+2; i++)
        fac[i] = fac[i-1]*i % mod;
}

void Lagrange()
{
    ll y = 0;
    for(int i = 1; i <= k+2; i++)
    {
        y = (y + qmi(i, k)) % mod;
        ll a = pre[i-1]*suf[i+1] % mod;
        ll b = fac[i-1]*((k-i)&1 ? -1ll : 1ll)*fac[k+2-i] % mod;
        ans = (ans + y*a%mod * qmi(b, mod-2)) % mod;
    } cout << (ans + mod) % mod << endl;
}

int main()
{
    scanf("%lld%lld", &n, &k);
    init(); Lagrange();
    return 0;
}

原文地址:https://www.cnblogs.com/zxytxdy/p/12147208.html

时间: 2024-08-30 18:35:30

CF622F:The Sum of the k-th Powers的相关文章

[题解] 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)\) 好,我们先不看题,来补一些数学. 考虑这样一个序列 \[ h_0,h_1,\dots,h_n,\dots \] 我们定义它的一个差分序列(一阶) \[ \Delta h_0, \Delta h_1, \dots ,

leetcode 862 shorest subarray with sum at least K

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值,然后扫一遍sum数组. 1.首先这里不需要最大,因为刚好够k就好了 2.这里需要距离最短.就是数组的长度最短. 这里的思路也一样,不过保存很多个min值,就是用一个队列,保存前缀的min值,不需要最min,只不过有更小的就更好. 也就是如果sum数组的值是: ..... 60, 40.....Y..

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 is no non-empty subarray with sum at least K, return -1. Example 1: Input: A = [1], K = 1 Output: 1 Example 2: Input: A = [1,2], K = 4 Output: -1 Exa

[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 is no non-empty subarray with sum at least K, return -1. Example 1: Input: A = [1], K = 1 Output: 1 Example 2: Input: A = [1,2], K = 4 Output: -1 Exa

lc 862. Shortest Subarray with Sum at Least K

断网导致原来写的那么多答案全没了,博客园能不能实时保存草稿,醉. https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 给一个数组a,找和大于k的所有子数组中的最短的那个. 最近二分有点上头,因为的确很强大,两个考虑二分的情况(其实需要刻意去想想能不能使用二分才会想到,这会是个好习惯): 1.最优问题变判断问题.比如:行递增列递增的矩阵中找某个元素->行递增列递增的矩阵中找第k大元素 2.有相应的简单孪生问题.比

LeetCode 1099. Two Sum Less Than K

原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, jexist satisfying this equation, return -1. Example

[LC] 1099. Two Sum Less Than K

Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, j exist satisfying this equation, return -1. Example 1: Input: A = [34,23,1,24,75,33,54,8], K = 60 Output: 58 Expl

2018年第四阶段组队训练赛第三场(BAPC2017 Preliminaries)

D.Disastrous Doubling 题目描述 A scientist, E. Collie, is going to do some experiments with bacteria.Right now, she has one bacterium. She already knows that this species of bacteria doubles itself every hour. Hence, after one hour there will be 2 bacter

Envious Exponents

问题 E: Envious Exponents 时间限制: 1 Sec  内存限制: 128 MB提交: 321  解决: 53[提交] [状态] [讨论版] [命题人:] 题目描述 Alice and Bob have an integer N. Alice and Bob are not happy with their integer. Last night they went to a cocktail party and found that another couple had th