Problem Description
A little girl loves programming competition very much. Recently, she has found a new kind of programming competition named "TopTopTopCoder". Every user who has registered in "TopTopTopCoder" system will have a rating, and the initial value of rating equals
to zero. After the user participates in the contest held by "TopTopTopCoder", her/his rating will be updated depending on her/his rank. Supposing that her/his current rating is X, if her/his rank is between on 1-200 after contest, her/his rating will be min(X+50,1000).
Her/His rating will be max(X-100,0) otherwise. To reach 1000 points as soon as possible, this little girl registered two accounts. She uses the account with less rating in each contest. The possibility of her rank between on 1 - 200 is P for every contest.
Can you tell her how many contests she needs to participate in to make one of her account ratings reach 1000 points?
Input
There are several test cases. Each test case is a single line containing a float number P (0.3 <= P <= 1.0). The meaning of P is described above.
Output
You should output a float number for each test case, indicating the expected count of contest she needs to participate in. This problem is special judged. The relative error less than 1e-5 will be accepted.
Sample Input
1.000000
0.814700
Sample Output
39.000000
82.181160
题意:一个人打cf,赢一场涨50分,输一场掉100分,用两个号打比赛,每次用分低的打,问其中一个号打到1000分的期望场数
思路:首先可以离散为赢一场涨1分,输一场掉2分,打到20分的期望场数;
由于两个号毫无相关,一个号打到20分另一个号肯定是19分,所以只需要考虑一个号;
dp[i]表示从i分涨到i+1分的期望场数,则有:
dp[i]=p*1+(1-p)*(1+dp[i-2]+dp[i-1]+dp[i]);
i=1,i=2预处理
这个方程理解为:如果赢了只需要1场,如果输了需要浪费1场,由于掉到了i-2分,需要再加上dp[i-2]+dp[i-1]+dp[i]场打到i+1分。
稍微整理可得动态转移方程:dp[i]=(1+q*dp[i-1]+q*dp[i-2])/p;(q=1-p)
#include <bits/stdc++.h>
using namespace std;
double dp[10005][125];
double p[125][125];
int num[10005];
int main()
{
double p;
while(cin >> p)
{
double dp[20];
double q=1-p;
dp[0]=1/p;
dp[1]=(1+q*dp[0])/p;
double sum=2*(dp[0]+dp[1]);
for(int i=2;i<20;i++)
{
dp[i]=(1+q*dp[i-1]+q*dp[i-2])/p;
sum+=2*dp[i];
}
printf("%.6f\n",sum-dp[19]);
}
return 0;
}
//dp[i]=p*1+(1-p)*(dp[i-2]+dp[i-1]+dp[i]+1);