题意: 求 ,要求M尽量小。

析:这其实就是一个伯努利数,伯努利数公式如下:

伯努利数满足条件B0 = 1,并且

也有

几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 20 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} LL lcm(LL a, LL b){
return a * (b / gcd(a, b));
} struct Fraction{
LL mole;
LL deno;
Fraction() : mole(0), deno(1){ }
Fraction(LL m, LL d) : mole(m), deno(d) { sinal(); } void sinal(){
if(mole < 0 && deno < 0) mole = -mole, deno = -deno;
else if(mole >= 0 && deno < 0) mole = -mole, deno = -deno;
if(deno == 0) mole = 1;
} friend Fraction operator + (const Fraction &lhs, const Fraction &rhs){
LL l = lcm(lhs.deno, rhs.deno);
LL m = lhs.mole * (l/lhs.deno) + rhs.mole * (l/rhs.deno);
return Fraction(m, l);
} friend Fraction operator - (const Fraction &lhs, const Fraction &rhs){
LL l = lcm(lhs.deno, rhs.deno);
LL m = lhs.mole * (l/lhs.deno) - rhs.mole * (l/rhs.deno);
return Fraction(m, l);
} friend Fraction operator * (const Fraction &lhs, const Fraction &rhs){
LL m = lhs.mole * rhs.mole;
LL d = lhs.deno * rhs.deno;
LL g = gcd(m, d);
return Fraction(m / g, d / g);
} friend Fraction operator / (const Fraction &lhs, const Fraction &rhs){
LL m = lhs.mole * rhs.deno;
LL d = lhs.deno * rhs.mole;
LL g = gcd(m, d);
return Fraction(m / g, d / g);
} void print(){
printf("%lld / %lld\n", mole, deno);
}
}; Fraction C[maxn][maxn];
Fraction B[maxn]; void init(){
for(int i = 0; i < 25; ++i)
C[i][0] = C[i][i] = Fraction(1, 1);
for(int i = 2; i < 25; ++i)
for(int j = 1; j < i; ++j)
C[i][j] = C[i-1][j] + C[i-1][j-1];
B[0] = Fraction(1, 1);
for(int i = 1; i < 23; ++i){
for(int j = 0; j < i; ++j)
B[i] = B[i] + C[i+1][j] * B[j];
B[i] = B[i] * Fraction(-1LL, i+1LL);
}
} Fraction ans[maxn]; int main(){
init();
int T; cin >> T;
while(T--){
scanf("%d", &n);
LL l = 1;
for(int i = 1; i <= n+1; ++i){
ans[i] = C[n+1][i] * B[n+1-i] * Fraction(1LL, n+1LL);
l = lcm(l, ans[i].deno);
}
ans[n] = ans[n] + Fraction(1LL, 1LL);
printf("%lld", l);
for(int i = n+1; i > 0; --i)
printf(" %lld", l / ans[i].deno * ans[i].mole);
printf(" 0\n");
if(T) printf("\n");
}
return 0;
}

  

UVa 766 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. UVA 10622 - Perfect P-th Powers(数论)

    UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...

  3. [CSAcademy]Sum of Powers

    [CSAcademy]Sum of Powers 题目大意: 给定\(n,m,k(n,m,k\le4096)\).一个无序可重集\(A\)为合法的,当且仅当\(|A|=m\)且\(\sum A_i=n ...

  4. Euler's Sum of Powers Conjecture

    转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...

  5. 【POJ1707】【伯努利数】Sum of powers

    Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...

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

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

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

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

  8. uva 11752 The Super Powers 素数+大数判断大小

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  9. UVA 10622 Perfect P-th Powers

    https://vjudge.net/problem/UVA-10622 将n分解质因数,指数的gcd就是答案 如果n是负数,将答案除2至奇数 原理:(a*b)^p=a^p*b^p #include& ...

随机推荐

  1. LeetCode 293. Flip Game

    原题链接在这里:https://leetcode.com/problems/flip-game/description/ 题目: You are playing the following Flip ...

  2. bzoj 2013: A huge tower 数学

    题目: 有\(N(2\leq N\leq 620000)\)块砖,要搭一个\(N\)层的塔,要求:如果砖\(A\)在砖\(B\)上面,那么\(A\)不能比\(B\)的长度\(+D\)要长.问有几种方法 ...

  3. 洛谷 P3223 [HNOI2012]排队

    题目描述 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不同的) 输入输 ...

  4. FS系统开发设计(思维导图)

      FS系统开发设计(思维导图) 最近做了一个小系统,公司应急,要对各个部门进行费用成本核算分摊,做运维,苦于无奈,简简单单的设计了一下,模仿用友ERP软件,首先对DB进行了初步设计,总体如下: 未完 ...

  5. 一个Bug 差点让服务器的文件系统崩溃

    昨天,公司的美国客户发邮件给我,说我的软件出问题了,我查来查去,发现居然是服务器上一个目录无法删除,一删除就报 cannot read from the source file or disk. 如果 ...

  6. web打印详解

    在B/S模式开发中,打印是个很大的困扰.无论是采用页面直接输出或者引用WORD.DLL也好,都有不足之处. 目前最好的办法就是采用第三方控件,网上流传的打印控件有很多.总结了下推荐几个给大家: 一.首 ...

  7. Java基础--阻塞队列ArrayBlockingQueue

    ArrayBlockingQueue是阻塞队列的一种,基于数组实现,长度固定,队尾添加,队首获取, 构造函数: ArrayBlockingQueue(int capacity) ArrayBlocki ...

  8. 本地测试html文件时CSS效果显示, 但是当django的服务器上运行时效果不显示

    本地测试时各种效果都显示, 但是当在django服务器上测试时, 效果却不显示, 原因是我将css文件放在一个static文件夹里, 没有在settings中设置static_dir选项.将stati ...

  9. SqlServer——事务一编程进阶(SqlServer技术内幕 T-SQL程序设计 第九章

    事务格式如下: 1.开启事务: begin tran 2.提交事务:commit tran 3.回滚事务:rollback tran 判断事务是提交还是应该回滚有两种方式,一是全局变量 @@error ...

  10. 问题:oracle decode;结果:oracle中的decode的使用

    oracle中的decode的使用 Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件 ...