正题

题目链接:https://www.luogu.com.cn/problem/CF622F


题目大意

给出\(n,k\),求

\[\sum_{i=1}^ni^k
\]

解题思路

很经典的拉格朗日差值问题

这个东西显然是可以化成一个\(k+1\)次的多项式的,所以我可以直接代\(k+2\)个点插出值来。看到顺眼先把\(n,k\)互换一下。

先上一个要刻在\(DNA\)里的公式

\[f(k)=\sum_{i=1}^ny_i\prod_{j=1,j\neq i}^n\frac{x_j-k}{x_i-x_j}
\]

发现这个直接计算是\(O(n^2)\)的搞不定。

上面的\(x_j-k\)挺好优化的,分别做一个前后缀积就好了,但是麻烦的是\(x_i-x_j\)。我们可以利用\(x_i\)是连续的这个性质,我们只需要带入\(x_i\in[1,n+2]\)的点即可。

此时\(x_i-x_j\)就变为了两段阶乘分别是\(\prod_{j=1}^{i-1}\frac{1}{i-j}\)和\(\prod_{j=i+1}^j\frac{1}{i-j}\)。预处理逆元前缀和就好了,需要注意的是因为后面那个式子\(i-j\)是负数所以我们需要判断一下如果\(n-i\)是奇数就要取反。

线性筛\(y_i\)的话时间复杂度\(O(n)\),懒得话直接快速幂\(O(n\log k)\)


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll N=1e6+10,P=1e9+7;
ll n,k,ans,inv[N],suf[N],pre[N];
ll power(ll x,ll b){
ll ans=1;
while(b){
if(b&1)ans=ans*x%P;
x=x*x%P;b>>=1;
}
return ans;
}
signed main()
{
scanf("%lld%lld",&k,&n);
// if(k<=n+2){
// for(ll i=1;i<=k;i++)
// ans=(ans+power(i,n))%P;
// printf("%d\n",ans);
// return 0;
// }
inv[1]=1;n+=2;
for(ll i=2;i<=n;i++)
inv[i]=P-(P/i)*inv[P%i]%P;
inv[0]=1;
for(ll i=1;i<=n;i++)
inv[i]=inv[i-1]*inv[i]%P;
ll tmp=1;pre[0]=suf[n+1]=1;
for(ll i=1;i<=n;i++)pre[i]=pre[i-1]*(k-i)%P;
for(ll i=n;i>=1;i--)suf[i]=suf[i+1]*(k-i)%P;
for(ll i=1,p=0;i<=n;i++){
(p+=power(i,n-2))%=P;
ans+=p*pre[i-1]%P*suf[i+1]%P*inv[i-1]%P*(((n-i)&1)?P-inv[n-i]:inv[n-i])%P;
ans=ans%P;
}
printf("%lld\n",(ans+P)%P);
return 0;
}

CF622F-The Sum of the k-th Powers【拉格朗日插值】的更多相关文章

  1. Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值

    The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...

  2. CF 622 F The Sum of the k-th Powers —— 拉格朗日插值

    题目:http://codeforces.com/contest/622/problem/F 设 f(x) = 1^k + 2^k + ... + n^k 则 f(x) - f(x-1) = x^k ...

  3. CF 622F The Sum of the k-th Powers——拉格朗日插值

    题目:http://codeforces.com/problemset/problem/622/F 发现 sigma(i=1~n) i 是一个二次的多项式( (1+n)*n/2 ),sigma(i=1 ...

  4. [题解] 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\ ...

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

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

  6. 「CF622F」The Sum of the k-th Powers「拉格朗日插值」

    题意 求\(\sum_{i=1}^n i^k\),\(n \leq 10^9,k \leq 10^6\) 题解 观察可得答案是一个\(k+1\)次多项式,我们找\(k+2\)个值带进去然后拉格朗日插值 ...

  7. Codeforces D. The Sum of the k-th Powers(拉格朗日插值)

    题目描述: The Sum of the k-th Powers time limit per test 2 seconds memory limit per test 256 megabytes i ...

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

    F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...

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

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

随机推荐

  1. centos8安装mysql8.0

    官网下载rpm地址 https://dev.mysql.com/downloads/repo/yum/ wget下载 wget https://repo.mysql.com//mysql80-comm ...

  2. C# 中await前后执行线程的问题

     悬赏园豆:20 [已解决问题] 浏览: 1763次 解决于 2018-08-15 22:43  今天有点疑惑就写了个测试的代码,发现控制台和Winform中不一样 比如: 控制台: ...Main( ...

  3. git push&pull命令详解

    git pull的作用是从一个仓库或者本地的分支拉取并且整合代码. git pull [<options>] [<repository> [<refspec>-​] ...

  4. ant的javac任务的相关属性配置

    任务和javac命令是相似,它编译两种类型的Java文件1)没有被编译的java文件2)曾经编译过,但是class文件版本和当前对应的java文件版本不匹配的java文件. 1)javac命令支持的参 ...

  5. Linux·命令收藏

    时间:2018-11-20 记录:byzqy 标题:Linux命令大全(手册) 地址:http://man.linuxde.net/ 标题:Linux script命令 -- 终端里的记录器 地址:h ...

  6. 在按照ROS官方步骤操作,同时用Git管理整个过程,git clone的新catkin_ws报错: catkin_package() include dir 'include' does not exist relative to

    在按照ROS官方步骤操作,同时用Git管理整个过程,git clone的新catkin_ws报错如下: CMake Error at /opt/ros/kinetic/share/catkin/cma ...

  7. JDK方法区、元空间区别 & String.intern相关面试题

    一.方法区.永久代.元空间 1.方法区.永久代 方法区也是各个线程共享的内存区域,它用于存储已经被虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据.方法区域又被称为"永久代& ...

  8. 羽夏笔记——Win32(非WinAPI)

    写在前面   本笔记是由本人独自整理出来的,图片来源于网络.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇文章有帮助你 ...

  9. 浅析Is-a,Has-a与like-a

    在面向对象的设计领域里,有很多设计思路,主要有三种:is-a.has-a.like-a. 这三种在java的类.接口.抽象类中很多体现,下面简述一下其定义. 1.Is-a(继承关系) is-a,顾名思 ...

  10. noip模拟46

    A. 数数 排好序从两头贪心即可 B. 数树 首先很容易想到容斥 如果选择的边集的相关点集有点的度数大于 \(1\) 是不合法的 也就是说一定形成若干条长度不一的链 要给这些链上的点安排排列中的数,方 ...