题目链接:http://poj.org/problem?id=1707

题意:给出n

在M为正整数且尽量小的前提下,使得n的系数均为整数。

思路:

i64 Gcd(i64 x,i64 y)
{
    if(y==0) return x;
    return Gcd(y,x%y);
}

i64 Lcm(i64 x,i64 y)
{
    x=x/Gcd(x,y)*y;
    if(x<0) x=-x;
    return x;
}

struct fraction
{
    i64 a,b;

    fraction() {}
    fraction(i64 x)
    {
        a=x; b=1;
    }

    fraction(i64 x,i64 y)
    {
        a=x; b=y;
        deal();
    }

    void deal()
    {
        if(b<0) b=-b,a=-a;
        i64 k=Gcd(a,b);
        if(k<0) k=-k;
        a/=k; b/=k;
    }

    fraction operator+(fraction p)
    {
        fraction ans;
        ans.b=Lcm(b,p.b);
        ans.a=ans.b/b*a+ans.b/p.b*p.a;
        ans.deal();
        return ans;
    }

    fraction operator-(fraction p)
    {
        fraction ans;
        ans.b=Lcm(b,p.b);
        ans.a=ans.b/b*a-ans.b/p.b*p.a;
        ans.deal();
        return ans;
    }

    fraction operator*(fraction p)
    {
        fraction ans;
        ans.a=a*p.a;
        ans.b=b*p.b;
        ans.deal();
        return ans;
    }

    fraction operator/(fraction p)
    {
        fraction ans;
        ans.a=a*p.b;
        ans.b=b*p.a;
        ans.deal();
        return ans;
    }

    void print()
    {
        printf("%lld/%lld\n",a,b);
    }
};

fraction B[20];
i64 C[N][N];

void init()
{
    int i,j;
    for(i=1;i<N;i++)
    {
        C[i][0]=C[i][i]=1;
        for(j=1;j<i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];
    }
    B[0]=fraction(1);
    for(i=1;i<=20;i++)
    {
        B[i]=fraction(0);
        for(j=0;j<i;j++) B[i]=B[i]-fraction(C[i+1][j])*B[j];
        B[i]=B[i]/fraction(C[i+1][i]);
    }
}

int n;
fraction a[N];

int main()
{
    init();
    Rush(n)
    {
        i64 i,L=1;
        for(i=0;i<=n;i++)
        {
            a[i]=fraction(C[n+1][i])*B[i]*fraction(1,n+1);
            L=Lcm(L,a[i].b);
        }
        printf("%lld ",L);
        a[1]=a[1]+fraction(1);
        for(i=0;i<=n;i++) printf("%lld ",L/a[i].b*a[i].a);
        puts("0");
    }
}

  

POJ 1707 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 766 Sum of powers (伯努利数)

    题意: 求 ,要求M尽量小. 析:这其实就是一个伯努利数,伯努利数公式如下: 伯努利数满足条件B0 = 1,并且 也有 几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个 ...

  3. ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法

    POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Fo ...

  4. [CSAcademy]Sum of Powers

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

  5. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  6. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  7. Euler's Sum of Powers Conjecture

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

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

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

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

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

随机推荐

  1. spring-mysqlclient开源了

    https://github.com/risedragon/spring-mysqlclient/wiki/spring-mysqlclient-user-guide 开源了一个项目,总结了几年的数据 ...

  2. String对象中常用的方法

    String对象中常用的方法   1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...

  3. php用户注册

    前言 网站用户注册与登录是很常用的一个功能,本节教材就以此来演示一下 PHP 中如何开发用户注册与登录模块. 本节需要用到的重点 PHP 基础知识: PHP 中预定义 $_POST 和 $_GET 全 ...

  4. Linux - 升级+编译kernel

    For upgrading present kernel to linux-next kernel, we need to follow below steps. 1. Check present k ...

  5. object to primitive in javascript

    例1: var a={};  alert(a); //[object Object]; 例2: var a={ toString:function(){ return 1; } } alert(a); ...

  6. SQL Server DATEADD() 函数

    SQL Server Date 函数 定义和用法 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日 ...

  7. LintCode-Unique Path II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. res/drawable目录下图片的Uri

    http://liuyun025.iteye.com/blog/1280838 有时候,我们要用到res/drawable目录下的图片Uri,而这个Uri该如何生存呢?下面就是这Uri的生成方法: U ...

  9. How does java technology relate to cloud computing?

    Java Paas shootout   (@IBM developer) Cloud computing is always a hot topic around IT field today.Ho ...

  10. VS2010常用插件介绍

    今天在写JS时,写到500多行时,感觉代码已经很难看了.想到C#代码都有折叠功能,是不是JS也有呢.在选项中找了一下,没有相关了的设置功能,于是就上网找.一找可就不得了,发现了好多好用的插件.都可以在 ...