http://www.chenguanghe.com/spoj-favdice-favorite-dice/
http://www.spoj.com/problems/FAVDICE/
分析: Coupon collector’s problem 假设有6个面, 那么第一次扔的面肯定是我们想要的, 这时可以看成是6/6,即6个面中出现6个我们都想要的, 其中的一个面出现的概率是1/(6/6) = 1, 因为这时候没有其他面出现过. 第二次扔的面有两种情况, 一种是已经出现过, 1/6, 另一种是没有出现过5/6, 那么我们当然要没有出现过的, 就是5/6中选一个, 1/(5/6), 以此类推就是n*(h(n)), 然后算下调和函数h的值就好了
http://www.spoj.com/problems/FAVDICE/
BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorite? Well, his die is magical and can be transformed into an N-sided unbiased die with the push of a button. Now BuggyD wants to learn more about his die, so he raises a question:
What is the expected number of throws of his die while it has N sides so that each number is rolled at least once?
Input
The first line of the input contains an integer t, the number of test cases. t test cases follow.
Each test case consists of a single line containing a single integer N (1 <= N <= 1000) - the number of sides on BuggyD's die.
Output
For each test case, print one line containing the expected number of times BuggyD needs to throw his N-sided die so that each number appears at least once. The expected number must be accurate to 2 decimal digits.
Example
Input: 2 1 12 Output: 1.00 37.24题目大意: 给一个n面的骰子, 问扔多少次至少每个面出现一次
分析: Coupon collector’s problem 假设有6个面, 那么第一次扔的面肯定是我们想要的, 这时可以看成是6/6,即6个面中出现6个我们都想要的, 其中的一个面出现的概率是1/(6/6) = 1, 因为这时候没有其他面出现过. 第二次扔的面有两种情况, 一种是已经出现过, 1/6, 另一种是没有出现过5/6, 那么我们当然要没有出现过的, 就是5/6中选一个, 1/(5/6), 以此类推就是n*(h(n)), 然后算下调和函数h的值就好了
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
out.printFormat("%.2f\n",n*calculateH(n));
}
private double calculateH(int n) {
double res = 0.0;
for (double i = 1; i <=n; i++) {
res += 1/i;
}
return res;
}