HDU 4652 dice (probability DP)

Source: Internet
Author: User

Dice
Problem descriptionyou have a dice with M faces, each face contains a distinct number. we assume when we tossing the dice, each face will occur randomly and uniformly. now you have t query to answer, each query has one of the following form:
0 m n: Ask for the expected number of tosses until the last n times results are all same.
1 m n: Ask for the expected number of tosses until the last n consecutive results are pairwise different.
Inputthe first line contains a number t. (1 ≤ T ≤ 100) The next T line each line contains a query as we mentioned above. (1 ≤ m, n ≤ 106) for Second Kind query, we guarantee n ≤ m. and in order to avoid potential precision issue, we guarantee the result for our query will not exceeding 109 in this problem.
Outputfor each query, output the corresponding result. The answer will be considered correct if the absolute or relative error doesn' t exceed 10-6.
Sample Input
60 6 10 6 30 6 51 6 21 6 41 6 6101 4534 251 1232 241 3213 151 4343 241 4343 91 65467 1231 43434 1001 34344 91 10001 151 1000000 2000
 
Sample output
1.00000000043.0000000001555.0000000002.2000000007.60000000083.20000000025.58631582426.01599003715.17634116024.5410457699.027721917127.908330426103.9754552539.00349551515.0562044724731.706620396
 
Source2013 multi-university training contest 5

Question:

N-edges of the dice, ask you the mathematical expectation of the number of times you need to throw the same (different) n consecutive times.


Solution:

Push the formula using recursive DP


(1) If the query is 0, then:


DP [I] records the mathematical expectation that I have the same number of consecutive times to n different needs.
DP [0] = 1 + dp [1]
DP [1] = 1 + (1/M * DP [2] + (m-1)/m * DP [1]) = 1 + (DP [2] + (1-m) * DP [1])/m;
DP [2] = 1 + (DP [3] + (1-m) * DP [1])/m;
....................
DP [N] = 0


Launch:

DP [I] = 1 + (m-1) * DP [1] + dp [I + 1])/m
DP [I + 1] = 1 + (m-1) * DP [1] + dp [I + 2])/m

Therefore, M * (DP [I + 1]-DP [I]) = (DP [I + 2]-DP [I + 1])

We found that it was an equal-to-ratio series.

DP [0]-DP [1] = 1;
DP [1]-DP [2] = m;
..........
DP [n-1]-DP [N] = m ^ (n-1)

Accumulate, and get: DP [0]-DP [N] = 1 + M + m ^ 2 + .......... m ^ (n-1) = (1-m ^ N)/(1-m)

So: DP [0] = (1-m ^ N)/(1-m );

(2) If the query is 1, then:

DP [0] = 1 + dp [1]
DP [1] = 1 + (DP [1] + m-1) DP [2])/m
DP [2] = 1 + (DP [1] + dp [2] + (m-2) DP [3])/m
DP [I] = 1 + (DP [1] + dp [2] +... DP [I] + (m-I) * DP [I + 1])/m
DP [I + 1] = 1 + (DP [1] + dp [2] +... DP [I] + dp [I + 1] + (m-i-1) * DP [I + 1])/m
...
DP [N] = 0;

Select DP [I] and DP [I + 1] to subtract them.

DP [I]-DP [I + 1] = (m-i-1)/m * (DP [I + 1]-DP [I + 2]);

Therefore DP [I + 1]-DP [I + 2] = m/(m-i-1) * (DP [I]-DP [I + 1]);

Therefore:
DP [0]-DP [1] = 1;
DP [1]-DP [2] = 1 * m-1 );
DP [2]-DP [3] = 1 * m/(S-1) * m/(m-2 );
..........

DP [n-1]-DP [N] = 1 * m/(m-1) * m/(m-2 )*....... * m/(m-n + 1 );

Accumulate to get the answer


Solution code:

#include <iostream>#include <cstdio>#include <cmath>using namespace std;inline double solve(){    int op,m,n;    scanf("%d%d%d",&op,&m,&n);    double ans=0;    if(op==0){        for(int i=0;i<=n-1;i++){            ans+=pow(1.0*m,i);        }    }else{        double tmp=1.0;        for(int i=1;i<=n;i++){            ans+=tmp;            tmp*=m*1.0/(m-i);        }    }    return ans;}int main(){    int t;    while(scanf("%d",&t)!=EOF){        while(t-- >0){            printf( "%.9lf\n",solve() );        }    }    return 0;}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.