分段的概率DP+矩阵快速幂
Scout YYF I
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4180 | Accepted: 1076 |
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
POJ Monthly Contest - 2009.08.23, Simon
如果不用快速幂(TLE的)。。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,mine[];
double p,d1,d2,d3,ans; int main()
{
while(scanf("%d%lf",&n,&p)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",mine+i);
}
sort(mine,mine++n);
if(mine[]==)
{
printf("0.0000000\n"); continue;
}
else if(n==)
{
printf("1.0000000\n"); continue;
}
bool flag=false;
for(int i=;i<n;i++)
{
if(mine[i]+==mine[i+])
{
printf("0.0000000\n"); flag=true; break;
}
}
if(flag==true) continue;
ans=.;
for(int i=;i<=n;i++)
{
int st=mine[i-]+,ed=mine[i];
d2=.,d1=.;
for(int j=st+;j<=ed;j++)
{
d3=d1*p+d2*(-p);
d2=d1; d1=d3;
}
ans*=(-d3);
}
printf("%.7lf\n",ans);
}
return ;
}
快速幂的。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct Matrix
{
double a[][];
Matrix() {}
Matrix(double A,double B,double C,double D)
{
a[][]=A;a[][]=B;a[][]=C;a[][]=D;
}
Matrix operator* (const Matrix& b) const
{
Matrix temp;
memset(temp.a,,sizeof(temp.a));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
temp.a[i][j]+=a[i][k]*b.a[k][j];
}
}
}
return temp;
}
Matrix Show()
{
for(int i=;i<;putchar(),i++) for(int j=;j<;putchar(' '),j++) cout<<a[i][j];
}
}; Matrix QuickPow(Matrix m,int n)
{
Matrix E(,,,);
while(n>)
{
if(n&) E=E*m;
m=m*m;
n=n>>;
}
E=E*m;
return E;
} int n,mine[];
double p,ans; int main()
{
while(scanf("%d%lf",&n,&p)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",mine+i);
sort(mine,mine+n+);
if(mine[]==)
{
printf("0.0000000\n"); continue;
}
else if(n==)
{
printf("1.0000000\n"); continue;
}
bool flag=false;
for(int i=;i<n;i++)
{
if(mine[i]+==mine[i+])
{
printf("0.0000000\n"); flag=true; break;
}
}
if(flag==true) continue;
ans=.;
for(int i=;i<=n;i++)
{
Matrix m(p,-p,,);
m=QuickPow(m,mine[i]-mine[i-]-);
ans*=-m.a[][];
}
printf("%.7lf\n",ans);
}
return ;
}