【CF622F】The Sum of the k-th Powers (拉格朗日插值法)
来源:互联网 发布:ubuntu安装tftp服务器 编辑:程序博客网 时间:2024/05/22 16:44
题目链接:传送门
题意:求
题解:
找规律可以发现前n项k次幂的和一定能用一个k+1次多项式表示出来,所以可以暴力地求出前k+2所对应的值,再用拉格朗日插值法求解即可
其中
令
预处理出阶乘,逆元,p数组即可,复杂度
//by sdfzchy#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int inf=(1<<30),N=1000100,mod=1e9+7;LL n,k,fac[N],inv[N],c,p[N];inline int in(){ char ch=getchar(); int f=1,tmp=0; while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();} return tmp*f;}LL ksm(LL a,LL b){ LL ret=1; while(b) { if(b&1) ret=ret*a%mod; a=a*a%mod; b>>=1; } return ret;}int main(){ scanf("%lld%lld",&n,&k); for(int i=1;i<=k+2;i++) p[i]=(p[i-1]+ksm(i,k))%mod; if(n<=k+2) {printf("%lld",p[n]);return 0;} fac[0]=inv[0]=c=1; for(int i=1;i<=k+2;i++) fac[i]=fac[i-1]*i%mod; inv[k+2]=ksm(fac[k+2],mod-2); for(int i=k+1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod; for(int i=1;i<=k+2;i++) c=c*(n-i)%mod; LL ans=0; for(int i=1;i<=k+2;i++) ans+=p[i]*c%mod*ksm(n-i,mod-2)%mod*inv[i-1]%mod*inv[k+2-i]%mod*((k+2-i)%2==0?1:-1)%mod;ans=ans%mod; printf("%lld",(ans+mod)%mod); return 0;}
阅读全文
0 0
- 【CF622F】The Sum of the k-th Powers (拉格朗日插值法)
- codeforces 622F. The Sum of the k-th Powers (拉格朗日插值法)★
- cf#ECR7 - F - The Sum of the k-th Powers -拉格朗日插值/逆元/数学
- Codeforces Educational Codeforces Round 7 - F. The Sum of the k-th Powers
- The K-th largest number(小根堆)
- Find the k-th minimal element of an unsorted array
- Sum of powers UVA
- give two sorted array, find the k-th smallest element of union of A and B
- poj2366_Sacrament of the sum
- Sacrament of the sum
- the Sum of Cube
- The Sum of Cube
- the Sum of Cube
- the Sum of Cube
- the Sum of Cube
- sdibt 3146 The K'th number(康托展开)
- The k-th Largest (并查集+线段树)
- TJU 2870 The K-th City
- MySQL
- 索引及视图
- 提起键盘就是干
- linux下重启Apache服务
- 导入web项目报no projects are found to import eclipse解决方式
- 【CF622F】The Sum of the k-th Powers (拉格朗日插值法)
- Android JPush的集成 极光推送
- php中引入文件方式及引入的优先级
- Oracle之索引改造成主键
- 2017/10/22
- Javaweb开发的环境搭建
- CoreData查询常见用法
- jenkins持续集成环境配置
- python 优化加速方案