【POJ1707】【伯努利数】Sum of powers
Description

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
Output
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

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的更多相关文章
- [伯努利数] poj 1707 Sum of powers
题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS Memory Lim ...
- [CSAcademy]Sum of Powers
[CSAcademy]Sum of Powers 题目大意: 给定\(n,m,k(n,m,k\le4096)\).一个无序可重集\(A\)为合法的,当且仅当\(|A|=m\)且\(\sum A_i=n ...
- Euler's Sum of Powers Conjecture
转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...
- UVA766 Sum of powers(1到n的自然数幂和 伯努利数)
自然数幂和: (1) 伯努利数的递推式: B0 = 1 (要满足(1)式,求出Bn后将B1改为1 /2) 参考:https://en.wikipedia.org/wiki/Bernoulli_numb ...
- UVa 766 Sum of powers (伯努利数)
题意: 求 ,要求M尽量小. 析:这其实就是一个伯努利数,伯努利数公式如下: 伯努利数满足条件B0 = 1,并且 也有 几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个 ...
- POJ 1707 Sum of powers(伯努利数)
题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...
- sum of powers
题意: 考虑所有的可重集{a1,a2,a3....ak} 满足a1+a2+....+ak=n,求所有a1^m+a2^m+a3^m的和 n,m,k<=5000 题解: part1: 考虑f[i][ ...
- 51nod1228 序列求和(自然数幂和)
与UVA766 Sum of powers类似,见http://www.cnblogs.com/IMGavin/p/5948824.html 由于结果对MOD取模,使用逆元 #include<c ...
- [转] Loren on the Art of MATLAB
http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ Loren ...
随机推荐
- 批量导入图片到word并添加文件名
Sub InsertPic() Dim myfile As FileDialog Set myfile = Application.FileDialog(msoFileDialogFilePicker ...
- 字符编码笔记:ASCII,Unicode和UTF-8,附带 Little endian和Big endian的解释
作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步 ...
- 数字集成电路设计-8-一个简单sobel图像边缘检测加速器的设计,实现,仿真与综合
引言 图像视频处理等多媒体领域是FPGA应用的最主要的方面之一,边缘检测是图像处理和计算机视觉中的基本问题,所以也是最常用的,随着数据量的不断增加以及对实时性的要求,一般软件已经不能满足实际需要,这时 ...
- Code Forces 711C Coloring Trees
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Linux编辑器的选择使用
在执行 crontab -e的时候 发现编辑器不对 用的是nano 立刻 sudo select-editor 然后终端返回 Select an editor. To change later, ru ...
- PHP如何取出数组最后一个元素?
<?php $array=array("first","sencond","third"); #1.echo end($array); ...
- Genymotion常见问题汇总(转)
为什么说是常见问题整合呢,因为我就是Genymotion最悲剧的使用者,该见过的问题,我基本都见过了,在此总结出这血的教训,望大家不要重蹈覆辙. 常见问题1:Genymotion在开启模拟器时 ...
- JDBC——Sql Server
sun公司设计一套java语言操作不同的数据库提供的是接口,二具体的实现类是由各大数据库厂商实现的. private static final String driver= "com.mic ...
- 在VS Nuget命令行下进行EF数据库迁移
找到项目中,用到数据库DLL的地方,然后选中该项目,打开Nuget命令行输入以下的命令: 其中cardId为迁移名称,自己取
- SAP ABAP 程序调用FORM
*&---------------------------------------------------------------------* *& Report ZHAIM_FOR ...