Problem 2278 YYS (FZU + java大数)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2278

题目:

题意:

  有n种卡牌,每种卡牌被抽到的概率为1/n,求收齐所有卡牌的天数的期望。

思路:

  易推得公式为:

    

  由于n的范围太大,直接求阶乘会爆,所以我们得用大数来求~

代码实现如下:

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static void main(String args[]) {
 6         Scanner sc = new Scanner(System.in);
 7         int t;
 8         t = sc.nextInt();
 9         while((t--) != 0) {
10             int n = sc.nextInt();
11             BigInteger ans = BigInteger.ONE;
12             for(int i = 1; i <= n; i++) {
13                 ans = ans.multiply(BigInteger.valueOf(i));
14             }
15             BigInteger cnt = BigInteger.valueOf(0);
16             for(int i = 1; i <= n; i++) {
17                 BigInteger cnt1 = ans.divide(BigInteger.valueOf(i));
18                 cnt = cnt.add(cnt1);
19             }
20             System.out.println(cnt+".0");;
21         }
22         sc.close();
23     }
24 }
原文地址:https://www.cnblogs.com/Dillonh/p/9507588.html