HDOJ 4762 Cut the Cake(概率+Java)


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
题目大意:求将一个蛋糕平均分为M分,将N个草莓随机的撒在蛋糕上,求拿到一块蛋糕上有n个草莓的概率。
思路:根据关系可以得公式为n/(m^(n-1))由于nm都很大所以上Java
import java.util.*;
import java.math.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner cin=new Scanner(System.in);
        BigInteger a,b;
        int n,m;
        int cla;
        cla=cin.nextInt();
        while(cla-->0)
        {
            m=cin.nextInt();
            n=cin.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值