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研发方向如何准备BAT技术面试答案(上)
http://blog.csdn.net/q979392157/article/details/52164319 阿里面试题总结 http://blog.csdn.net/q979392157/art ...
- jQuery 选择同时包含两个class的元素的实现方法
Jquery选择器 多个 class属性参照以下案例 <element class="a b good list card"> 1. 交集选择: $(".a. ...
- 动态树之LCT(link-cut tree)讲解
动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...
- CSS-border属性制作小三角
1--三角向上下左上.右上.右下.左下这四个方向突出的样式 向左上角突出: border-color: transparent transparent transparent #FFCC00; bor ...
- 如何学好JAVA编程
我的思路: 1.我觉得先学学JSP,用纯JSP做个日记本简单的小系统,纯粹从语言层面上了解一些基 础知识,把tomcat玩熟了,就用记事本编就行,主要是熟悉,能够理解jsp运行机制.然 ...
- 创建线注记LineElement
1.根据2点创建一条线 /// <summary> /// 创建线 /// </summary> /// <param name="pnt1"> ...
- C#直接赋值和反射赋值(无GC)的性能比较
using System; using System.Reflection; using System.Diagnostics; using System.Runtime.InteropService ...
- C#中精确计时的一点收获 【转】
C#中精确计时的一点收获 [转] 以下所有代码运行环境:Windows 2003, Intel(R) Core(TM) 2 Duo CPU E8400 @ 3.00GHz 2.99GHz,2.96G ...
- 数据存储_ SQLite (1)
一.SQL语句 如果要在程序运行过程中操作数据库中的数据,那得先学会使用SQL语句 1.什么是SQL SQL(structured query language):结构化查询语言 SQL 是一种对关系 ...
- jquery 判断网络资源,网络文件是否存在
前提是,不能跨域访问引入jquery库<script src="jQuery.js" type="text/javascript"></scr ...