HDU-4762 Cut the Cake(大数,概率)

本文详细解析了CuttheCake算法,这是一个关于将圆形蛋糕平均分割并计算特定装饰物分布概率的问题。通过数学推导和Java代码实现,展示了如何计算特定条件下草莓恰好落在同一份蛋糕上的概率。

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

Cut the Cake

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


 

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

2013 ACM/ICPC Asia Regional Changchun Online

 

 

Recommend

liuyiding

 

 

Statistic | Submit | Discuss | Note

题意:将一个原型蛋糕分成m份扇形,然后使n个草莓恰好在其中的一份上

思路:我们从中先取出一个来有n种选择,然后剩下的n-1个草莓在其极角[0,360/m]的范围内。一个的概率为1/m,n-1个的概率为1/m^(n-1)。因此总的概率为n/m^(n-1)。因为20^20超过了longlong, 需要用大数。

AC代码:

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
	public static BigInteger gcd(BigInteger a,BigInteger b)
	{
		if(!b.equals(BigInteger.ZERO)) return gcd(b,a.mod(b));
		return a;
	}
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int t=cin.nextInt();
		while(cin.hasNext())
		{
			int m=cin.nextInt();
			int n=cin.nextInt();
			BigInteger N=BigInteger.valueOf(n);
			BigInteger M=BigInteger.valueOf(m).pow(n-1);
			BigInteger tmp = gcd(N,M);
			System.out.println(N.divide(tmp)+"/"+M.divide(tmp));
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值