首页 > ACM题库 > HDU-杭电 > HDU 4762-Cut the Cake-概率-[解题报告]HOJ
2015
09-17

HDU 4762-Cut the Cake-概率-[解题报告]HOJ

Cut the Cake

问题描述 :

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.

输入:

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)

输出:

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)

样例输入:

2
3 3
3 4

样例输出:

1/3
4/27

题目链接:

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

 

题意:

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

 

分析:

我们从中先取出一个来有n种选择,然后剩下的n-1个草莓在其极角[0,360/m]的范围内。

一个的概率为1/m,n-1个的概率为1/m^(n-1);

因此总的概率为n/m^(n-1).,因为20^20超过了longlong, 需要用高精度

 

代码如下:

//package fuck;

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	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(t>0){
        	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));
        	t--;
        }
    }  
}  

 

 

参考:http://blog.csdn.net/bigbigship/article/details/45576991