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值, ...
随机推荐
- python中的内建函数
本文用作记录python中的内建函数及其功能,本文内容随时补充. 完整的内建函数及其说明参考官方文档: https://docs.python.org/3.5/library/functions ...
- python解析库之 XPath
1. XPath (XML Path Language) XML路径语言 2. XPath 常用规则: nodename 选取此节点的所有子节点 / 从当前 ...
- Jenkins自动化搭建测试环境(二)
Fork项目 找到项目 单击Fork 这时,会发送一个邮件到你的git邮箱中,点击链接即可完成fork 这样,这个工程就已经fork到自己的git上了 然后就可以下载这个工程到本机了 这里我们需要使用 ...
- Java程序的结构和执行
目录 Java程序的结构 Java程序的执行 source code -- compiler -- class file -- JVM compiler JVM Java语法 数据类型 数据的存储 堆 ...
- python - work4
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: work_20181108.py@ide: PyCharm Communit ...
- install cinnamon on ubuntu 14.04
emotion: I feel not comfortable with ubuntu 14.04 default desktop unity,i still look for a alternati ...
- 【Luogu】P3205合唱队(区间DP)
题目链接 通过这题我发现我已经不会DP了 区间DP,f[i][j]是从左面转移来的,d[i][j]是从右面转移来的 然后DP方程是 ]) f[i][j]+=f[i+][j]; ][j]; f[i][j ...
- 二进制<1>
Matrix67:位运算简介及实用技巧(一) 基础篇 什么是位运算? 程序中的所有数在计算机内存中都是以二进制的形式储存的.位运算说穿了,就是直接对整数在内存中的二进制位进行操作.比如,and运 ...
- [POJ1155]TELE
[POJ1155]TELE 试题描述 A TV-network plans to broadcast an important football match. Their network of tra ...
- bzoj3238 [Ahoi2013]差异 后缀数组+单调栈
[bzoj3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...