【HDOJ 4762】 Cut the Cake (数学概率+大数)

来源:互联网 发布:有声读书软件 编辑:程序博客网 时间:2024/05/29 04:06

【HDOJ 4762】 Cut the Cake (数学概率+大数)


Cut the Cake

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


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
23 33 4
 

Sample Output
1/34/27
 


n个草莓撒在蛋糕上 蛋糕分成m块 问草莓在同一块的概率


高中的不同球放不同盒子 球都在同一盒子的概率问题


先考虑第一颗草莓 放在某块蛋糕上的概率是1/m 其余n-1块草莓跟他在一起的概率是1/m^(n-1) 由于第一颗草莓可以选 所以*n 蛋糕也可以选 *m 

化简就是 n/(m^(n-1)) 由于大数据是20 20 20^19会爆 所以上高精度 模板/JAVA


代码如下:

import java.util.*;import java.math.*;public class Main{public static void main(String[] args){Scanner in = new Scanner(System.in);BigInteger a,b,c;int n,m;int t=in.nextInt();while(t-->0){m=in.nextInt();n=in.nextInt();a=BigInteger.valueOf(n);b=BigInteger.valueOf(1);for(int i=1;i<n;i++)b=b.multiply(BigInteger.valueOf(m));BigInteger g=a.gcd(b);System.out.println(a.divide(g)+"/"+b.divide(g));}}}



0 0
原创粉丝点击