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;
}
相關文章
- 序列(dp+矩陣加速)矩陣
- 幸運數(dp+矩陣加速)矩陣
- 巨大的數(dp+矩陣加速)矩陣
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- POJ 3071 Football(概率DP)
- 手把手教你將矩陣&概率畫成圖矩陣
- HDU 5816 Hearthstone(狀態壓縮DP+概率)
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- 巨大的矩陣(矩陣加速)矩陣
- 鄰接矩陣、度矩陣矩陣
- POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- poj--1625Censored!+AC自動機上的dp+大數
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 矩陣矩陣
- 求任意矩陣的伴隨矩陣矩陣
- 矩陣和陣列矩陣陣列
- 理解矩陣矩陣
- 海浪矩陣矩陣
- 矩陣相乘矩陣
- 稀疏矩陣矩陣
- 螺旋矩陣矩陣
- 矩陣乘法矩陣
- 8.6 矩陣?矩陣
- 找矩陣矩陣
- 矩陣分解矩陣
- 快手矩陣管理平臺,矩陣管理有方法矩陣
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- 矩陣:如何使用矩陣操作進行 PageRank 計算?矩陣
- 演算法學習:矩陣快速冪/矩陣加速演算法矩陣
- 高斯消除矩陣矩陣
- 矩陣求導矩陣求導
- 置換矩陣矩陣
- 視訊矩陣矩陣
- 矩陣樹定理矩陣
- leetcode:螺旋矩陣LeetCode矩陣
- 矩陣置0矩陣
- 雅可比矩陣矩陣
- 隨機矩陣隨機矩陣