Codeforces 540D Bad Luck Island【概率dp】

来源:互联网 发布:数据库后台做什么 编辑:程序博客网 时间:2024/04/29 12:36

D. Bad Luck Island
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors and p papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper, and paper kills a rock. Your task is to determine for each species what is the probability that this species will be the only one to inhabit this island after a long enough period of time.

Input

The single line contains three integers r,s and p (1 ≤ r, s, p ≤ 100) — the original number of individuals in the species of rock, scissors and paper, respectively.

Output

Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed10 - 9.

Examples
Input
2 2 2
Output
0.333333333333 0.333333333333 0.333333333333
Input
2 1 2
Output
0.150000000000 0.300000000000 0.550000000000
Input
1 1 3
Output
0.057142857143 0.657142857143 0.285714285714

题目大意:


现在有石头r人,剪刀s人,布p人.

每次会有两个不同阵营的人见面,然后一个人可能会出局。

问最终三个队获胜的几率。

一个队获胜意味着其他队的人都死了。


思路:


观察到r,s,p均小于100.那么很明显,设定dp【i】【j】【k】表示石头还剩下i人,剪刀还剩下j人,布还剩下k人的几率。

那么就有:

①dp【i-1】【j】【k】+=dp【i】【j】【k】*这种情况的概率;

②dp【i】【j-1】【k】+=dp【i】【j】【k】*这种情况的概率;

③dp【i】【j】【k-1】+=dp【i】【j】【k】*这种情况的概率;

很明显,三种情况的概率分别为:

①i*k/(i*j+j*k+i*k);石头和布一起出现的时候,石头才可能死一个人。

②i*j/(i*j+j*k+i*k);石头和剪子一起出现的时候,剪子才可能死一个人。

③j*k/(i*j+j*k+i*k);剪子和布一起出现的时候,布才可能死一个人。


那么过程维护一下dp数组,注意三层for的枚举方向即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;double dp[105][105][105];double cal(int a,int b,int c){    double aa=(double)a;    double bb=(double)b;    double cc=(double)c;    if(aa*bb+bb*cc+cc*aa==0)return 0;    else    {        return  (aa*bb)/(aa*bb+bb*cc+cc*aa);    }}int main(){    int r,s,p;    while(~scanf("%d%d%d",&r,&s,&p))    {        memset(dp,0,sizeof(dp));        dp[r][s][p]=1;        for(int i=r;i>=0;i--)        {            for(int j=s;j>=0;j--)            {                for(int k=p;k>=0;k--)                {                    if(i-1>=0)dp[i-1][j][k]+=dp[i][j][k]*cal(i,k,j);                    if(j-1>=0)dp[i][j-1][k]+=dp[i][j][k]*cal(i,j,k);                    if(k-1>=0)dp[i][j][k-1]+=dp[i][j][k]*cal(j,k,i);                }            }        }        double a,b,c;        a=0;b=0;c=0;        for(int i=0;i<=r;i++)a+=dp[i][0][0];        for(int i=0;i<=s;i++)b+=dp[0][i][0];        for(int i=0;i<=p;i++)c+=dp[0][0][i];        printf("%.12lf %.12lf %.12lf\n",a,b,c);    }}






0 0
原创粉丝点击