首页 > 代码库 > poj3744--Scout YYF I(概率dp第五弹:矩阵优化)

poj3744--Scout YYF I(概率dp第五弹:矩阵优化)

Scout YYF I
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5093 Accepted: 1385

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.522 0.52 4

Sample Output

0.50000000.2500000

Source

POJ Monthly Contest - 2009.08.23, Simon
有一条路,n个炸弹,有p的概率向前走一步,(1-p)的概率向前走两步,问从1到走完的概率是多少?给出了每个炸弹的位置。dp[i]表示在第i步到走完的概率
状态方程很简单:dp[i] = p*dp[i+1] + (1-p)*dp[i+2] ;在有炸弹的位置的概率是0,最后的概率是1
难点在于炸弹的位置很大,不能直接计算,矩阵优化
p  (1-p)          dp[i+1]       dp[i]
1    0           *     dp[i+2]   =  dp[i+1]
然后使用矩阵的快速幂http://blog.csdn.net/winddreams/article/details/40454195,忽略掉两个炸弹中间的结果,求最终值。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{    double x , y ;    double  a[2][2] ;} s , k ;node f(node p,node q){    node k ;    int i , j , l ;    k.x = p.x ;    k.y = q.y ;    for(i = 0 ; i < k.x ; i++)    {        for(j = 0 ; j < k.y ; j++)        {            k.a[i][j] = 0 ;            for(l = 0 ; l < p.y ; l++)                k.a[i][j] += p.a[i][l]*q.a[l][j] ;        }    }    return k ;}node pow(node p,int m){    node q ;    q.x = q.y = 2 ;    q.a[0][0] = q.a[1][1] = 1 ;    q.a[0][1] = q.a[1][0] = 0 ;    if(m == 0)        return q ;    q = pow(p,m/2);    q = f(q,q);    if( m%2 )        q = f(q,p);    return q;}int main(){    int n , i , j , a[12] , m , flag;    double p ;    while(scanf("%d %lf", &n, &p)!=EOF)    {        flag = a[0] = 0 ;        for(i = 1 ; i <= n ; i++)        {            scanf("%d", &a[i]);            if(a[i] == 1) flag = 1 ;        }        if(flag)        {            printf("0.0000000\n");            continue ;        }        sort(a,a+n+1);        s.x = 2 ;        s.y = 1 ;        s.a[0][0] = 1 ;        k.x = k.y = 2 ;        k.a[0][0] = p ;        k.a[0][1] = (1-p) ;        k.a[1][0] = 1 ;        k.a[1][1] = 0 ;        for(i = n-1 ; i >= 0 ; i--)        {            s.a[1][0] = s.a[0][0] ;            s.a[0][0] = 0 ;            m = ( a[i+1]-1-a[i] ) ;            s = f(pow(k,m),s);        }        printf("%.7lf\n", s.a[0][0]);    }    return 0;}

poj3744--Scout YYF I(概率dp第五弹:矩阵优化)