CF622F-The Sum of the k-th Powers【拉格朗日插值】
正题
题目链接:https://www.luogu.com.cn/problem/CF622F
题目大意
给出\(n,k\),求
\]
解题思路
很经典的拉格朗日差值问题
这个东西显然是可以化成一个\(k+1\)次的多项式的,所以我可以直接代\(k+2\)个点插出值来。看到顺眼先把\(n,k\)互换一下。
先上一个要刻在\(DNA\)里的公式
\]
发现这个直接计算是\(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【拉格朗日插值】的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- [题解] 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\ ...
- 解题:CF622F The Sum of the k-th Powers
题面 TJOI2018出CF原题弱化版是不是有点太过分了?对,就是 TJOI2018 教科书般的亵渎 然而我这个问题只会那个题的范围的m^3做法 回忆一下1到n求和是二次的,平方求和公式是三次的,立方 ...
- 「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\)个值带进去然后拉格朗日插值 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- redis支持的数据类型有哪些?
string,list,hash,set,zset 底层实现数据结构 数据结构 String sds struct sdshdr{ //等于 SDS 保存字符串的长度 int len; //记录 bu ...
- liunx系统mysql全量备份和增量备份
前提 在互联网项目中最终还是读数据进行操作,都离不开曾删改查,那么数据是重中之重,数据库的备份就显得格外重要. 但是每次都直接导出整个数据库的sql文件,显然是不现实的.对数据库的性能影响比较 ...
- 单链表(Java--尚硅谷)
基础知识 大体结构和C++的链表差不多 补充之前不知道的:链表分两类,带和不带头结点的链表 现在才知道,Java没有像C/C++那样的指针 首先创建一个LinkList类,然后把链表的各个功能添加进去 ...
- prism 中的 自定义region
参考网址: https://blog.csdn.net/weixin_30872499/article/details/98673059 并不是所有控件都可以被用作Region了吗?我们将Gird块的 ...
- bicabo C#多线程详解(三)
继续上一节的问题:调换两个新创建的线程启动顺序会是什么结果? using System; using System.Threading;namespace Test{ class TestThr ...
- 【mysql】截取查询分析
1. 慢查询日志 1.1 是什么 (1)MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL ...
- redis知识点及常见面试题
redis知识点及常见面试题 参考: https://zm8.sm-tc.cn/?src=l4uLj4zF0NCIiIjRnJGdk5CYjNGckJLQrIqNiZaJnpOWjIvQno2Llpy ...
- Sublime Text3 显示左侧的目录树
file->open folder选择一个文件夹,打开一个新窗口把原来的关掉 View->Sise Bar->Hide Side Bar就可以了
- promise的信任问题&控制反转
//信任问题 //第三方的某个库 function method(cb){ setTimeout(function(){ cb && cb(); //这个库的bug:函数被多调用了一次 ...
- form表单中id与name的区别
以前经常写form表单时,不写id和name,总觉得没有什么用.后来一看后台套完的页面发现,他们都补上name,不知道所以然,就查了一下资料,吓我一跳,要是照我那样写根本不会有数据传到服务器.原来表单 ...