hdu 4652 Dice(期望)

來源:互聯網
上載者:User

標籤:機率dp

http://acm.hdu.edu.cn/showproblem.php?pid=4652


擲一枚骰子,有m個面,問擲出連續出現n個相同的面以及連續出現n個兩兩不同的面的期望。

設dp[i]表示已經擲出i個相同/不同的面的期望,可以確定終態dp[n] = 0,

對於出現連續n個相同的面有

dp[i] = 1/m * dp[i+1] + (m-1)/m*dp[1] + 1

再列一個式子 dp[i+1] = 1/m * dp[i+2] + (m-1)/m * dp[1] + 1

兩式相減得dp[i+1] - dp[i] = 1/m * (dp[i+2] - d[i+1]),可以發現任意連續的兩數之差成等比數列,就可以求出dp[0]。


對於出現n個兩兩不同的面有

dp[i] = (m-1)/m * dp[i+1] + 1/m * (dp[1] + dp[2] + ...+ dp[i])

再列一個式子dp[i+1] = (m-1)/m * dp[i+2] + 1/m*(dp[1] + dp[2] +....+dp[i+1])

兩式相減得dp[i+1] - dp[i] = (m-i-1)/m * (dp[i+1] - dp[i+2]),最後各個連續的式子相減也能求出dp[0]。


#include <stdio.h>#include <iostream>#include <map>#include <set>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>//#define LL __int64#define LL long long#define eps 1e-8#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 100010;int main(){int test;int order,m,n;while(~scanf("%d",&test)){for(int i = 1; i <= test; i++){scanf("%d %d %d",&order,&m,&n);double ans = 1;if(order == 0){double p = m*1.0,tmp = m*1.0;for(int i = 1; i <= n-1; i++){ans += tmp;tmp *= p;}}else{double tmp = 1.0;for(int i = 1; i <= n-1; i++){tmp *= m*1.0/(m-i);ans += tmp;}}printf("%.9lf\n",ans);}}return 0;}

 

hdu 4652 Dice(期望)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.