CF 540D——Bad Luck Island——————【概率dp】

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 rs 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 exceed 10 - 9.

Sample test(s)
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个人。三类人中最后会剩下一种人的概率分别是多少。
定义dp[i][j][k]表示剩下出石头的i个,出剪刀的j个,出布的k个的概率。dp[i][j][k]=dp[i+1][j][k]*pi+dp[i][j+1][k]*pj+dp[i][j][k+1]*pk。

#include<bits/stdc++.h>
using namespace std;
double dp[110][110][110];
int main(){
    int r,s,p;
    double sum;
    while(scanf("%d%d%d",&r,&s,&p)!=EOF){
        memset(dp,0,sizeof(dp));
        dp[r][s][p]=1.0;
        for(int i=r;i>=1;i--){
            for(int j=s;j>=1;j--){
                for(int k=p;k>=1;k--){
                    sum=i*j+j*k+i*k*1.0;
                    dp[i-1][j][k]+=dp[i][j][k]*(i*k*1.0/sum);
                    dp[i][j-1][k]+=dp[i][j][k]*(i*j*1.0/sum);
                    dp[i][j][k-1]+=dp[i][j][k]*(j*k*1.0/sum);
                }
            }
        }
        double ans1,ans2,ans3;
        ans1=ans2=ans3=0;
        for(int i=r;i>=1;i--){
            for(int j=s;j>=0;j--){
                ans1+=dp[i][j][0];
            }
        }
        for(int j=s;j>=1;j--){
            for(int k=p;k>=0;k--){
                ans2+=dp[0][j][k];
            }
        }
        for(int i=r;i>=0;i--){
            for(int k=p;k>=1;k--){
                ans3+=dp[i][0][k];
            }
        }
        printf("%.10f %.10f %.10f
",ans1,ans2,ans3);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/chengsheng/p/4731920.html