自然数幂和:

(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. 理解Cookie和Session机制(转)

    目录[-] Cookie机制 什么是Cookie 记录用户访问次数 Cookie的不可跨域名性 Unicode编码:保存中文 BASE64编码:保存二进制图片 设置Cookie的所有属性 Cookie ...

  2. Mvc视图引擎、寻址规则

    目前MVC中用的较多的视图引擎应该是WebFormViewEngine和RazorViewEngine了. 一个Request请求首先会进入Routing进行判断,对于错误的url是不能被路由匹配到的 ...

  3. MySQL索引结构--由 B-/B+树看

    B-树 B-树,这里的 B 表示 balance( 平衡的意思),B-树是一种多路自平衡的搜索树它类似普通的平衡二叉树,不同的一点是B-树允许每个节点有更多的子节点.下图是 B-树的简化图. B-树有 ...

  4. CLI:如何使用Go开发命令行

    CLI或者"command line interface"是用户在命令行下交互的程序.由于通过将程序编译到一个静态文件中来减少依赖,一次Go特别适合开发CLI程序.如果你编写过安装 ...

  5. 用File判断D盘下面是否还有txt文件

    package cn.idcast; import java.io.File; public class File1 { public static void main(String[] args) ...

  6. eclipse安装Eclipse Memory Analyzer插件

    在Install New software中输入 http://archive.eclipse.org/mat/1.2/update-site/ 然后选择Memory Analyzer for Ecl ...

  7. SQL SERVER批量修改表名前缀

    比如前缀由mms_修改为 ets_ exec   sp_msforeachtable     @command1='  declare   @o   sysname,@n   sysname     ...

  8. Vundle的安装

    1.Vundle.vim 安装 https://github.com/VundleVim/Vundle.vim 2.插件安装https://github.com/yangyangwithgnu/use ...

  9. java基础知识(一)数据类型(下)

    前面介绍了java的8种基本数据类型,包括boolean, byte, char, short,  int, long, float, double.同时,java也提供了这些类型的封装类,分别为Bo ...

  10. javascript数据结构-优先队列

    这里之所以扩充一个 有限队列 是因为,生活使用中队列通常会附加优先级,比如排队买票,一般老人和军人等会有优先权限. 实现:继承上篇的 普通队列实现.这里用一种方法,入队的时候,进行排序插入到指定位置, ...