poj3744 Scout YYF I
来源:互联网 发布:java构造方法的特点 编辑:程序博客网 时间:2024/05/18 03:33
Scout YYF I
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4023 Accepted: 1029
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 ofp, 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.
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].
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.522 0.52 4
Sample Output
0.50000000.2500000
Source
POJ Monthly Contest - 2009.08.23, Simon
就是一条路,有很多的地雷,要求不踩到上面的概率,我们可以很容易得到状态转移方程 ,dp[i]=dp[i-1]*p+dp[i-2]*(1-p),但是,我们发现,n是很大的,我们只能用矩阵快速幂才行,这样,我们能快速求解,我们可以用雷把所有的路分成一段一段的,这样, 我们,要求不踩到地雷,不就是每一段都不踩到地雷,那么,我们,就可以能过求,每一段刚开踩到地雷上,再用1去减,这样,不就可以快速求解了么,问题也就解决了!原本,我们考虑了很多特殊情况,反倒还错了不少次!…….
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;int num[11];double p;struct matrix { double a[2][2]; matrix operator * (matrix x)const { int i,j,k; matrix temp; for(i=0;i<2;i++) for(j=0;j<2;j++) { temp.a[i][j]=0; for(k=0;k<2;k++) temp.a[i][j]+=a[i][k]*x.a[k][j]; } return temp; }};matrix original,temp;double fcose(int n){ original.a[0][0]=original.a[1][1]=1; original.a[0][1]=original.a[1][0]=0; temp.a[0][0]=p;temp.a[0][1]=1; temp.a[1][0]=1-p;temp.a[1][1]=0; while(n) { if(n&1) original=original*temp; temp=temp*temp; n=n>>1; } return 1-original.a[0][0];}bool cmp(int a,int b){ return a<b;}int main(){ int n,last,i; double result ; while(scanf("%d%lf",&n,&p)!=EOF) { result=1; for(i=1;i<=n;i++) scanf("%d",&num[i]); sort(num+1,num+n+1,cmp); result=fcose(num[1]-1); bool flag=true; for(i=2;i<=n;i++) { if(num[i]==num[i-1]) continue; result*=fcose(num[i]-num[i-1]-1); } printf("%.7f\n",result); } return 0;}
- poj3744 Scout YYF I
- poj3744 Scout YYF I
- POJ3744----Scout YYF I
- 【POJ3744】【Scout YYF I】
- 【POJ3744】Scout YYF I
- poj3744 Scout YYF I 概率
- POJ3744-Scout YYF I--概率DP
- [POJ3744]Scout YYF I 期望DP
- poj3744 Scout YYF I 概率dp+矩阵快速幂
- poj3744--Scout YYF I(概率dp第五弹:矩阵优化)
- POJ3744 Scout YYF I (概率DP + 矩阵优化)
- 【POJ3744】Scout YYF I-概率DP+矩阵加速优化
- POJ3744:Scout YYF I(概率dp & 矩阵优化)
- POJ3744 Scout YYF I ( 矩阵快速幂 + 期望概率DP )
- poj3744 Scout YYF I 【概率dp+矩阵优化】
- poj3744——Scout YYF I(概率DP+矩阵快速幂)
- poj-Scout YYF I
- Scout YYF I
- 大数据处理技术 - 基于Hadoop的实战
- Oracle instr函数
- POI生成excel文件,自定义单元格颜色
- 位带操作
- IOS百度地图开发系列-控件的圆角设置
- poj3744 Scout YYF I
- 机房收费系统(一)
- sleep函数
- 6410之驱动程序的分层分离,总线设备驱动模型
- 进制转换
- 那迷们
- Android Sensor传感器系统架构初探
- TranslateAnimation详解
- .NET 中对 PHP、Binary、SOAP、XML、JSON、Hessian 等序列化的效率对比