UVA766 Sum of powers(1到n的自然数幂和 伯努利数)
自然数幂和:

(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的自然数幂和 伯努利数)的更多相关文章
- CodeForces - 622F:The Sum of the k-th Powers (拉格朗日插值法求自然数幂和)
There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees. ...
- [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 ...
- [伯努利数] poj 1707 Sum of powers
题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS Memory Lim ...
- 【POJ1707】【伯努利数】Sum of powers
Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...
- POJ 1707 Sum of powers(伯努利数)
题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...
- 求自然数幂和 B - The Sum of the k-th Powers CodeForces - 622F
题解: 很多方法 斯特林数推导略麻烦但是不依赖于模数 代码: 拉格朗日插值 由于可以证明这是个K+1次多项式于是可以直接用插值 #include <bits/stdc++.h> using ...
- sum of powers
题意: 考虑所有的可重集{a1,a2,a3....ak} 满足a1+a2+....+ak=n,求所有a1^m+a2^m+a3^m的和 n,m,k<=5000 题解: part1: 考虑f[i][ ...
- UVa 766 Sum of powers (伯努利数)
题意: 求 ,要求M尽量小. 析:这其实就是一个伯努利数,伯努利数公式如下: 伯努利数满足条件B0 = 1,并且 也有 几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个 ...
随机推荐
- [转]Java compiler level does not match解决方法
查看链接:http://jingyan.baidu.com/article/95c9d20da3ec5fec4e756186.html
- c# cache 缓存
System.Web.Caching简介 /// <summary> /// 创建随机字符串 /// </summary> /// <returns></re ...
- js-JavaScript高级程序设计学习笔记19
第22章 高级技巧 1.高级函数 1.安全的类型检测. typeof,instanceof并非完全可靠. 安全的类型检测:使用Object原生的toString()方法. function isArr ...
- Hbase集群master.HMasterCommandLine: Master exiting
2016-12-15 17:01:57,473 INFO [main] impl.MetricsSystemImpl: HBase metrics system started 2016-12-15 ...
- TP-LINK WR941 DD-WRT刷回OpenWRT及OpenWRT刷回原厂固件
1.DD-Wrt 刷回 OpenWrt A.从官网下载固件: root@TL-DDWRT:/tmp# wget http://downloads.openwrt.org/barrier_breaker ...
- 优化php代码 - 字符串echo输出 逗号也可作php连接符
2016年12月12日10:00:16 ====================== 网页访问速度的提升,是可以通过代码的优化来实现的.代码的优化,并不是说代码越少越好,而是主要看代码的运行能力和执行 ...
- 第四章 电商云化,4.2 集团AliDocker化双11总结(作者: 林轩、白慕、潇谦)
4.2 集团AliDocker化双11总结 前言 在基础设施方面,今年双11最大的变化是支撑双11的所有交易核心应用都跑在了Docker容器中.几十万Docker容器撑起了双11交易17.5万笔每秒的 ...
- 双守护进程(不死service)-5.0系统以下
上链接: http://files.cnblogs.com/files/andlp/DaemonProcess.zip 5.0以上 参考marsDaemon
- 使用JSF框架过程中的若干典型问题及其解决方案
1.commandXxx点击后,不调用action中的方法: 原因1:xhtml后缀名的文件,最终也会转化为普通的html文件(这是熟悉JSF框架的关键.),commandXxx点击后不调用后台act ...
- uboot学习第一天
Windows操作系统BIOS(设置) Windows系统 文件系统 驱动程序 应用程序 linux操作系统bootloader(引导系统) kernel(内核) 文件系统 驱动程序 应用程序 交叉编 ...