https://vjudge.net/problem/51Nod-1228

Description

T(n) = n^k,S(n) = T(1) + T(2) + ...... T(n)。给出n和k,求S(n)。

例如k = 2,n = 5,S(n) = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55。
由于结果很大,输出S(n) Mod 1000000007的结果即可。

Input

第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 5000)

第2 - T + 1行:每行2个数,N, K中间用空格分割。(1 <= N <= 10^18, 1 <= K <= 2000)Output共T行,对应S(n) Mod 1000000007的结果。

Sample Input

3
5 3
4 2
4 1

Sample Output

225
30
10

分析

求自然数的幂和,有一个基于伯努利数的公式。

于是线性处理出每一项,那么每个case就是线性求解了。

伯努利数怎么计算呢?

首先B0=1,然后有

Bn提取出来,得到

这样就能递推伯努利数了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = 1e9+;
ll C[maxn][maxn],B[maxn],inv[maxn];
inline ll add(ll a){
if(a>=mod) a-=mod;
return a;
}
void init(){
C[][]=;
for(int i=;i<maxn;i++){
C[i][]=C[i][i]=;
for(int j=;j<i;j++){
C[i][j]=add(C[i-][j-]+C[i-][j]);
}
}
inv[]=;
for(int i=;i<maxn;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; //线性递推逆元
B[]=;
for(int i=;i<maxn-;i++){
B[i]=;
for(int j=;j<i;j++){
B[i]=add(B[i]+C[i+][j]*B[j]%mod);
}
B[i]=add(B[i]*(-inv[i+])%mod+mod);
}
}
ll tmp[maxn];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
init();
int T;
scanf("%d",&T);
while(T--){
ll n,k;
scanf("%lld%lld",&n,&k);
n%=mod; //这里取个模比较好,求tmp时才不会爆
tmp[]=;
for(int i=;i<maxn;i++) tmp[i]=tmp[i-]*(n+)%mod;
ll ans=;
for(ll i=;i<=k+;i++){
ans=add(ans+C[k+][i]*B[k+-i]%mod*tmp[i]%mod);
}
ans=ans*inv[k+]%mod;
printf("%lld\n",ans);
}
return ;
}

51Nod - 1228 序列求和 (自然数幂和+伯努利数)的更多相关文章

  1. 51nod 1228 序列求和(伯努利数)

    1228 序列求和  题目来源: HackerRank 基准时间限制:3 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 T(n) = n^k,S(n) = T(1 ...

  2. 51Nod 1228 序列求和

    T(n) = n^k,S(n) = T(1) + T(2) + ...... T(n).给出n和k,求S(n).   例如k = 2,n = 5,S(n) = 1^2 + 2^2 + 3^2 + 4^ ...

  3. 51Node1228序列求和 ——自然数幂和模板&&伯努利数

    伯努利数法 伯努利数原本就是处理等幂和的问题,可以推出 $$ \sum_{i=1}^{n}i^k={1\over{k+1}}\sum_{i=1}^{k+1}C_{k+1}^i*B_{k+1-i}*(n ...

  4. 51nod1228 序列求和(自然数幂和)

    与UVA766 Sum of powers类似,见http://www.cnblogs.com/IMGavin/p/5948824.html 由于结果对MOD取模,使用逆元 #include<c ...

  5. 51nod 1228 序列求和 ( 1^k+2^k+3^k+...+n^k )

    C为组合数,B为伯努利数 具体推到过程略 参考博客:http://blog.csdn.net/acdreamers/article/details/38929067# (我的式子和博客中的不一样,不过 ...

  6. 自然数幂和&伯努利数(Bernoulli)

    二项式定理求自然数幂和 由二项式定理展开得 \[ (n+1)^{k+1}-n^{k+1}=\binom {k+1}1n^k+\binom {k+1}2n^{k-1}+\cdots+\binom {k+ ...

  7. 51NOD 1258 序列求和 V4 [任意模数fft 多项式求逆元 伯努利数]

    1258 序列求和 V4 题意:求\(S_m(n) = \sum_{i=1}^n i^m \mod 10^9+7\),多组数据,\(T \le 500, n \le 10^{18}, k \le 50 ...

  8. UVA766 Sum of powers(1到n的自然数幂和 伯努利数)

    自然数幂和: (1) 伯努利数的递推式: B0 = 1 (要满足(1)式,求出Bn后将B1改为1 /2) 参考:https://en.wikipedia.org/wiki/Bernoulli_numb ...

  9. 51nod 1258 序列求和 V4

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1258 1258 序列求和 V4  基准时间限制:8 秒 空间限制:131 ...

随机推荐

  1. BZOJ 4833: [Lydsy1704月赛]最小公倍佩尔数(数论 + 最值反演)

    题面 令 \({(1+\sqrt 2)}^n=e(n)+f(n)*\sqrt2\) ,其中 \(e(n),f(n)\) 都是整数,显然有 \({(1-\sqrt 2)}^n=e(n)-f(n)*\sq ...

  2. CISCO运维记录之3650堆叠设备升级IOS(Version 16.3.6版本存在bug)

    CISCO运维记录之3650堆叠设备升级IOS(Version 16.3.6版本存在bug) 思科3000系列交换机使用cat3k_caa-universalk9.16.3.6版本存在bug,设备运行 ...

  3. Java 枚举 的学习

    在JDK5.0之后,引进了一种与C语言相通的枚举类型. 所谓枚举类型就是指含有一组具有固定值, 并且容量有限的数据集合. 例如,定义一个星期的枚举类型, 从周一到周日是具有固定大小和固定值的集合 pu ...

  4. css元素溢出

    当子元素的尺寸超过父元素的尺寸时,需要设置父元素显示溢出的子元素的方式,设置的方法是通过overflow属性来设置. overflow的设置项: 1.visible 默认值.内容不会被修剪,会呈现在元 ...

  5. JDK8中的并行流

    1.IntStream.parallel():获取并行流处理 2. Collection中调用parallelStream()获取并行流 3.并行排序Arrays.parallelSort()

  6. Ubuntu16.04下的NetCore环境搭建(附录含Ubuntu 18.04 安装 NetCore2.1)

    跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux VSCode安装:http://www.cnblogs.com/dunitia ...

  7. 模拟@Test的功能实现

    注解和注释区别 * 注释:给程序员看的.* 注解:给虚拟机看的.(让虚拟机看到程序中的注解,注解代表程序的一些特殊的功能.) JDK中提供的注解 @Override :描述子类重写父类的方法: * J ...

  8. A1146. Topological Order

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  9. 怎么理解本征无序态的蛋白质(Intrinsically disordered proteins)

    见维基的解释: An intrinsically disordered protein (IDP) is a protein that lacks a fixed or ordered three-d ...

  10. jdk1.8的新特性:很全面

    JDK1.8: https://www.cnblogs.com/tiantianbyconan/p/3613506.html stream的几个方法: filter: 过滤条件 过滤为空的方法: 刚好 ...