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. Marriage Match III HDU - 3277(二分权值 + 拆点 建边)

    题意: 只不过是hdu3081多加了k种选择 想一下,最多能玩x轮,是不是就是每个女生能最多选x个男生 现在题中的每个女生比3081多了k中选择   那就把女生拆点  i  i‘ i --> i ...

  2. MT【293】拐点处切线

    (2018浙江高考压轴题)已知函数$f(x)=\sqrt{x}-\ln x.$(2)若$a\le 3-4\ln 2,$证明:对于任意$k>0$,直线$y=kx+a$ 与曲线$y=f(x)$有唯一 ...

  3. Nagios 使用 NSClient++ 监控Windows Server

    在被监控的Windows server 主机上安装NSClinet++下载地址:https://www.nsclient.org/download/32bit:http://files.nsclien ...

  4. Codeforces 346D Robot Control(01BFS)

    题意 有一个 \(N\) 个点, \(M\) 条边的有向图, 初始有一个机器人在 \(1\) 号点. 每个时刻, 这个机器人会随机选择一条从该点出发地边并通过.当机器人到达点 \(N\) 时, 它就会 ...

  5. Leetcode 345. 反转字符串中的元音字母 By Python

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  6. android ViewStub简单介绍

    ViewStub是一种非常灵活的视图,主要用于布局资源的实时加载. ViewStub 的继承类关系如下: public final class ViewStubextends View java.la ...

  7. 【转】linux清屏的几种方法

    在windows的DOS操作界面里面,清屏的命令是cls,那么在linux 里面的清屏命令是什么呢?下面笔者分享几种在linux下用过的清屏方法. 1.clear命令.这个命令将会刷新屏幕,本质上只是 ...

  8. Zabbix3.4监控平台部署

    环境依赖 CentOS 7.3 + PHP5.4 + MariaDB + Nginx Zabbix Server 3.4.1 环境要求 12 CPU ,最少8 CPU 32G 内存,最少16G 1T ...

  9. 基于配置文件的redis的主从复制

    redis中主从复制有很多种配置方法: 1. 使用配置文件即为redis.conf来配置 在随从redis中配置 # slaveof {masterHost} {MastePort} slaveof ...

  10. 数组和list互转

    数组转list 方法1: String[] stringArray = { "a", "b", "c", "d", &q ...