Scout YYF I (POJ - 3744)(概率dp+矩陣快速冪)

Scout YYF I POJ - 3744

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

有n個炸彈在不同的位置,你在1點,往前跳一格的概率是p,跳兩格的概率是1-p,你能跳出這些炸彈的概率是多少,

  • 可以得出遞推公式dp[i] = dp[i-1]* p+dp[i-2] * (1-p),dp[炸彈位置] = 0,結果求dp[最後一個炸彈位置+1] = dp[最後一個炸彈位置-1]*(1-p),可是最後一個炸彈位置太大,用矩陣快速冪加速
    [dp[i]dp[i1]]=[p1p10][dp[i]dp[i1]] \left[ \begin{matrix} dp[i] \\ dp[i-1] \end{matrix} \right]= \left[ \begin{matrix} p & 1-p \\ 1 & 0 \end{matrix} \right] \left[ \begin{matrix} dp[i] \\ dp[i-1] \end{matrix} \right]
  • 因爲矩陣乘法滿足a*(b*c) = a * b *c,所以 [dp[n+1]dp[n]]=[p1p10]n[dp[1]dp[0]] \left[ \begin{matrix} dp[n+1] \\ dp[n] \end{matrix} \right]= \left[ \begin{matrix} p & 1-p \\ 1 & 0 \end{matrix} \right] 的n次方* \left[ \begin{matrix} dp[1] \\ dp[0] \end{matrix} \right]
  • 每次用快速冪計算到下一個有炸彈的位置,然後讓dp[當前位置] = 0,再算下一個位置
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<algorithm>
using namespace std;
class node
{
public:

    double mar[3][3];
    node()
    {
        memset(mar,0,sizeof(mar));
    }
};
long long a[120];

node mul(node a,node b)
{
    node c;

    for(long long i = 0; i<2; i++)
    {
        for(long long j = 0; j<2; j++)
        {
            for(long long k = 0; k<2; k++)
            {
                c.mar[i][j] += a.mar[i][k]*b.mar[k][j];
            }
        }
    }
    return c;
}
node qui_mul(node a,long long num)
{
    node ans;
    ans.mar[0][0] = ans.mar[1][1] = 1;
    while(num)
    {
        if(num&1)
        {
            ans = mul(ans,a);
        }
        num>>=1;
        a = mul(a,a);
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    long long i,j,m,n,now;
    double p;
    while(scanf("%lld %lf",&n,&p)!=EOF)
    {
        node aa,ans,bb;
        for(i = 0; i<n; i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        aa.mar[0][0] = p;
        aa.mar[0][1] = 1-p;
        aa.mar[1][0] = 1;

        ans.mar[0][0] =1;
        now = 1;
        for(i = 0; i<n; i++)
        {
            long long num = a[i]-now;
            bb = qui_mul(aa,num);
            ans = mul(bb,ans);
            ans.mar[0][0] = 0;
            now = a[i];
        }
        cout<<fixed<<setprecision(7)<<ans.mar[1][0]*(1-p)<<endl;

    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章