题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6568
Math
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 196 Accepted Submission(s): 25
Special Judge
Problem Description
Avin sells robots to clients. At second 0, Avin is at the location (0,0) on a number axis with a robot.
He wants to go to (L, 0) with the robot. He walks a unit distance per second, and he can only stop at integer coordinates. Now, he decides to follow these walking rules repeatedly until he arrives (L, 0) with the robot:
1) If Avin has the robot with himself, the robot may be dropped down with probability p.
2) If Avin had dropped the robot, he will figure it out with probability q. Specially, if Avin arrives at (L, 0) without robot, he will turn around immediately.
3) If Avin does not see that the robot had been dropped, he walks one step right; otherwise, he walks left until he is at the same location as the robot.
What is the expectation of walking time he needs to arrive (L, 0) with the robot?
He wants to go to (L, 0) with the robot. He walks a unit distance per second, and he can only stop at integer coordinates. Now, he decides to follow these walking rules repeatedly until he arrives (L, 0) with the robot:
1) If Avin has the robot with himself, the robot may be dropped down with probability p.
2) If Avin had dropped the robot, he will figure it out with probability q. Specially, if Avin arrives at (L, 0) without robot, he will turn around immediately.
3) If Avin does not see that the robot had been dropped, he walks one step right; otherwise, he walks left until he is at the same location as the robot.
What is the expectation of walking time he needs to arrive (L, 0) with the robot?
Input
One line with three numbers L, p and q, where L (1 ≤ L ≤ 100, 000) is an integer, p and q are real numbers with three digits and within (0, 1).
Output
Print the expected walking time. Your answer is considered correct if the absolute or relative error doesn’t exceed 1e6 . Formally, let your answer be a, and the jury’s answer be b. Your answer is considered correct if |a−b|max(1,|b|) ≤ 1e-6.
Sample Input
1
0.500
0.500
Sample Output
2.0000000000
解题思路:首先定义状态dp[i]为从i点出发,带着机器人到达下一个点所需要的期望。
那么dp[i] = 1-p+p*dp[i]+由于i点掉落机器人往返所需的期望值。接下来的关键就是求出这个期望值了c[i],设a[i]为走了i长度发现机器人掉落时,往返的时间。那么c[i] = ∑(1-p)^j*p*a[j]*2*j(注意到达L点一定会返回)
最后ans=dp[0]+dp[1]+...+dp[L-1]
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; const double eps=1e-8; double sum[maxn]; double dp[maxn]; double a[maxn]; int main(){ int l; double ans=0.0; double p,q;//掉落的概率 发现的概率 while(~scanf("%d%lf%lf",&l,&p,&q)){ for(int i=1;i<=l;i++){ a[i]=pow(1.0-q,i)*q*(2.0*i);//走了i长度发现机器人掉落时,往返的时间。 } sum[l+1]=0; for(int i=l;i>=0;i--){ sum[i]=sum[i+1]+a[l-i]; } double ans=0.0; for(int i=0;i<l;i++){ dp[i]=(1.0-p)+p*(sum[i]+pow(1.0-q,l-i+1)*(2*(l-i))); dp[i]=dp[i]/(1-p); ans+=dp[i]; // cout<<dp[i]<<" "<<endl; } printf("%.10lf ",ans); //cout<<pow(p,2); } return 0; }