Cut the Cake
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 558 Accepted Submission(s): 274
Problem Description
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly one of her friends HZ took some (N) strawberries which MMM likes very much to decorate the cake (of course they also eat strawberries, not just for decoration). HZ is in charge of the decoration, and he thinks that it's not a big deal that he put the strawberries on the cake randomly one by one. After that, MMM would cut the cake into M pieces of sector with equal size and shape (the last one came to the party will have no cake to eat), and choose one piece first. MMM wants to know the probability that she can get all N strawberries, can you help her? As the cake is so big, all strawberries on it could be treat as points.
Input
First line is the integer T, which means there are T cases.
For each case, two integers M, N indicate the number of her friends and the number of strawberry.
(2 < M, N <= 20, T <= 400)
For each case, two integers M, N indicate the number of her friends and the number of strawberry.
(2 < M, N <= 20, T <= 400)
Output
As the probability could be very small, you should output the probability in the form of a fraction in lowest terms. For each case, output the probability in a single line. Please see the sample for more details.
Sample Input
2 3 3 3 4
Sample Output
1/3 4/27
Source
Recommend
liuyiding
明显如果要最优只能是某块草莓处在左边界上,其余草莓在这块蛋糕的概率就是(1/M)^(N-1),有N块草莓,就是N*(1/M)^(N-1)。主要是高精度的调试。
明显如果要最优只能是某块草莓处在左边界上,其余草莓在这块蛋糕的概率就是(1/M)^(N-1),有N块草莓,就是N*(1/M)^(N-1)。主要是高精度的调试。
#include <cstdio>
#include <cstring>
class Bignum
{
private:
int size;
int num[50];
public:
Bignum()
{
size = 0;
memset(num,0,sizeof num);
}
Bignum(int a)
{
size = 0;
while (a)
{
num[++size] = a%10;
a /= 10;
}
}
Bignum& operator=(const Bignum& bb)
{
memset(num,0,sizeof num);
size = bb.size;
for (int i=1;i<=size;i++)
num[i] = bb.num[i];
return *this;
}
void operator*=(const Bignum& bb)
{
Bignum tmp;
tmp.size = size + bb.size;
for (int i=1;i<=size;i++)
{
for (int j=1;j<=bb.size;j++)
{
tmp.num[i+j-1] += num[i]*bb.num[j];
while (tmp.num[i+j-1]>9)
{
tmp.num[i+j] += tmp.num[i+j-1]/10;
tmp.num[i+j-1] %= 10;
}
}
}
while (!tmp.num[tmp.size])tmp.size--;
*this = tmp;
}
int operator%(int bb)const
{
int left = 0;
for (int i=size;i>=1;i--)
left = (left*10+num[i])%bb;
return left;
}
Bignum operator/(const int bb)
{
Bignum ans;
int left = 0;
ans.size = size;
for (int i=1;i<=size;i++)
{
ans.num[i] = (left*10+num[size-i+1]);
left = ans.num[i] % bb;
ans.num[i] /= bb;
}
for (int i=1;i<=ans.size/2;i++)
{
int tmp = ans.num[ans.size-i+1];
ans.num[ans.size-i+1] = ans.num[i];
ans.num[i] = tmp;
}
while (!ans.num[ans.size])ans.size--;
return ans;
}
void output()
{
for (int i=size;i>=1;i--)
printf("%d",num[i]);
}
};
Bignum MM;
void quickpower(Bignum& b,int k)
{
b = Bignum(1);
while (k)
{
if (k&1) b *= MM;
MM *= MM;
k >>= 1;
}
}
int gcd(int a,int b)
{
while (b)
{
int tmp = a%b;
a = b;
b = tmp;
}
return a;
}
int gcd(const Bignum aa,int b)
{
int tmp = aa%b;
int a = b;
b = tmp;
if (!b) return a;
while (b)
{
tmp = a%b;
a = b;
b = tmp;
}
return a;
}
int main()
{
//freopen("cake.in","r",stdin);
//freopen("cake.out","w",stdout);
int T;
scanf("%d",&T);
while (T--)
{
int m,n;
scanf("%d%d",&m,&n);
Bignum M = Bignum(m);
MM = M;
quickpower(M,n-1);
int GCD = gcd(M,n);
printf("%d/",n/GCD);
(M/GCD).output();
printf("\n");
}
return 0;
}