Codeforces 622F The Sum of the k-th Powers
Discription
There are well-known formulas: ,
,
. Also mathematicians found similar formulas for higher degrees.
Find the value of the sum modulo 109 + 7 (so you should find the remainder after dividing the answer by the value 109 + 7).
Input
The only line contains two integers n, k (1 ≤ n ≤ 109, 0 ≤ k ≤ 106).
Output
Print the only integer a — the remainder after dividing the value of the sum by the value 109 + 7.
Example
4 1
10
4 2
30
4 3
100
4 0
4 拉格朗日差值裸题。。。
为什么我以前都用高斯消元做自然幂数和2333333
拉格朗日差值的构造原理和中国剩余定理类似,这里就不在赘述,网上也有很多讲的。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000010;
const int ha=1000000007;
int jc[maxn+5],TT,l;
int n,k,ans=0,base; inline int add(int x,int y){
x+=y;
return x>=ha?x-ha:x;
} inline int ksm(int x,int y){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} inline void init(){
jc[0]=1;
for(int i=1;i<=maxn;i++) jc[i]=jc[i-1]*(ll)i%ha;
l=k+2,base=1;
} inline void solve(){
if(n<=l){
for(int i=1;i<=n;i++) ans=add(ans,ksm(i,k));
}
else{
for(int i=1;i<=l;i++) base=base*(ll)(n-i)%ha;
for(int i=1,now;i<=l;i++){
TT=add(TT,ksm(i,k));
now=base*(ll)ksm(n-i,ha-2)%ha*(ll)ksm(jc[i-1],ha-2)%ha*(ll)ksm(jc[l-i],ha-2)%ha;
if((l-i)&1) now=now*(ll)(ha-1)%ha;
ans=add(ans,TT*(ll)now%ha);
}
}
} int main(){
scanf("%d%d",&n,&k);
init();
solve();
printf("%d\n",ans);
return 0;
}
Codeforces 622F The Sum of the k-th Powers的更多相关文章
- codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法
题目链接 求sigma(i : 1 to n)i^k. 为了做这个题这两天真是补了不少数论, 之前连乘法逆元都不知道... 关于拉格朗日插值法, 我是看的这里http://www.guokr.com/ ...
- Codeforces 622F The Sum of the k-th Powers(数论)
题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...
- Codeforces 622F The Sum of the k-th Powers ( 自然数幂和、拉格朗日插值法 )
题目链接 题意 : 就是让你求个自然数幂和.最高次可达 1e6 .求和上限是 1e9 分析 : 题目给出了最高次 k = 1.2.3 时候的自然数幂和求和公式 可以发现求和公式的最高次都是 k+1 ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- codeforces 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...
- [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 ...
- Codeforces 631E Product Sum 斜率优化
我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移.对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优. 我们设最优值为val, val = max(a[ j ] ...
- 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 ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
随机推荐
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- Python中的属性访问与描述符
Python中的属性访问与描述符 请给作者点赞--> 原文链接 在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个nam ...
- 安装go 1.5 & 部署
https://storage.googleapis.com/golang/go1.5.linux-amd64.tar.gz tar -C /usr/local -xzf go1.5.linux-am ...
- HDU 3488 KM Tour
参考题解 这题注意有重边.. #include <cstdio> #include <cstring> #include <algorithm> using nam ...
- 前端PS切图技巧
先选择“编辑”-“首选项” 打开,找到“参考线” 设置一下每格网格 100像素 5个细块 确定后 ctrl+‘ 出现网格.(通过网格对齐切图比用参考线切图更好). 如果使用PS cc的软件的话, ...
- change login screen wallpaper on ubuntu14.04
install lightdm-gtk-greeter $ apt-get install lightdm config lightdm $ vim /etc/lightdm/lightdm-gtk- ...
- [svn学习篇]svn使用教程
http://www.cnblogs.com/longshiyVip/p/4905901.html http://blog.csdn.net/dily3825002/article/details/6 ...
- Struts2理解——转发和重定向
转发和重定向设置: <action name="deptAction" class="com.syaccp.erp.action.DeptA ...
- iOS学习笔记43-Swift(三)类
一.Swift的类class 作为一门面向对象语言,类也是Swift的非常重要的类型,我们先来看下一个简单的类 //Swift中一个类可以不继承于任何其他基类,那么此类本身就是一个基类 class P ...
- kb-07专题线段树-04--离散化;
/* poj2528 线段树 好题,用到了离散化,二分定位,特殊的区间查寻方式:在下面的代码注释中有详细的解释: */ #include<iostream> #include<cst ...