hdu 4762 - Cut the Cake(大数)

思路十分简单,就是计算一下 n / ((m)^(n-1));

代码如下:

int cnt,ans[100];

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
void cal(int x)
{
    for(int i = 0; i < cnt; ++i)
        ans[i] *= x;
    int t = 0;
    for(int i = 0; i < cnt; ++i)
    {
        ans[i] += t;
        t = ans[i]/10000;
        ans[i] %=10000;
    }
    while(t)
    {
        ans[cnt++] = t%10000;
        t /= 10000;
    }
}

int main()
{
    int t,n,m,z;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        memset(ans,sizeof(ans));
        z = n;
        cnt = 1;
        ans[0] = 1;
        for(int i = 0; i < n-1; ++i)
        {
            int g = gcd(m,z);
            z /= g;
            cal(m/g);
        }
        printf("%d/",z);
        for(int i = cnt-1; i >= 0; --i)
            i==cnt-1?printf("%d",ans[i]):printf("%.4d",ans[i]);
        printf("\n");
    }
    return 0;
}

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...