1. 程式人生 > >poj 3744 Scout YYF I (矩陣乘法+概率與期望DP)

poj 3744 Scout YYF I (矩陣乘法+概率與期望DP)

Scout YYF I
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8237 Accepted: 2428

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


題解:矩陣乘法+概率與期望DP

對於不是地雷的位置,我們用矩陣乘法來優化DP

f[i]=f[i-1]*p+f[i-2]*p

對於是地雷的位置x,

f[x+1]=f[x-1]*(1-p)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,a[20]; double p;
struct data{
	double a[3][3];
}e,c,f;
void clear(data &a)
{
	for (int i=1;i<=2;i++)
	 for (int j=1;j<=2;j++) a.a[i][j]=e.a[i][j];
}
data mul(data a,data b)
{
	data c;
	for (int i=1;i<=2;i++)
	 for (int j=1;j<=2;j++) {
	 	c.a[i][j]=0;
	 	c.a[i][j]+=a.a[i][1]*b.a[1][j];
	 	c.a[i][j]+=a.a[i][2]*b.a[2][j];
	 }
	return c;
}
data quickpow(data num,int x)
{
	data ans; clear(ans);
	data base; base=num;
	while (x) {
		if (x&1) ans=mul(ans,base);
		x>>=1;
		base=mul(base,base);
	}
	return ans;
}
int main()
{
	freopen("a.in","r",stdin);
//	freopen("my.out","w",stdout);
	for (int i=1;i<=2;i++) e.a[i][i]=1;
	while (scanf("%d%lf",&n,&p)!=EOF) {
	   c.a[1][1]=0; c.a[1][2]=(1.0-p);
	   c.a[2][1]=1; c.a[2][2]=p;
	   f.a[1][1]=0; f.a[1][2]=1;
	   for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	   sort(a+1,a+n+1);
	   if (a[1]==1) {
	   	printf("%.7lf\n",0);
	   	continue;
	   }
	   data ans; a[0]=0; bool pd=false;
	   for (int i=1;i<=n;i++) {
	   	  if (a[i-1]+1==a[i]) {
	   	  	   pd=true;
	   	  	   break;
			 }
	   	  data ans=quickpow(c,a[i]-a[i-1]-2);
	   	  ans=mul(f,ans);
	   	  f.a[1][1]=0; f.a[1][2]=ans.a[1][2]*(1.0-p);
	   }
	   if (!pd)  printf("%.7lf\n",f.a[1][2]);
	   else printf("%.7lf\n",0);
	}
}


相關推薦

poj 3744 Scout YYF I 矩陣乘法+概率期望DP

Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8237 Accepted: 2428 Des

POJ 3744 Scout YYF I 矩陣相乘+概率DP

題面: Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7254 Accepted: 2118 Description YYF is a couragous scout. Now he i

poj-3744 Scout YYF I [用矩陣優化概率遞推式]

/* 題意:在一條不滿地雷的路上,你現在的起點在1處。 在N個點處布有地雷,1<=N<=10。地雷點的座標範圍:[1,100000000]. 每次前進p的概率前進一步,1-p的概率前進兩步

概率DP+矩陣快速冪】 POJ - 3744 Scout YYF I

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. Afte

poj 3744 Scout YYF I 矩陣快速冪 概率論

題目 題解 規模較大,可以很容易想到是需要矩陣快速冪來加速遞推的。 將概率轉化為多次從前面個雷後面一格安全達到下個雷後面一格。 [10][p11−p0]x x=ai−ai−1−1 轉移一次之後捨棄mat0,0的概率

POJ 3744 Scout YYF I(矩陣優化的概率DP

解題思路: dp[i] = p * dp[i-1] + (1 - p) * dp[i-2]; 由於N比較大,dp[i]需要用矩陣快速冪求解。 安全通過整段路的概率等於安全通過每一個兩個炸彈區間的概率乘積。 #include <iostream> #include

POJ 3744 Scout YYF I概率dp

特殊矩陣 con 滿足 sta struct -s 沒有 ssi get 題目鏈接:http://poj.org/problem?id=3744 題意:   有n個地雷,位置為pos[i]。   在每個位置,你向前走一步的概率為p,向前走兩步的概率為1-p。   你的初始位

[poj 3744]Scout YYF I

通過 through sin gin images tex pro tdi %d 題目大意: YYF在一條有地雷的路上行走,每次有p的概率走一步,否則走兩步。求安全到達終點概率。 又是概率水題,來分(luan)析(gao)一下: 題目所求為安全到達終點概率,那麽肯定要分

POJ-3744-Scout YYF I

ACM模版 描述 題解 分段 + 概率 DP + 矩陣加速。 首先,題目給了雷的數目至多隻有十個,不算多,可以將全程進行分段,保證每段只有一個雷或者多個雷在一個位置,並且雷的位置都是段尾。 分段後,每一段之間都是獨立的,求出安全通過每一段的概率

解題報告 之 POJ 3744 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 no

Scout YYF I矩陣優化dp

題目: 我是超連結 題意: 主人公有p的機率走一步,1-p的機率走兩步,主人公一開始在格子1,給出地雷的位置,請問安全通過的概率是多少 題解: 概率與期望第一彈,但是這道題體現的基本是矩陣優化思想,那就當是矩陣題做一下 dp[i]表示到第i個格子安全的概率,不難發現:dp[

codeforces 148D. Bag of mice 概率期望DP

The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in t

POJ 3744 Scout YYF 矩陣優化的概率DP

題目大意: 在一條不滿地雷的路上,你現在的起點在1處。在N個點處布有地雷,1<=N<=10。地雷點的座標範圍:[1,100000000]. 每次前進p的概率前進一步,1-p的概率前進1-p步。問順利通過這條路的概率。就是不要走到有地雷的地方。 題目思路:

POJ 3744 Scout YYF(概率DP+矩陣快速冪

Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6890 Accepted: 2004 Description YYF is a couragous scout.

【BZOJ 4818】 4818: [Sdoi2017]序列計數 矩陣乘法、容斥計數

【分析】   f[i][j]表示填i個數,mod為j的方案數。   這個轉移可以用矩陣加速啦,那麼n很大也沒關係了。   然後要至少有一個質數,就用總方案數-沒有一個質數。   然後一開始跑20s,看到別人2s,然後覺得這個迴圈矩陣的矩陣乘法可以O(n^2),改了就2s了。。 1 #include

POJ 3233 Matrix Power Series 矩陣快速冪+等比數列二分求和

Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23205 Accepted: 9669 Description Given a n × n ma

hihoCoder#1743:K-偏差排列矩陣快速冪+狀壓dp

unit register 狀態 long struct CP def com 不同的 題意: 如果一個 \(1\to N\) 的排列 \(P=[P_1, P_2, ... P_N]\) 中的任意元素 \(P_i\) 都滿足 \(|P_i-i| ≤ K\) ,我們就稱

bzoj1076概率期望dp入門

題目大意:給定k次彈出寶物的機會,每次隨機彈出n種寶物的機會,如果吃過這種寶物的所有前提寶物就可以吃這種寶物,求最優策略的期望得分 看到資料範圍果斷狀壓DP- - 不看資料範圍害死人- - 至於吃還是不吃 這是個問題 對於這種最優策略的期望DP 我們一般都是從後往前推 列舉

期望—— 概率期望 DP 學習筆記

我們來跟著這篇部落格來學習一下好辣。 只會給出一些自己的理解。 方法一:直接定義期望狀態 全期望公式:E(Y)=∑nP(X=xi)E(Y|X=xi) 這道題的終點很明確,那就是走到 n 即停止。對於期望 DP,我們一般採用逆

poj 3744 Scout (Another) YYF I - 概率期望 - 動態規劃 - 矩陣快速冪

遞推 cto bits dig min ability 構建 nes text (Another) YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int