1. 程式人生 > >zoj 3329(dp-數學期望)

zoj 3329(dp-數學期望)

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 Die1Die2 and Die3Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the dice are fair dice, so the probability of rolling each value, 1 to K1

K2K3 is exactly 1 / K1, 1 / K2 and 1 / K3. You have a counter, and the game is played as follow:

  1. Set the counter to 0 at first.
  2. Roll the 3 dice simultaneously. If the up-facing number of Die1 is a, the up-facing number of Die2 is b and the up-facing number of Die3 is c, set the counter to 0. Otherwise, add the counter by the total value of the 3 up-facing numbers.
  3. If the counter's number is still not greater than n, go to step 2. Otherwise the game is ended.

Calculate the expectation of the number of times that you cast dice before the end of the game.

Input

There are multiple test cases. The first line of input is an integer T (0 < T <= 300) indicating the number of test cases. Then T

 test cases follow. Each test case is a line contains 7 non-negative integers nK1K2K3abc (0 <= n <= 500, 1 < K1K2K3 <= 6, 1 <= a <= K1, 1 <= b <= K2, 1 <= c <= K3).

Output

For each test case, output the answer in a single line. A relative error of 1e-8 will be accepted.

Sample Input

2 0 2 2 2 1 1 1 0 6 6 6 1 1 1 

Sample Output

1.142857142857143 1.004651162790698
今天剛剛講了這題,經過連教的指導,明白了這題的精髓,數學期望的題一般有dp解,但dp一般只能夠解有向無環圖(DAG),如果有環必然狀態迴圈,這題是有環圖,但是有巧妙的處理後可以得出結果。
題意是給你三個骰子,分別有k1,k2,k3個面,設初始分數為0,當分數>=n結束,但是當你擲三個骰子分別為a,b,c的時候分數清0;這題的狀態很好轉移,E(i)=sum(Pk*E(i+k))+P0*E(0)+1;(i表示當前分數,而pk表示三骰子總和為k的概率,E儲存的是分數i距離n還差的期望(需要投擲的平均次數),加一表示加入當前這次投擲),如果不懂這裡的話請參考我的另一題poj2096,這裡懂的話,就進入關鍵步驟,由方程你會發現,每個E(i)都包含E(0),而E(0)又是我們要求的結果,顯然是個定值,我們是否可以像數學中那樣分離引數呢,顯然是可以的可以寫這樣一個方程設E(i)=a(i)*E(0)+b(i);顯然將這個式子帶入E(i)=sum(Pk*E(i+k))+P0*E(0)+1,可以得到一個關於E[i]和E[0],a[i],b[i]的特徵方程,得到E[i]=(sum(a[i+k])+p0)*E[0]+sum(b[i+k])+1;顯然a[i]=sum(a[i+k])+p0)*E[0],b[i]=sum(b[i+k])+1,而由E(i)=a(i)*E(0)+b(i)可以得出E[0]=a(0)*E(0)+b(0);得到E[0]=b[0]/(1-a[0]);,我們只需要,得到a[0],b[0],即可,而a[i],b[i]的狀態轉移方程也有了,出狀態也容易求E[n]=0,所以a[n]=0,b[n]=0;
下面是我的程式碼
#include<cstdio>  
#include<cstring>  
double p[19],tem; 
int s,k1,k2,k3,a,b,c; 
void init() 
{       
	tem=1.0/(k1*k2*k3); 
	s=k1+k2+k3;  
	memset(p,0,sizeof(p));
	for(int i=1;i<=k1;i++) 
		for(int j=1;j<=k2;j++)
			for(int k=1;k<=k3;k++)
			{
				if(i!=a||j!=b||k!=c)
					p[i+j+k]+=tem;
			} 
}  
void dp(int n) 
{ 
	double a[501]={0},b[501]={0}; 
	for(int i=n;i>=0;i--)
	{      
		for(int k=3;k<=s&&(i+k)<=n;k++)
		{
		a[i]+=a[i+k]*p[k];
		b[i]+=b[i+k]*p[k];                                 
		}
		a[i]+=tem;
		b[i]+=1;
	}
	printf("%.15lf\n",b[0]/(1-a[0]));
}
int main() 
{
	int m,n; 
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
		init();
		dp(n);
	}
	return 0;
}

相關推薦

zoj 3329dp-數學期望

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, 

2018.11.07【校內模擬】異或數位DP數學期望

傳送門 解析: 蒟蒻考場上只想了隨機情況下的期望,於是就拿了部分分滾粗了。。。 其實最優情況下的期望我好像還推錯了,最後學習了標解才會的。 我好菜啊。。。希望今年NOIP不要打醬油就行了。 思路: 首先隨機的情況其實非常好想。我們只需要考慮每個位出現

ZOJ3329-One Person Game概率DP數學期望

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person g

ZOJ3551Bloodsucker 數學期望

ans post cas ould cal digits oba ostream () In 0th day, there are n-1 people and 1 bloodsucker. Every day, two and only two of them meet.

【BZOJ4872】分手是祝願動態規劃,數學期望

esp math map ostream pac mes ++i rac define 【BZOJ4872】分手是祝願(動態規劃,數學期望) 題面 BZOJ 題解 對於一個狀態,如何求解當前的最短步數? 從大到小枚舉,每次把最大的沒有關掉的燈關掉 暴力枚舉因數關就好 假設我

【BZOJ1415】【NOI2005】聰聰和可可動態規劃,數學期望

數學期望 class ios char for problem lin vector noi 【BZOJ1415】【NOI2005】聰聰和可可(動態規劃,數學期望) 題面 BZOJ 題解 先預處理出當可可在某個點,聰聰在某個點時 聰聰會往哪裏走 然後記憶化搜索一下就好了 #

2018.10.20 NOIP模擬 麵包數學期望

傳送門 把方差的式子拆開。 方差=平方的期望-期望的平方。 顯然只用維護點對的個數和總方案數就行了。 利用分步的思想來統計。 要統計覆蓋一個矩形(x1,y1,x2,y2)(x1,y1,x2,y2)(x1

hdu5984 Pocky數學期望公式

題目連結: 題目大意: 對於一根長度為 L 的木棍,每次等概率的取一個點將其分成兩半,然後吃掉左邊一半,直到剩下的長度小於 d,計算需要吃(分割)次數的數學期望 題目思路: 顯然,如果 L/d 相同

【HDU 6410】序列期望 百度之星複賽B 數學期望

題目大意 有n個隨機變數x1,x2,…,xn。給定區間[l1,r1],…,[ln,rn],變數xi的值會等概率成為區間[li,ri]中的任意一個整數。 顯然這n個隨機變數的值會有一共∏ni=1(ri−li+1) 種情況,且每種情況出現的概率為 ∏ni=1ri

ZOJ 3329 One Person Game——期望DP

line 清零 double turn const per div main game 題目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 高斯消元好像時間復雜度太高。 註意到每個位置都可以從

【NOIP2017提高A組衝刺11.2】救贖數學期望

Description “是的。”我回答,“我不會忘記你。在森林裡我會一點點記起往日的世界。要記起的大概很多很多:各種人、各種場所、各種光、各種歌曲……” ——村上春樹《世界盡頭與冷酷仙境》 在沒有心存在的世界盡頭,音樂能夠使小鎮居民消散的心重新聚攏成形。

【BZOJ1076】獎勵關動態規劃,數學期望

題面 懶,粘地址 題解 我也是看了題解才會做 看著資料範圍,很容易想到狀壓 然後,設f[i][j]表示當前第i輪,狀態為j的期望 列舉當前掉出來哪一個物品 然後。。。。怎麼轉移???

HDU 5245 染色問題數學期望

Joyful Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 478    Accepted Submis

【BZOJ1076】【SCOI2008】獎勵關DP期望、狀壓

Description Solution 套路的狀壓期望DP題。。。 考慮倒退期望:設fi,jfi,j為一直到第i−1i−1輪、當前狀態為jj的最大分數。 轉移 若當前狀態滿足第kk個

關於android中的單位dp、sp

字號 兩個 metrics 不一定 ont ace white 超級 style android讓人頭疼的適配問題。 --------- Android 中的單位大概有這些: 經常使用的dip、sp。有時候用到px。 --------- 介紹兩個類: Type

[luoguP1005] 矩陣取數遊戲DP + 高精度

put ring 分享 tdi pre closed () hide += 傳送門 和奶牛那個題很像,每一行狀態互不影響,也就是求 n 遍DP 不過高精度非常惡心,第一次寫,調了我一上午。 ——代碼 1 #includ

[luoguP1282] 多米諾骨牌DP + 背包

getchar() digi 差值 log color pen org == close 傳送門 將問題轉換成分組背包,每一組有上下兩個,每一組中必須選則一個,上面的價值為0,下面的價值為1,求價值最小 因為要求上下兩部分差值最小,只需從背包大小為總數 / 2 時

lightoj 1095 - Arrange the Numbersdp+組合數

鏈接 簡單 turn amp using ace ring ++ ber 題目鏈接:http://www.lightoj.com/volume_showproblem.php?problem=1095 題解:其實是一道簡單的組合數只要推導一下錯排就行了。在這裏就推導

HDU 5890 Eighty sevenDP+bitset優化

using ostream brush i+1 eset ons bitset print sort 這個題的背包的思想還是很容易想到的,但是這個bitset優化還是有點神奇的呀。。 #include<bitset> #include<stdio.h

[HDU2157]How many ways??DP + 矩陣優化

per printf 需要 給定 get sizeof ref 傳送門 href 傳送門 k < 20 k這麽小,隨便dp一下就好了。。。 dp[i][j][k]表示從i到j經過k個點的方案數 4重循環。。 但是如果k很大就不好弄了 把給定的圖