UVA 10288 Coupons

#include <iostream>
#include <stdio.h>
#include <cstring>
#define N 34
typedef long long LL;
using namespace std;

struct T{
    LL zs,fz,fm;
    T()
    {
        zs = 0;
        fz = 0;
        fm = 1;
    }
    T(LL a, LL b, LL c)
    {
        zs = a;
        fz = b;
        fm = c;
    }
    void Add(struct T t)
    {
        zs += t.zs;
        fz = fz*t.fm+t.fz*fm;
        fm = fm*t.fm;
        Standard();
    }
    void Standard()
    {
        zs += fz/fm;
        fz = fz%fm;
        LL g=gcd(fz,fm);
        fz = fz/g;
        fm = fm/g;
    }
    LL gcd(LL a, LL b)
    {
        return b==0?a:gcd(b,a%b);
    }
};

struct T ans[N];

void Init()
{
    for(int i=1;i<N;i++)
    {
        for(int j=1;j<=i;j++)
        {
            ans[i].Add(T(0,i,j));
        }
    }
}

int Len(LL a)
{
    int len=0;
    while(a)
    {
        len++;
        a /= 10;
    }
    return len;
}

void Print(struct T t)
{
    int i;
    if(t.fz==0)
        printf("%lld\n", t.zs);
    else
    {
        for(i=0;i<Len(t.zs)+1;i++)
            printf(" ");
        printf("%lld\n", t.fz);
        printf("%lld ", t.zs);
        for(i=0;i<Len(t.fm);i++)
            printf("-");
        printf("\n");
        for(i=0;i<Len(t.zs)+1;i++)
            printf(" ");
        printf("%lld\n", t.fm);
    }
}

int main()
{
    int n;
    Init();
    while(scanf("%d",&n)!=EOF)
    {
        Print(ans[n]);
    }
    return 0;
}

 

posted @ 2015-10-06 02:33  屠一刀  阅读(204)  评论(0编辑  收藏  举报