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,然后后面就一样了,然后最后再加上一个 ...
随机推荐
- .NET 微信Token验证和消息接收和回复
public class wxXmlModel { public string ToUserName { get; set; } public string FromUserName { get; s ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- 交换机--Switch
交换机(Switch)意为"开关"是一种用于电(光)信号转发的网络设备.它可以为接入交换机的任意两个网络节点提供独享的电信号通路.最常见的交换机是以太网交换机. 这里的" ...
- 使用ASP.NET WEB API构建基于REST风格的服务实战系列教程(一)——使用EF6构建数据库及模型
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 使用Entity Framework Code First模式构建数据库对象 已经决定使用EF C ...
- jquery,php之间的ajax关系以及json
1.最简介写法 function AjaxDepotGoods(id){ $.ajax({ url:"{:U('stock/depot_goods')}", success:fun ...
- 关于application/x-www-form-urlencoded等字符编码的解释说明
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型. 下边是说明: application/x-www-form-urlen ...
- 深入理解javascript原型和闭包(9)——简述【执行上下文】下
继续上一篇文章(http://www.cnblogs.com/wangfupeng1988/p/3986420.html)的内容. 上一篇我们讲到在全局环境下的代码段中,执行上下文环境中有如何数据: ...
- 什么时候用@Resource,什么时候用@service
Spring中什么时候用@Resource,什么时候用@service当你需要定义某个类为一个bean,则在这个类的类名前一行使用@Service("XXX"),就相当于讲这个类定 ...
- [Java] Java反射
首先推荐三个十分有趣的网站: http://www.programcreek.com/simple-java/ http://tutorials.jenkov.com/ http://www.meet ...
- 如何应对ISP乱插广告(案例分析)
一.广告从何而来? 利益让人铤而走险,从而推动行业“发展”:广告的利益还真不小,xx房产门户网站上一个广告位少则几千,多则几十万:记得在校读书的时候,刚学会做网站,第一想法就是等自己的网站发展成熟有人 ...