题意: 求 ,要求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. android sdk国内服务器下载

    推荐使用国内东软的服务器下载android sdk相关: 如果是android sdk manager: HTTP Proxy Server : mirrors.neusoft.edu.cn HTTP ...

  2. 大整数乘法(Comba 乘法 (Comba  Multiplication)原理)

    Comba 乘法以(在密码学方面)不太出名的 Paul G. Comba 得名.上面的笔算乘法,虽然比较简单, 但是有个很大的问题:在 O(n^2) 的复杂度上进行计算和向上传递进位,看看前面的那个竖 ...

  3. BZOJ3809:Gty的二逼妹子序列

    浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...

  4. Spring Boot基本配置

    本文参考javaEE开发的颠覆者SpringBoot实战第一版 基本配置 入口类和@SpringBootApplication Spring Boot通常有一个名为*Application的入口类,且 ...

  5. UEditor富文本编辑器的图片上传 http://fex.baidu.com/ueditor/#server-deploy

    http://fex.baidu.com/ueditor/#server-deploy http://fex.baidu.com/ueditor/#server-path 首先 editor配置文件中 ...

  6. DB字段顺序与类的属性顺序一致:{Oracle.DataAccess.Client.OracleException ORA-00932: 数据类型不一致: 应为 TIMESTAMP, 但却获得 NUMBER

    {Oracle.DataAccess.Client.OracleException ORA-00932: 数据类型不一致: 应为 TIMESTAMP, 但却获得 NUMBER     应用程序中类型T ...

  7. POJ1020(小正方形铺大正方形)

    Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16579   Accepted: 5403 ...

  8. (转)nodejs搭建本地http服务器

    本文转载自:http://www.cnblogs.com/shawn-xie/archive/2013/06/06/3121173.html 由于不做php相关的东西,懒得装apache,干脆利用no ...

  9. 【转】 Pro Android学习笔记(八三):了解Package(2):包签名过程

    目录(?)[-] 类比例子 数字签名 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在W ...

  10. sql语句中GROUP BY 和 HAVING的使用 count()

    在介绍GROUP BY 和 HAVING 子句前,我们必需先讲讲sql语言中一种特殊的函数:聚合函数, 例如SUM, COUNT, MAX, AVG等.这些函数和其它函数的根本区别就是它们一般作用在多 ...