【模拟试题】Scout YYF I

【模拟试题】Scout YYF I

Description

YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of p, or jump two step with a probality of 1-p. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely. 
题意:
你在一条布满地雷的道路上,开始在坐标1。每次有概率P向前走一步,有概率1-P向前走两步。道中路某几个点上会有地雷,问你安全通过的概率。地雷数N<=10,坐标范围在100000000内。


Input

The input contains many test cases ended with EOF.
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].

Output

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point

Sample Input

1 0.5

2

2 0.5

2 4

Sample Output

0.5000000

0.2500000

Solution

        这道题题面意思非常裸,但是难度在于较大的范围,暴力的线性递推会超时。

        递推:f[i]=f[i-1]*p+f[i-2]*(1.0-p),地雷为0.

        这道题还有一个显著的特点,极少的地雷数,所以我们不妨从中入手。在两个雷之间的大量递推是没有意义的,标解是用快速幂加快过程,但我听某大牛说这种只有两种选择的操作多做几遍后每一格的概率是相同的,具体次数取决于数据。(汗。。。)在蒟蒻的多次尝试下,一份诡异的代码出现了:

Source

#include<iostream>
using namespace std;
int P[15],n;
double p,ans,t,a1,a2;
inline void dp(){t=a2;a2=a1*(1-p)+t*p;a1=t;return;}
int main() {
	int i,j;
	while(scanf("%d%lf",&n,&p)!=EOF){
		P[0]=0;ans=1;
		for(i=1;i<=n;i++)scanf("%d",&P[i]);
		for(i=0;i<n;i++){
			if(i&&P[i+1]-P[i]==1){ans=0;break;}
			a1=0;a2=1;//通过分段记录概率减小误差 
			if(P[i+1]-P[i]<=30)
				for(j=P[i]+1;j<P[i+1];j++)dp(); 
			else 
				for(j=0;j<30;j++)dp();//诡异的30 
			ans*=(1-a2);
		}
		printf("%.7lf\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值