poj3744--Scout YYF I(機率dp第五彈:矩陣最佳化),poj3744--scoutyyf

來源:互聯網
上載者:User

poj3744--Scout YYF I(機率dp第五彈:矩陣最佳化),poj3744--scoutyyf
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;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.