Yinyangshi is a famous RPG game on mobile phones.
Kim enjoys collecting cards in this game. Suppose there are n kinds of cards. If you want to get a new card, you need to pay W coins to draw a card. Each time you can only draw one card, all the cards appear randomly with same probability 1/n. Kim can get 1 coin each day. Suppose Kim has 0 coin and no cards on day 0. Every W days, Kim can draw a card with W coins. In this problem ,we define W=(n-1)!.
Now Kim wants to know the expected days he can collect all the n kinds of cards.
Input
The first line an integer T(1 ≤ T ≤ 10). There are T test cases.
The next T lines, each line an integer n. (1≤n≤3000)
Output
For each n, output the expected days to collect all the n kinds of cards, rounded to one decimal place.
Sample Input
4 1 2 5 9
Sample Output
1.0 3.0 274.0 1026576.0
题意:一开始你没有一张卡片,每隔(n-1)!天你可以抽一张卡片,抽中每张卡片的概率为1/n,现在你要抽取n张卡片,期望是多少?
分析:假设有n张卡片,你已经抽到了k张不同的卡片,则抽中(k+1)张不同的概率为:(n-k)/n,所以抽中(k+1)张的期望次数为n/(n-k),也就是平均抽n/(n-k)张才能抽到一张不同的卡片,期望即是平均值
即 E(k+1) = n/(n-k)
所以期望 E = (E(1)+E(2)+...+E(n))*(n-1)! = (n/n+n/(n-1)+...+n/1)*(n-1)! = n!/n + n!/(n-1) + ... + n!/1
参考博客:https://blog.csdn.net/hnust_xx/article/details/75807071
AC代码:
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger [] a = new BigInteger [3010]; a[1] = BigInteger.valueOf(1); for( int i = 2; i <= 3000; i ++ ) { a[i] = a[i-1].multiply(BigInteger.valueOf(i)); } Scanner cin = new Scanner(System.in); int T; T = cin.nextInt(); while( T > 0 ) { T --; int n; n = cin.nextInt(); BigInteger sum = BigInteger.valueOf(0); for( int i = 1; i <= n; i ++ ) { sum = sum.add(a[n].divide(BigInteger.valueOf(i))); } System.out.print(sum); System.out.println(".0"); } } }