POJ 3744 Scout YYF I 矩阵快速幂优化--概率dp

点击打开链接


Scout YYF I

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5416   Accepted: 1491

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个雷放在1~100000000的位置上,初始位置是1,走一步的概率是p,走两步的概率是1-p,求顺利通过所有雷的概率。

状态转移方程:dp[i]=p*dp[i-1]+(1-p)*dp[i-2],如果直接算会MLE或者TLE,可以转换成用矩阵快速幂求解。

那么可以分别求出从起点到第一个雷的概率,然后求出第一个雷的位置+1到第二个雷的概率,依次分别求出不睬到雷的概率(1-踩到雷的概率),然后累乘就是答案。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
double p;
int pos[17];
struct Matrax
{
    double m[2][2];
}a,per,tmp;
void init()//建立矩阵
{
    a.m[0][0]=p;a.m[0][1]=1-p;
    a.m[1][0]=1;a.m[1][1]=0;
    per.m[0][0]=1;per.m[0][1]=0;
    per.m[1][0]=0;per.m[1][1]=1;
}
Matrax multi(Matrax a,Matrax b)//矩阵相乘
{
    Matrax c;
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
        {
            c.m[i][j]=0;
            for(int k=0;k<2;k++)
                c.m[i][j]+=a.m[i][k]*b.m[k][j];
        }
        return c;
}
Matrax power(int k)//矩阵快速幂
{
    Matrax pp=a,ans=per;
    while(k)
    {
        if(k&1){ans=multi(ans,pp);k--;}
        else {k>>=1;pp=multi(pp,pp);}
    }
    return ans;
}
int main()
{
    int n;
    while(scanf("%d%lf",&n,&p)!=EOF)
    {
        bool no=false;
        double ans=1;
        memset(pos,0,sizeof(pos));
        for(int i=1;i<=n;i++)
            scanf("%d",&pos[i]);
        sort(pos+1,pos+n+1);
        for(int i=1;i<=n;i++)
            if(pos[i]==1||pos[i]==pos[i-1]){no=true;break;}
        if(no){printf("%.7lf\n",0);continue;}

        init();
        tmp=power(pos[1]-1);//求从1到第一个雷的概率
        ans*=(1-tmp.m[0][0]);//求不到第一个雷的概率
        for(int i=2;i<=n;i++)
        {
            if(pos[i]==pos[i-1])continue;
            tmp=power(pos[i]-pos[i-1]-1);//求从第i-1个雷后面一步到第i个雷的概率
            ans*=(1-tmp.m[0][0]);//不到第i个雷的概率
        }
        printf("%.7lf\n",ans);
    }
    return 0;
}

时间: 2024-10-06 09:08:51

POJ 3744 Scout YYF I 矩阵快速幂优化--概率dp的相关文章

POJ 3744 Scout YYF I 矩阵快速幂

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4452   Accepted: 1159 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 (矩阵)

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

POJ 3744 Scout YYF I(矩阵优化的概率DP)

解题思路: dp[i] = p * dp[i-1] + (1 - p) * dp[i-2]; 由于N比较大,dp[i]需要用矩阵快速幂求解. 安全通过整段路的概率等于安全通过每一个两个炸弹区间的概率乘积. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #

hdu 5564 Clarke and digits 矩阵快速幂优化数位dp

Clarke and digits Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned into a researcher, did a research on digits. He w

HDU 5863 cjj&#39;s string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )

题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但是 m 很小的时候 发现此题DP并不合适.于是想可能是某种组合数学的问题可以直接公式算 看到题解的我.恍然大悟.对于这种数据.可以考虑一下矩阵快速幂优化的DP 首先要想到线性递推的 DP 式子 最直观的想法就是 dp[i][j] = 到第 i 个位置为止.前面最长匹配长度为 j 的方案数 但是如果仔

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

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

POJ3744——概率DP 矩阵快速幂优化——Scout YYF I

http://poj.org/problem?id=3744 矩阵快速幂: 利用DP的递推式 就本题来说 dp[i] = p*dp[i-1] + (1-p)*dp[i-2] 由于x非常大最大1亿,这样的话复杂度就为1亿 所以这里可以用矩阵的思想 [dp[i]   dp[i-1] ] = [ dp[i-1]  dp[i-2] ] | p   1 - p| | 1      0  | 递推得到 n - 1 [dp[n]   dp[n-1]] = [dp[1]   dp[2] ] |p   1 - p

poj 3744 概率dp 矩阵快速幂优化

一位童子兵要穿过一条路,路上有些地方放着地雷.这位童子兵非常好玩,走路一蹦一跳的.每次他在 i 位置有 p 的概率走一步到 i+1 ,或者 (1-p) 的概率跳一步到 i+2.童子兵初始在1位置,求他安全通过这条道路的概率. 以所在位置为状态,dp[i] 表示在位置 i 的安全的概率. dp[i] = p * dp[i-1] + (1 - p) * dp[i-2]; // i 位置没有地雷 但是题目数据的范围是 10^8 这样dp的话会 TLE. 想想可以用矩阵快速幂优化.简单退出矩阵是 |p

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st