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

题目链接

传送门

题面

题意

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

思路

根据数学知识或者说题目提示可知\(\sum\limits_{i=1}^{n}i^k\)可以被一个\(k+1\)次多项式表示。
由拉格朗日插值法(推荐学习博客)的公式:\(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}\)可以得到结果。
由于本题的特殊性,可以将\(w_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}
\]
因此我们可以通过\(O(k+2)\)的复杂度得到\(l(x),y_i,x-x_i\),然后通过预处理阶乘的逆元我们可以\(O((k+2)log(k+2))\)得到\(w_i\),所以总复杂度为在\(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;
}

原文地址:https://www.cnblogs.com/Dillonh/p/11183676.html

时间: 2024-08-26 08:47:51

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

CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforces Round 26 题意: 设定数组 y = f(x) 使得 y[i] = sum(x[j]) (0 <= j < i) 求初始数组 A0 经过多少次 f(x) 后 会有一个元素 大于 k 分析: 考虑 A0 = {1, 0, 0, 0} A1 = {1, 1, 1, 1} -> {C(

Educational Codeforces Round 22 E. Army Creation(主席树)

题目链接:Educational Codeforces Round 22 E. Army Creation 题意: 给你n个数和一个数k,然后有q个询问. 每个询问 有一个区间[l,r],问你这个区间内在满足每一种数不超过k的情况下,最大能选多少个数出来. 强制在线. 题解: 一看就要用到主席树,和主席数求区间内有多少不同的数的个数处理方法相同. 依次将每个数插入,当这个数出现的个数等于k了,就把最前面的那个数删掉. 然后询问就访问root[r]就行了. 第一次写完数据结构没有调试一遍过样例,一

Educational Codeforces Round 21 D. Array Division

题目链接:Educational Codeforces Round 21 D. Array Division 题意: 给你n个数,现在你可以改变1<=个数的位置,然后问你是否存在有一个k,使得sum(a[i])(1<=i<=k)==sum(a[j])(k+1<=j<=n) 题解: 分析: 如果需要将一个数移动,无非就是将这个数从第一部分移到第二部分,或者从第二部分移到第一部分. 所以,我们只需要开两个map来记录一下两部分有哪些数. 当两部分的差值/2等于其中一部分的一个数时

Educational Codeforces Round 21 F. Card Game(网络流之最大点权独立集)

题目链接:Educational Codeforces Round 21 F. Card Game 题意: 有n个卡片,每个卡片有三个值:p,c,l; 现在让你找一个最小的L,使得满足选出来的卡片l<=L,并且所有卡片的p的和不小于k. 选择卡片时有限制,任意两张卡片的c之和不能为质数. 题解: 和hdu 1565 方格取数(2)一样,都是求最大点权独立集. 不难看出来,这题再多一个二分. 注意的是在构造二部图的时候,按照c值的奇偶性构造. 当c==1时要单独处理,因为如果有多个c==1的卡片,

Educational Codeforces Round 35 E. Stack Sorting 模拟

Educational Codeforces Round 35 E. Stack Sorting 题意:长度为 n 的序列 a[] ,a[] 里的数是 1~n,一个空栈 s,一个空序列 b[].两个操作:把 a[] 的第一个数放到 s 里: 或者把 s 的栈顶元素加到 b[] 的末尾. 如果你能通过这两个操作把 a[] 的数最后都放入 b[] 中,且 b[] 是升序的,那就可说 a[] 是 stack-sortable . 现在给出 a[] 的前 k 个数,要你确定是否有满足 stack-sor

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code

Educational Codeforces Round 69 (Rated for Div. 2) E. Culture Code 题目链接 题意: 给出\(n\)个俄罗斯套娃,每个套娃都有一个\(in_i,out_i\),并满足\(out_i>in_i\).定义套娃\(i\)能套在套娃\(j\)里面,当且仅当\(out_i\leq in_j\). 定义极大套娃组:当且仅当不能有另外一个套娃套在它们身上. 定义套娃组额外空间为\(in_1+(in_2-out_1)+\cdots +(in_k-

Educational Codeforces Round 73 (Rated for Div. 2)

比赛链接:Educational Codeforces Round 73 (Rated for Div. 2) 官方题解:Educational Codeforces Round 73 Editorial A. 2048 Game 题意 如果一个只包含 \(2\) 的幂次的集合,问能否从中选择一些数使得和为 \(2048\). 思路 不断合并直到凑到 \(2048\). 代码 #include <bits/stdc++.h> using namespace std; int main() {