码迷,mamicode.com
首页 > 其他好文 > 详细

2019CCPC-江西省赛 Math(概率dp )

时间:2019-07-28 20:04:00      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:one   pow   space   axis   tle   figure   git   tom   distance   

题目链接: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?
 

 

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 |ab|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\n",ans);
	//cout<<pow(p,2);
}	
	
	return 0;
} 

  

2019CCPC-江西省赛 Math(概率dp )

标签:one   pow   space   axis   tle   figure   git   tom   distance   

原文地址:https://www.cnblogs.com/Zhi-71/p/11260424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!