HDU 4762 Cut the Cake

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4762

题目大意:将n个草莓随机放在蛋糕上,草莓被看做是点,然后将蛋糕平均切成m份,求所有草莓在同一块蛋糕上的概率

Sample Input
2
3 3
3 4
 
Sample Output
1/3
4/27

分析:计算出这个概率公式为:n/(mn-1),m、n最大为20,mn超出了64位,用到大数,用java容易写出

代码如下:

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static void main(String args[])
 6     {
 7         Scanner cin=new Scanner(System.in);
 8         int test=cin.nextInt();
 9         while(test-->0)
10         {
11             BigInteger m;
12             m=cin.nextBigInteger();
13             int n=cin.nextInt();
14             m=m.pow(n-1);
15             BigInteger nn=BigInteger.valueOf(n);
16             BigInteger p=m.gcd(nn);
17             System.out.println(nn.divide(p)+"/"+m.divide(p));
18         }
19     }
20 }
原文地址:https://www.cnblogs.com/acm-bingzi/p/3346603.html