hdu 4762 Cut the Cake (大数乘法)

猜公式:

  ans=n/m^(n-1)

#include<stdio.h>

#include<string.h>

struct BigNum

{

    int num[100];

    int len;

};

int gcd(int a,int b)

{

    if(b==0)

        return a;

    return gcd(b,a%b);

}

BigNum mul(BigNum &a,int b)

{

    BigNum c;

    int i,len;

    len=a.len;

    memset(c.num,0,sizeof(c.num));

    if(b==0)

    {

        c.len=1;

        return c;

    }

    for(i=0; i<len; i++)

    {

        c.num[i]+=(a.num[i]*b);

        if(c.num[i]>=10)

        {

            c.num[i+1]=c.num[i]/10;

            c.num[i]%=10;

        }

    }

    while(c.num[len]>0)

    {

        c.num[len+1]=c.num[len]/10;

        c.num[len++]%=10;

    }

    c.len=len;

    return c;

}

int main()

{

    int t,m,n,i,a,b,c;

    BigNum s;

    scanf("%d",&t);

    while(t--)

    {

        scanf("%d%d",&m,&n);

        s.num[0]=1;

        s.len=1;

        a=n;

        for(i=1; i<n; i++)

        {

            b=m;

            c=gcd(a,m);

            a/=c;

            b/=c;

            s=mul(s,b);

        }

        printf("%d/",a);

        for(i=s.len-1; i>=0; i--)

            printf("%d",s.num[i]);

        printf("\n");

    }

    return 0;

}

 

你可能感兴趣的:(HDU)