这题概率dp入门。。。怎么连入个门都要被卡qaq
明显 dp方程为
d[i]=p*d[i-1]+(1-p)d[i-2]
由于i很大。。所以直接转移会tle。。。所以用矩阵乘法优化就行了。。
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 300005
#define nm 100000005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
struct mat{
int n,m;
double a[3][3];
friend mat operator*(const mat&x,const mat&y){
mat s;s.n=x.n;s.m=y.m;mem(s.a);
inc(i,1,s.n)inc(j,1,s.m)inc(k,1,x.m)s.a[i][j]+=x.a[i][k]*y.a[k][j];
return s;
}
}ans,t,null;
int n,a[NM];
double p;
mat pow(mat t,int k){return k?pow(t*t,k>>1)*(k&1?t:null):null;}
int main(){
//freopen("data.in","r",stdin);
t.m=t.n=2;
t.a[1][1]=0;t.a[1][2]=1;
null.m=null.n=2;
null.a[1][1]=null.a[2][2]=1;
while(~scanf("%d %lf",&n,&p)){
t.a[2][1]=1-p;t.a[2][2]=p;
inc(i,1,n)a[i]=read();
sort(a+1,a+1+n);
if(a[1]==1){printf("%.7lf\n",0.0);continue;}
a[0]=0;
ans.n=2;ans.m=1;
ans.a[1][1]=0;ans.a[2][1]=1;
inc(i,1,n){
int k=a[i]-a[i-1]-2;
if(k<0){ans.a[2][1]=0;break;}
ans=pow(t,k)*ans;
//printf("%.7lf\n",ans.a[2][1]);
ans.a[2][1]*=(1-p);ans.a[1][1]=0;
}
printf("%.7lf\n",ans.a[2][1]);
}
return 0;
}
Scout YYF I
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10034 | Accepted: 2926 |
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.
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.5 2 2 0.5 2 4
Sample Output
0.5000000 0.2500000
Source
[Submit] [Go Back] [Status] [Discuss]