题意: 求 ,要求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. CodeForces - 438D: The Child and Sequence(势能线段树)

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  2. tests

    test

  3. PCM音量控制

    http://blog.jianchihu.net/pcm-volume-control.html 一.声音的相关概念 声音是介质振动在听觉系统中产生的反应.声音总可以被分解为不同频率不同强度正弦波的 ...

  4. 表有外键所以delete报错了,这里有2种办法处理:

    表有外键所以delete报错了,这里有2种办法处理: (1)      临时设置外键失效 (2)      删除表涉及到的外键的表的数据 2.外键失效的处理方案 mysql> SET FOREI ...

  5. 设置eclipse中jsp/html文件好看的自动排版

    注:本文转载于ieayoio,原文链接:https://blog.csdn.net/ieayoio/article/details/49930587#8912689 eclipse中jsp文件代码的排 ...

  6. iOS 【资源篇】

    iOS9开发入门教程索引 Objective-C视频教程 O-c Blog 社区 畅游  http://www.9ria.com/ 苹果中文开发社区  http://www.cocoachina.co ...

  7. JSF中使用f:ajax标签无刷新页面改变数据

    ajax本是用在前端的一种异步请求数据的操作,广泛用于js中,一般的js框架如jq都有被封装好的方法,用于发起异步请求操作.异步操作可以增强用户体验和操作,越来越多的程序都在使用ajax.JSF的fa ...

  8. 人脸识别FaceNet+TensorFlow

    一.本文目标 利用facenet源码实现从摄像头读取视频,实时检测并识别视频中的人脸.换句话说:把facenet源码中contributed目录下的real_time_face_recognition ...

  9. Task用法(1)-启动方法

    第一.基本使用 Thread,ThreadPool,Task的区别 Task是.NET4.0加入的,跟线程池ThreadPool的功能类似,用Task开启新任务时,会从线程池中调用线程,而Thread ...

  10. WCF宿主Window Service Demo

    尝试了下将服务寄宿在window 服务上.具体步骤如下 整个解决方案截图 一.创建window 服务 Wcf.WinService namespace Wcf.WinService { public ...