The Sum of the k-th Powers()Educational Codeforces Round 7F+拉格朗日插值法)

题目链接

传送门

题面

在这里插入图片描述

题意

给你 n , k n,k n,k,要你求 ∑ i = 1 n i k \sum\limits_{i=1}^{n}i^k i=1nik的值。

思路

根据数学知识或者说题目提示可知 ∑ i = 1 n i k \sum\limits_{i=1}^{n}i^k i=1nik可以被一个 k + 1 k+1 k+1次多项式表示。
由拉格朗日插值法(推荐学习博客)的公式: L ( x ) = l ( x ) ∑ i = 1 k + 2 y i w i x − x i , 其中 l ( x ) = ∏ i = 1 k + 2 ( x − i ) , y i = ∑ j = 1 i j k , w i = ∏ j = 1 , j ̸ = i n 1 x i − x j 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} L(x)=l(x)i=1k+2yixxiwi,其中l(x)=i=1k+2(xi),yi=j=1ijk,wi=j=1,j̸=inxixj1可以得到结果。
由于本题的特殊性,可以将 w i w_i wi进行化简:
w i = ∏ j = 1 , j ̸ = i n 1 x i − x j = ∏ j = 1 , j ̸ = i n 1 i − j = 1 ( i − 1 ) ( i − 2 ) ∗ ⋯ ∗ 1 ∗ ( i − ( i + 1 ) ) … ( i − ( k + 2 ) ) = ( − 1 ) k + 2 − i 1 ( i − 1 ) ! ( k + 2 − i ) ! \begin{aligned} 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} wi=j=1,j̸=inxixj1=j=1,j̸=inij1=(i1)(i2)1(i(i+1))(i(k+2))1=(1)k+2i(i1)!(k+2i)!1
因此我们可以通过 O ( k + 2 ) O(k+2) O(k+2)的复杂度得到 l ( x ) , y i , x − x i l(x),y_i,x-x_i l(x),yi,xxi,然后通过预处理阶乘的逆元我们可以 O ( ( k + 2 ) l o g ( k + 2 ) ) O((k+2)log(k+2)) O((k+2)log(k+2))得到 w i w_i wi,所以总复杂度为在 O ( ( k + 2 ) l o g ( k + 2 ) + ( k + 2 ) ) O((k+2)log(k+2)+(k+2)) 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值