Cut the Cake(大数)

本文解析了一道经典的概率题目,描述了如何计算在将N颗草莓随机放置于蛋糕上后,平均分配给M个人时,一个人获得所有草莓的概率。通过数学公式和编程实现的方式给出了详细的解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem   Link:http://acm.hdu.edu.cn/showproblem.php?pid=4762



Cut the Cake

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 849    Accepted Submission(s): 412


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)
 

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   |   We have carefully selected several similar problems for you:   4988  4987  4986  4985  4984 
 
code:


#include<stdio.h>
#include<string.h>
int gcd(int a,int b)
{
if(b==0)
return a;
gcd(b,a%b);
}
int main()
{
int t,m,n,a[30],b1,b2,i,j,k,cnt,c;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d%d",&m,&n);
memset(a,0,sizeof(a));
a[1]=1;
b1=n;
cnt=1;
for(i=1;i<=n-1;i++)
{     
   b2=m;
k=gcd(b1,b2);
b1/=k;
b2/=k;
c=0;
for(j=1;j<=cnt;j++)
{
a[j]=a[j]*b2+c;
c=a[j]/10;
a[j]%=10;
}
while(c)
{
a[++cnt]=c%10;
c/=10;
}
}
//for(i=cnt;i>=1;i--)
// printf("%d ",a[i]);
//printf("\n");
printf("%d/",b1);
for(i=cnt;i>=1;i--)
printf("%d",a[i]);
printf("\n");
}
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值