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.5
2
2 0.5
2 4

Sample Output

0.5000000
0.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;
}
时间: 2024-10-06 09:08:42

poj3744--Scout YYF I(概率dp第五弹:矩阵优化)的相关文章

[poj3744]Scout YYF I(概率dp+矩阵快速幂)

题意:在一维空间上存在一些雷,求安全通过的概率.其中人有$p$的概率前进一步,$1-p$的概率前进两步. 解题关键:若不考虑雷,则有转移方程:$dp[i] = p*dp[i - 1] + (1 - p)*dp[i - 2]$ 由于雷的数量很少,所以可以以雷为界,将区域分开,在每个区域中,通过该段的概率等于1-踩到该段终点的地雷的概率.然后用矩阵快速幂优化一下即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorit

POJ3744 Scout YYF I (概率DP + 矩阵优化)

题目链接: http://poj.org/problem?id=3744 题意: 有一段路,路上有n个陷阱,每一次只能向前走一步或者两步,求安全走过这段路的改路 分析: 设dp[i]表示安全走过第i个陷阱的概率 那么dp[i+1]=dp[i]*(1-p(走到第i+1个陷阱)) 因为每次只能走一步或者两步,所有安全走过第i个陷阱后的位置一定在a[i]+1;\ 其中a[i]表示第i个陷阱的位置 求从a[i]+1,走到a[i+1]的概率的时候我们需要用到矩阵来经行优化 ans[i]表示走到位置i的概率

poj 3744 Scout YYF I(概率dp,矩阵优化)

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

POJ 3744:Scout YYF I 概率DP+特征方程+快速幂

Scout YYF I 题目连接: http://poj.org/problem?id=3744 题意: 有个人要到一个叫“mine road”的地方,路线是一条直线,起点在1,路上有N个地雷,坐标在[1, 100000000]之间,这人每次有p(0.25≤p≤0.75)的概率向前走一步,且有1-p的概率向前走两步,问这货安全通过雷区到达"mine road"的概率 题解: 利用特征方程求出通项表达式,要走过有雷的地方f(n+1)=f(n-1)*(1-p).   PS:也可以用矩阵快速

POJ 3744 Scout YYF I (概率DP+矩阵快速幂)

题意:小明要从1走过一段直线雷区,给定n个地雷的坐标,他走一步的概率是p,两步的概率为1-p,问你他能安全通过雷区的概率. 析:很明显这是一个概率DP,用d(i)表示到达 i 时他安全的概率,那么d[i] = p * d[i-1] + (1-p) * d[i-2];这个状态转移方程很好理解, 就是说要想到达 i 要么从第 i-1 走一步,要么从 i-2 走两步,最后加起来,然后问题来了,这个数可能达到 1E8,那么时间空间复杂度都受不了, 观察这个状态转移方程,是不是很像Fibnacci数列,所

poj 3744 Scout YYF I 概率dp+矩阵乘法

分析: dis(k,v1,v2)函数求到当前位置概率为v1,到当前位置之前一步的概率为v2,前进k步到达位置的概率,然后矩阵加速. 代码: //poj 3744 //sep9 #include <iostream> #include <algorithm> using namespace std; int pos[12]; double p,mat[4][4]; double ans[4][4]; void mul1() { double c[4][4]; c[0][1]=c[1]

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

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 n

hdu3076--ssworld VS DDD(概率dp第三弹,求概率)

ssworld VS DDD Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1487    Accepted Submission(s): 304 Problem Description One day, sssworld and DDD play games together, but there are some special

zoj3329--One Person Game(概率dp第六弹:形成环的dp,带入系数,高斯消元)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the