Description

A young schoolboy would like to calculate the sum


for some fixed natural k and different natural n. He observed that calculating ik
for all i (1<=i<=n) and summing up results is a too slow way to
do it, because the number of required arithmetical operations increases
as n increases. Fortunately, there is another method which takes only a
constant number of operations regardless of n. It is possible to show
that the sum Sk(n) is equal to some polynomial of degree k+1 in the variable n with rational coefficients, i.e.,



We require that integer M be positive and as small as possible. Under this condition the entire set of such numbers (i.e. M, ak+1, ak, ... , a1, a0)
will be unique for the given k. You have to write a program to find
such set of coefficients to help the schoolboy make his calculations
quicker.

Input

The input file contains a single integer k (1<=k<=20).

Output

Write integer numbers M, ak+1, ak, ... , a1, a0
to the output file in the given order. Numbers should be separated by
one space. Remember that you should write the answer with the smallest
positive M possible.

Sample Input

2

Sample Output

6 2 3 1 0

Source

【分析】
题意就是给出一个k,找一个最小的M使得中a[i]皆为整数.
这个涉及到伯努利数的一些公式,如果不知道的话基本没法做..

1. 伯努利数与自然数幂的关系:

2. 伯努利数递推式:

先通过递推式求得伯努利数,然后用1公式并将中间的(n+1) ^ i,变成n ^ i,后面再加上n ^ k,化进去就行了。

 /*
宋代朱敦儒
《西江月·世事短如春梦》
世事短如春梦,人情薄似秋云。不须计较苦劳心。万事原来有命。
幸遇三杯酒好,况逢一朵花新。片时欢笑且相亲。明日阴晴未定。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
const double Pi = acos(-1.0);
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b){return b == ? a: gcd(b, a % b);}
struct Num{
ll a, b;//分数,b为分母
Num(ll x = , ll y = ) {a = x;b = y;}
void update(){
ll tmp = gcd(a, b);
a /= tmp;
b /= tmp;
}
Num operator + (const Num &c){
ll fz = a * c.b + b * c.a, fm = b * c.b;
if (fz == ) return Num(, );
ll tmp = gcd(fz, fm);
return Num(fz / tmp, fm / tmp);
}
}B[MAXN], A[MAXN];
ll C[MAXN][MAXN]; void init(){
//预处理组合数
for (int i = ; i < MAXN; i++) C[i][] = C[i][i] = ;
for (int i = ; i < MAXN; i++)
for (int j = ; j < MAXN; j++) C[i][j] = C[i - ][j] + C[i - ][j - ];
//预处理伯努利数
B[] = Num(, );
for (int i = ; i < MAXN; i++){
Num tmp = Num(, ), add;
for (int j = ; j < i; j++){
add = B[j];
add.a *= C[i + ][j];
tmp = tmp + add;
}
if (tmp.a) tmp.b *= -(i + );
tmp.update();
B[i] = tmp;
}
}
void work(){
int n;
scanf("%d", &n);
ll M = n + , flag = , Lcm;
A[] = Num(, );
for (int i = ; i <= n + ; i++){
if (B[n + - i].a == ) {A[i] = Num(, );continue;}
Num tmp = B[n + - i];
tmp.a *= C[n + ][i];//C[n+1][i] = C[n + 1][n + 1 - i]
tmp.update();
if (flag == ) Lcm = flag = tmp.b;
A[i] = tmp;
}
A[n] = A[n] + Num(n + , ); for (int i = ; i <= n + ; i++){
if (A[i].a == ) continue;
Lcm = (Lcm * A[i].b) / gcd(Lcm, A[i].b);
}
if (Lcm < ) Lcm *= -;
M *= Lcm;
printf("%lld", M);
for (int i = n + ; i >= ; i--) printf(" %lld", A[i].a * Lcm / A[i].b);
} int main(){ init();
work();
//printf("%lld\n", C[5][3]);
return ;
}

【POJ1707】【伯努利数】Sum of powers的更多相关文章

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

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

  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. UVA766 Sum of powers(1到n的自然数幂和 伯努利数)

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

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

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

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

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

  7. sum of powers

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

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

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

  9. [转] Loren on the Art of MATLAB

    http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ Loren ...

随机推荐

  1. JSP页面同时操作所有Input输入框

    项目里要写个function,对页面上所有input输入框进行非空判断,对非空input全部置为readOnly,提交的时候判断是否有空白项目. var inputs=document.getElem ...

  2. JAVA环境变量正确设置,却无法在cmd中javac

    今晚试着重新设置JAVA的环境变量,按着度娘告知的操作方法: 1.打开我的电脑--属性--高级--环境变量 2.新建系统变量JAVA_HOME 和CLASSPATH 变量名:JAVA_HOME 变量值 ...

  3. useradd、passwd、userdel

    useradd是新建用户 userdel -r 是删除用户 passwd是修改密码 groupadd是新建组 groupdel是删除组 useradd yonghu  为添加用户 echo " ...

  4. S2SH邮件注册激活后注册成功

    首先我的思路是这样的:①接收从客户端接收过来的数据(密码,用户名,邮箱) ②将密码进行MD5加密,然后将信息用"_"连接起来(用于后面分解) ③将信息交个一个工具类中实现生成邮件信 ...

  5. MINA学习之体系介绍

    基于MINA应用程序结构图: 我们可以看出,MINA是应用程序(客户端或服务端)和底层基于TCP,UDP等通讯协议的网络层之间的粘合剂.而且各个模块之间是相互独立的,你只需要在MINA体 系基础上设计 ...

  6. DataTable导入到Excel文件

        ;                saveFileDialog.RestoreDirectory = ;            , intIndex] = column.ColumnName; ...

  7. web配置nagios工具

    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员, ...

  8. POJ 1185 炮兵

    是中国标题.大家都说水问题.但是,良好的1A它? 标题效果: 给出n*m的矩阵,当某个单元格有炮兵部队时它的上下左右两格(不包含斜着的方向)是这支部队的攻击范围.问在两支部队之间不可能相互攻击到的情况 ...

  9. 分布式服务框架 Zookeeper -- 管理分布式环境中的数据--转载

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/ Zookeeper 分布式服务框架是 Apache Hadoop ...

  10. Java基础知识强化之集合框架笔记63:Map集合之HashMap嵌套ArrayList

    1. ArrayList集合嵌套HashMap集合并遍历. 需求:假设ArrayList集合的元素是HashMap.有3个.每一个HashMap集合的键和值都是字符串.元素我已经完成,请遍历. 结果: ...