Codeforces B. 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 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
Copy
2 2 2
Output
Copy
0.333333333333 0.333333333333 0.333333333333
Input
Copy
2 1 2
Output
Copy
0.150000000000 0.300000000000 0.550000000000
Input
Copy
1 1 3
Output
Copy
0.057142857143 0.657142857143 0.285714285714
思路:
这道题刚看的时候觉得好神奇啊,一定要用到好高大上的概率知识,但是我的数学已经丢完了,只能跳过了。知道了解法之后更觉得神奇,啊~神奇的概率dp。
定义dp[i][j][k]是物种石头剩下i个,剪刀剩下j个,布剩下k个的情形发生的概率。刚开始时dp[r][s][p]=1,现在考虑状态转移。
什么时候剪刀会少一个呢?它遇到了石头,这种情况发生的概率呢?\(\frac{C_s^1*C_r^1}{C_{r+s+p}^2}\),同理少一个布呢?它遇到了剪刀,概率呢?\(\frac{C_p^1*C_s^1}{C_{r+s+p}^2}\),石头少一个的概率是\(\frac{C_p^1*C_r^1}{C_{r+s+p}^2}\),那么状态转移方程是
\[sum=sr+pr+sp\]
\[dp[i-1][j][k]+=dp[i][j][k]*\frac{C_s^1*C_r^1}{sum}\]
\[dp[i][j-1][k]+=dp[i][j][k]*\frac{C_p^1*C_r^1}{C_{sum}^2}\]
\[dp[i][j][k-1]+=dp[i][j][k]*\frac{C_p^1*C_s^1}{C_{sum}^2}\]
从头开始递推即可。
最后统计答案的时候统计dp[i][j][0]这种的答案,因为这种答案的胜负已定,只能存在一个物种
代码:
#include <iostream>
#include <iomanip>
#define max_n 105
using namespace std;
int r,s,p;
double dp[max_n][max_n][max_n];
int main()
{
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;
dp[i-1][j][k] += dp[i][j][k]*(double)(i*k)/sum;
dp[i][j-1][k] += dp[i][j][k]*(double)(i*j)/sum;
dp[i][j][k-1] += dp[i][j][k]*(double)(j*k)/sum;
}
}
}
double ans1 = 0;
double ans2 = 0;
double ans3 = 0;
for(int i = 0;i<=100;i++)
{
for(int j = 0;j<=100;j++)
{
ans1 += dp[i][j][0];
ans2 += dp[0][i][j];
ans3 += dp[i][0][j];
}
}
cout.setf(ios_base::fixed,ios_base::floatfield);
cout << setprecision(12) << ans1 << " " << ans2 << " " << ans3 << endl;
return 0;
}
标签:frac,int,double,sum,Codeforces,Bad,Island,species,dp 来源: https://www.cnblogs.com/zhanhonhao/p/11332955.html