hdu 4762 Cut the Cake (大数乘法)

时间:2022-06-26 12:36:27

猜公式:

  ans=n/m^(n-1)

#include<stdio.h>
#include<string.h>
struct BigNum
{
int num[];
int len;
};
int gcd(int a,int b)
{
if(b==)
return a;
return gcd(b,a%b);
}
BigNum mul(BigNum &a,int b)
{
BigNum c;
int i,len;
len=a.len;
memset(c.num,,sizeof(c.num));
if(b==)
{
c.len=;
return c;
}
for(i=; i<len; i++)
{
c.num[i]+=(a.num[i]*b);
if(c.num[i]>=)
{
c.num[i+]=c.num[i]/;
c.num[i]%=;
}
}
while(c.num[len]>)
{
c.num[len+]=c.num[len]/;
c.num[len++]%=;
}
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[]=;
s.len=;
a=n;
for(i=; i<n; i++)
{
b=m;
c=gcd(a,m);
a/=c;
b/=c;
s=mul(s,b);
}
printf("%d/",a);
for(i=s.len-; i>=; i--)
printf("%d",s.num[i]);
printf("\n");
}
return ;
}