NO IMAGE
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 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

題意:

石頭、剪刀、布這3種動物生活在一個荒島上,石頭吃剪刀、剪刀吃布、布吃石頭。現已知這3種動物各有r、s、p只,問等到足夠的時間過後,這島上最後剩下的動物是哪一種(不要想著最後的物種會餓死啦~),求每種動物剩下的概率。

分析:

一開始純搜尋–超時,覺著這資料量只是算出每種概率的話應該不會超時,所以可能是記憶化搜尋?然後寫著寫著總感覺哪不對。。然後發現別人直接dp。。對啊,直接dp也可以啊。。於是瞬間寫出dp表示式。兩種物種相遇的概率是:(x*y)/(x*y x*z y*z)。

code:

#include<bits/stdc  .h>
using namespace std;
typedef long long ll;
#define INF 1<<30
double dp[110][110][110];
double R, S, P;
int main()
{
int r, s, p;
cin >> r >> s >> p;
dp[r][s][p] = 1.0;
for(int i=r; i>=0; i--)
{
for(int j=s; j>=0; j--)
{
for(int k=p; k>=0; k--)
{
double sum = i*j i*k j*k;
if(i && sum) dp[i-1][j][k]  = (i*k)/sum*dp[i][j][k];
if(j && sum) dp[i][j-1][k]  = (i*j)/sum*dp[i][j][k];
if(k && sum) dp[i][j][k-1]  = (j*k)/sum*dp[i][j][k];
}
}
}
for(int i=0; i<=r; i  )
R  = dp[i][0][0];
for(int i=0; i<=s; i  )
S  = dp[0][i][0];
for(int i=0; i<=p; i  )
P  = dp[0][0][i];
printf("%.9f %.9f %.9f\n", R,S,P);
return 0;
}

記憶化搜尋:

#include<stdio.h>
double dp[110][110][110];
double dfs(int r, int s, int p)
{
if(dp[r][s][p]) return dp[r][s][p];
if(p == 0) return 1; //當p==0也就是說,當p滅絕,此時最終留下來的是物種r
if(s*r == 0) return 0; //表示物種r或s滅絕的概率為0,因為我已假設就是p物種滅絕
//一直算到有一種物種滅絕,這樣就可以直接知道誰存活,是不是滿足自己的假設
int sum = r*s r*p s*p;
return dp[r][s][p] = 1.0*r*s/sum*dfs(r,s-1,p) 1.0*r*p/sum*dfs(r-1,s,p) 1.0*s*p/sum*dfs(r,s,p-1);
}
int main()
{
int r, s, p;
scanf("%d%d%d", &r,&s,&p);
printf("%.9f %.9f %.9f\n", dfs(r,s,p),dfs(s,p,r),dfs(p,r,s));
//dfs(i,j,k)表示i存活,算到k滅絕為止
return 0;
}