自然数幂和:

(1)

伯努利数的递推式:

B0 = 1

(要满足(1)式,求出Bn后将B1改为1 /2)

参考:https://en.wikipedia.org/wiki/Bernoulli_number

http://blog.csdn.net/acdreamers/article/details/38929067

使用分数类,代入求解

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 25, INF = 0x3F3F3F3F; LL gcd(LL a, LL b){
while(b){
LL t = a % b;
a = b;
b = t;
}
return a;
} LL lcm(LL a, LL b){
return a / gcd(a, b) * b;
} struct frac{
LL x, y;
frac(){
x = 0;
y = 1;
}
frac(LL x1, LL y1){
x = x1;
y = y1;
}
frac operator*(const frac &tp)const{
LL a = x * tp.x;
LL b = y * tp.y;
LL d = gcd(a, b);
a /= d;
b /= d;
if(a >= 0 && b < 0){
a = -a;
b = -b;
}
return frac(a, b);
} frac operator+(const frac &tp)const{
LL a = x * tp.y + tp.x * y;
LL b = y * tp.y;
LL d = gcd(a, b);
a /= d;
b /= d;
if(a >= 0 && b < 0){
a = -a;
b = -b;
} return frac(a, b);
} }ans[N][N], bo[N]; LL cm[N][N];
void init(){
memset(cm, 0, sizeof(cm));
cm[0][0] = 1;
for(int i = 1; i < N; i++){
cm[i][0] = 1;
for(int j = 1; j <= i; j++){
cm[i][j] = cm[i - 1][j - 1] + cm[i - 1][j];
}
} bo[0].x = 1, bo[0].y = 1;
for(int i = 1; i < N; i++){
bo[i].x = 0;
bo[i].y = 1;
for(int j = 0; j < i; j++){
bo[i] = bo[i] + frac(cm[i + 1][j], 1) * bo[j];
}
bo[i] = bo[i] * frac(-1, i + 1);
}
bo[1].x = 1; bo[1].y = 2;
for(int m = 0; m < N; m++){
for(int k = 0; k <= m; k++){
ans[m][m + 1 - k] = frac(cm[m + 1][k], 1) * bo[k] * frac(1, m + 1);
}
LL lc = ans[m][0].y;
for(int k = 1; k <= m; k++){
lc = lcm(ans[m][k].y, lc);
}
for(int k = 0; k <= m + 1; k++){
LL d = lc / ans[m][k].y;
ans[m][k].x *= d;
ans[m][k].y *= d;
}
} } int main(){
init();
int t;
cin >> t;
while(t--){
int n;
cin >>n;
printf("%lld ", ans[n][0].y);
for(int i = n + 1; i >= 0; i--){
if(i == 0){
printf("%lld\n", ans[n][i].x);
}else{
printf("%lld ", ans[n][i].x);
}
}
if(t){
printf("\n");
}
} return 0;
}

  

UVA766 Sum of powers(1到n的自然数幂和 伯努利数)的更多相关文章

  1. CodeForces - 622F:The Sum of the k-th Powers (拉格朗日插值法求自然数幂和)

    There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees. ...

  2. [CSAcademy]Sum of Powers

    [CSAcademy]Sum of Powers 题目大意: 给定\(n,m,k(n,m,k\le4096)\).一个无序可重集\(A\)为合法的,当且仅当\(|A|=m\)且\(\sum A_i=n ...

  3. Euler's Sum of Powers Conjecture

    转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...

  4. [伯努利数] poj 1707 Sum of powers

    题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Lim ...

  5. 【POJ1707】【伯努利数】Sum of powers

    Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...

  6. POJ 1707 Sum of powers(伯努利数)

    题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...

  7. 求自然数幂和 B - The Sum of the k-th Powers CodeForces - 622F

    题解: 很多方法 斯特林数推导略麻烦但是不依赖于模数 代码: 拉格朗日插值 由于可以证明这是个K+1次多项式于是可以直接用插值 #include <bits/stdc++.h> using ...

  8. sum of powers

    题意: 考虑所有的可重集{a1,a2,a3....ak} 满足a1+a2+....+ak=n,求所有a1^m+a2^m+a3^m的和 n,m,k<=5000 题解: part1: 考虑f[i][ ...

  9. UVa 766 Sum of powers (伯努利数)

    题意: 求 ,要求M尽量小. 析:这其实就是一个伯努利数,伯努利数公式如下: 伯努利数满足条件B0 = 1,并且 也有 几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个 ...

随机推荐

  1. Mac上更新Ruby

    因为准备在项目中使用bootstrap,在安装bootstrap过程中提示需要Ruby的版本在1.9.2以上,而目前使用的Ruby版本是Mac系统自带的1.8.7.所以需要对Ruby进行升级.这里使用 ...

  2. 下载imagenet2012数据集

    摸索了一下,imagenet2012下载,跟大家分享一下 用迅雷会员加速都可以下载,有的用百度云也可以离线下载 http://www.image-net.org/challenges/LSVRC/20 ...

  3. DOSBOX 自动挂载技巧

    DOSBOX下载之后,win10已经不支持debug了(win-XP虚拟机有!),所以需要单独下载,每次使用都需要挂载上去,十分不方便. 解决办法是修改属性文件,每次挂载都自动执行: 把编写好的汇编文 ...

  4. Size Balance Tree(SBT模板整理)

    /* * tree[x].left 表示以 x 为节点的左儿子 * tree[x].right 表示以 x 为节点的右儿子 * tree[x].size 表示以 x 为根的节点的个数(大小) */ s ...

  5. iOS提交后申请加急审核

    链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i would like to里选择加急审核 然后填 ...

  6. primefaces 查询 点击按钮 加载 动画 ajax loader

    只要在/WEB-INF/template.xhtml中body 里面加入: <ui:insert name="status"> <p:ajaxStatus sty ...

  7. Android开发笔记之《知识漏点纪录与学习》

    1. NDK的异常捕获方法 2. Andorid性能优化:http://www.cnblogs.com/yezhennan/p/5442557.html 3. 插件化与组件化:http://blog. ...

  8. Maven+Spring MVC Spring Mybatis配置

    环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...

  9. kettle参数、变量详细讲解[转]

    kettle 3.2 以前的版本里只有 variable 和 argument,kettle 3.2 中,又引入了 parameter 概念:variable 即environment variabl ...

  10. 用类方法------>快速创建一个autorelease的对象,在封装的类方法内部

    在封装的类方法内部,也就是+ (id)personWithName:(NSString *)name andAge:(int)age内部: 创建了一个person对象,并且创建了一个person*类型 ...