POJ 3744 概率dp+矩陣
http://poj.org/problem?id=3744
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].
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開始出發, 每走一步,有兩種情況:步子為1的概率為p;步子為2的概率為1-p。求能安全通過這條路的概率是多少?
解題思路:這是一道概率dp+矩陣的題,dp[i]=dp[i-1]*p+dp[i-2]*(1-p);這個題的資料範圍太大,中間又有地雷間隔,所以是不能直接跑的。我們依據地雷的位置把要求的概率看做n段來求:1~a[0],a[0]+1~a[1],,,,,,,a[n-2]+1~a[n-1].每一段的最後一個數為地雷的位置,這樣我們求出每個dp[ a[x] ] 的值,最後把所有的1-a[x] 相乘即為答案。對於每段我們用矩陣來做:
值得一提的是:我們把初始矩陣直接看成:a[1]=1,a[0]=1;這樣做起來方便的多
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct Matrix
{
double m[2][2];
};
Matrix I={1,0,0,1};
Matrix mult_matrix(Matrix a,Matrix b)
{
Matrix 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;
}
Matrix quick_mod(Matrix a,int n)
{
Matrix c=I;
while(n)
{
if(n&1)
c=mult_matrix(c,a);
a=mult_matrix(a,a);
n>>=1;
}
return c;
}
int a[15],n;
double p;
int main()
{
while(~scanf("%d%lf",&n,&p))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);//順序
double ans=1.0;
Matrix A={p,1-p,1,0};//初始化轉移矩陣
Matrix B=quick_mod(A,a[0]-1);
ans*=(1-B.m[0][0]);
for(int i=1;i<n;i++)
{
Matrix B=quick_mod(A,a[i]-a[i-1]-1);
ans*=(1-B.m[0][0]);
}
printf("%.7lf\n",ans);
}
return 0;
}
相關文章
- POJ3744 Scout YYF I (概率DP + 矩陣優化)矩陣優化
- 矩陣類 poj3420矩陣
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- poj1050 最大子矩陣和矩陣
- HDU3519Lucky Coins Sequence(DP+矩陣加速)矩陣
- 手把手教你將矩陣&概率畫成圖矩陣
- 基於概率的矩陣分解原理詳解(PMF)矩陣
- POJ 1050-To the Max(最大子矩陣和)矩陣
- POJ 3150 Cellular Automaton(矩陣快速冪)矩陣
- POJ 2778-DNA Sequence(AC自動機+構建鄰接矩陣+矩陣快速冪)矩陣
- 【Ac自動機+矩陣加速】poj 2778 DNA Sequence矩陣
- poj 2778 AC自動機與矩陣連乘矩陣
- POJ 3233 Matrix Power Series(矩陣+二分)矩陣
- 概率論12 矩與矩生成函式函式
- poj3017 dp+單調佇列佇列
- 轉移概率矩陣------3Q大戰背後的數學矩陣
- “機會潛在利潤一企業成功概率”矩陣(轉載)矩陣
- 生成螺旋矩陣(方陣、矩陣)矩陣
- 鄰接矩陣、度矩陣矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- POJ3070 Fibonacci[矩陣乘法]【學習筆記】矩陣筆記
- 矩陣矩陣
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 3D圖形:矩陣的行列式,矩陣的逆、正交矩陣、齊次矩陣3D矩陣
- 矩陣中最大的二維矩陣矩陣
- UVA 11427 Expect the Expected (概率dp+推公式求期望 詳解)公式
- POJ 3034 Whac-a-Mole(三維dp+處理小技巧)
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- 矩陣和陣列矩陣陣列
- 理解矩陣矩陣
- 矩陣相乘矩陣
- 矩陣分解矩陣
- 稀疏矩陣矩陣
- Numpy 矩陣矩陣
- 穿越矩陣矩陣
- 混淆矩陣矩陣
- 螺旋矩陣矩陣