传送门
Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 470 Accepted Submission(s): 323
Special Judge
Problem Description
You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form:
0 m n: ask for the expected number of tosses until the last n times results are all same.
1 m n: ask for the expected number of tosses until the last n consecutive results are pairwise different.
Input
The first line contains a number T.(1≤T≤100) The next T line each line contains a query as we mentioned above. (1≤m,n≤106) For second kind query, we guarantee n≤m. And in order to avoid potential precision issue, we guarantee the result for our query will not exceeding 109 in this problem.
Output
For each query, output the corresponding result. The answer will be considered correct if the absolute or relative error doesn’t exceed 10-6.
Sample Input
6
0 6 1
0 6 3
0 6 5
1 6 2
1 6 4
1 6 6
10
1 4534 25
1 1232 24
1 3213 15
1 4343 24
1 4343 9
1 65467 123
1 43434 100
1 34344 9
1 10001 15
1 1000000 2000
Sample Output
1.000000000
43.000000000
1555.000000000
2.200000000
7.600000000
83.200000000
25.586315824
26.015990037
15.176341160
24.541045769
9.027721917
127.908330426
103.975455253
9.003495515
15.056204472
4731.706620396
Source
2013 Multi-University Training Contest 5
题目大意:
给你一个骰子,这个骰子一共有 m 个面,给你一个操作数op,如果 op == 0的话让你求的就是最后扔的骰子的 n 个连续相同的扔的次数的期望,如果 op == 1的话就是让你求的 n 个连续不同的扔的次数的期望。
解题思路:
看到这个题的话,第一反应肯定就是概率DP,然后肯定是分两种情况:
在第一种情况的条件下(n个连续相同):
设 DP[i]表示的就是已经有 i 个 连续相同的面了,然后到 n 个连续相同的面的期望。
那么可以分析一下DP[n]一定是0;
然后左边相加,右边相加,左边得到的就是DP[0]-DP[n]
右边得到的就是1+m+m^2+..+m^(n-1)(等比数列求和公式可得)
在第二种情况的条件下(n个连续不相同):
设 DP[i]表示的就是已经有 i 个 连续不相同的面了,然后到 n 个连续不相同的面的期望。
这里直接给一下公式吧:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
int m, n;
double Solve0()
{
double ans = pow(m,n);
return 1.0*(ans-1)/(m-1);
}
double Solve1()
{
double sum = 0.0, ans = 1.0;
for(int i=1; i<=n; i++)
{
ans *= 1.0*m/(m-i+1);
sum += ans;
}
return sum;
}
int main()
{
int T, op;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d%d%d",&op,&m,&n);
if(op == 0)
printf("%.8lf\n",Solve0());
else if(op == 1)
printf("%.8lf\n",Solve1());
}
}
return 0;
}