poj 3744 Scout YYF I (矩阵快速幂+概率dp)

Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4374   Accepted: 1141

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].

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


思路来源于:博博(题解推荐)

题意:

你在一条布满地雷的道路上,开始在坐标1。每次有概率P向前走一步,有概率1-P向前走两步。道中路某几个点上会有地雷,问你安全通过的概率。地雷数N<=10,坐标范围在100000000内。


思路:

第一次接触矩阵快速幂,其实就是二进制的思想吧,留个纪念。

dp[i]=dp[i-1]*p+dp[i-2]*(1-p);

构造矩阵

| P ,1-P |
| 1 , 0    |

那么有

|dp[n]   |           |p      1-p|^n-1     |dp[1]|

                  =                           *

|dp[n-1]|          |1          0|            |dp[0]|

然后将每个a[i]+1到a[i+1]看做一段单独处理就行。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define MAXN 100005
#define OO (1<<31)-1
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,tot,flag;
double p,ans;
int a[15];

struct Matrix
{
    int row,col;//行列
    double v[3][3];
    Matrix operator*(Matrix &tt)//矩阵相乘。前面矩阵的列必须和后面矩阵的行相同
    {
        int i,j,k;
        Matrix temp;
        temp.row=row;
        temp.col=tt.col;
        for(i=0; i<row; i++)
            for(j=0; j<tt.col; j++)
            {
                temp.v[i][j]=0;
                for(k=0; k<col; k++)
                    temp.v[i][j]+=v[i][k]*tt.v[k][j];
            }
        return temp;
    }
};
Matrix pow_mod(Matrix x,int i)
{
    Matrix tmp;
    tmp.row=tmp.col=2;
    tmp.v[0][0]=tmp.v[1][1]=1;
    tmp.v[0][1]=tmp.v[1][0]=0;
    while(i)
    {
        if(i&1) tmp=tmp*x;
        x=x*x;
        i>>=1;
    }
    return tmp;
}
int main()
{
    int i,j,t;
    while(~scanf("%d%lf",&n,&p))
    {
        a[0]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n+1);
        if(a[1]==1)
        {
            ans=0;
            printf("%.7f\n",ans);
            continue ;
        }
        ans=1;
        for(i=1;i<=n;i++)
        {
            Matrix jj;
            jj.row=jj.col=2;
            jj.v[0][0]=p; jj.v[0][1]=1-p;
            jj.v[1][0]=1; jj.v[1][1]=0;
            t=a[i]-a[i-1]-1;
            Matrix yy=pow_mod(jj,t);
            ans*=(1-yy.v[0][0]);
        }
        printf("%.7f\n",ans);
    }
    return 0;
}
/*
2 0.5
2 2
2 0.5
2 4
*/







你可能感兴趣的:(poj 3744 Scout YYF I (矩阵快速幂+概率dp))