Codeforces 540D Bad Luck Island【概率dp】

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 exceed 10 - 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);
    }
}






引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值