123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package me.yoqi;
- import java.math.BigInteger;
- public class Test {
- public static void main(String args[]) {
- int init = 25;
- double pk = 0;
- for (int n = 3; n < init; n++) {
- BigInteger nn = new BigInteger(String.valueOf(Math.pow(365, n)));
- long temp = (combination(365, n).multiply(factorial(n)).divide(nn)).longValue();
- pk = 1 - temp;
- if (pk >= 0.5) {
- System.out.println("最小K:" + n);
- break;
- }
- }
- System.out.print(factorial(25));
- }
- /**
- * 计算阶乘数,即n! = n * (n-1) * ... * 2 * 1
- *
- * @param n
- * @return
- */
- private static BigInteger factorial(int n) {
- BigInteger nn=new BigInteger(Integer.toString(n));
- return (BigInteger) ((n > 1) ? factorial(n - 1).multiply(nn) : 1);
- }
- /**
- * 计算排列数,即A(n, m) = n!/(n-m)!
- *
- * @param n
- * @param m
- * @return
- */
- public static BigInteger arrangement(int n, int m) {
- return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)) : 0);
- }
- /**
- * 计算组合数,即C(n, m) = n!/((n-m)! * m!)
- *
- * @param n
- * @param m
- * @return
- */
- public static BigInteger combination(int n, int m) {
- return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)).divide(factorial(m)) : 0);
- }
- }
|