HDOJ 4762 Cut the Cake(概率+Java)
2015-09-09 22:14
477 查看
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
Sample Output
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都很大所以上Javaimport 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)); } } }
相关文章推荐
- Java—double类型不以科学计数法表示
- springmvc上传临时文件目录配置
- think in java 学习笔记(一)方法重载
- Java.的包装类
- java job 资源
- hdu 1085 Holding Bin-Laden Captive!(Java)
- 设计模式笔记
- springmvc+spring4+hibernate的删除和更新方法无效,已经解决
- 专访黄勇:Java在未来的很长一段时间仍是主流
- JDK源代码研究 TreeSet TreeMap 红黑树算法实现
- java编程中Properties类的具体作用和使用!
- Java Date API demo
- <LeetCode(Java版)>String to Integer
- 学习java与思维导图
- Java参数按值传递和按引用传递
- java 实现倒计时的效果
- Android Studio(七):项目从Eclipse到Android Studio迁移
- hdu 1002 A + B Problem II(Java)
- Android Studio(七):项目从Eclipse到Android Studio迁移
- 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(下)