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}\),那么状态转移方程是
\]
\]
\]
\]
从头开始递推即可。
最后统计答案的时候统计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;
}
最新文章
- [Android]Volley源码分析(五)
- NLP中word2vec的CBOW模型和Skip-Gram模型
- MVCC PostgreSQL实现事务和多版本并发控制的精华
- Python开发【第四章】:Python函数剖析
- 利用Java进行MySql数据库的导入和导出
- Linux配置apache等系列
- 图算法之Floyd-Warshall 算法-- 任意两点间最小距离
- HDU-4618 Palindrome Sub-Array 暴力枚举
- 初识 Angular 体会
- ASP.NET MVC应用程序把文字写在图片上
- 在 redhat 6.4上安装Python 2.7.5
- 201521123121 《Java程序设计》第9周学习总结
- Cocos2D v2.0至v3.x简洁转换指南(四)
- idea 通过命令操作git
- 通过java递归思想实现以树形方式展现出该目录中的所有子目录和文件
- pycharm技巧
- MySQL(3)-MySQL Workbench
- Bungee Jumping---hdu1155(物理题)
- Educational Codeforces Round 22 E. Army Creation 主席树 或 分块
- window.location.href无法跳转