poj3744 Scout YYF I

Scout YYF I
Time Limit: 1000MS   Memory Limit: 65536K
Total 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 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

就是一条路,有很多的地雷,要求不踩到上面的概率,我们可以很容易得到状态转移方程 ,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;

}



 

你可能感兴趣的:(poj)