CodeForces 540D Bad Luck Island (概率dp)
2015-11-20 17:43
344 查看
http://codeforces.com/problemset/problem/540/D
Bad Luck Island
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.
Sample test(s)
input
output
input
output
input
output
题意:
有r个石头,s个剪刀,p个布在一个岛上。问在足够长的时间里,石头,剪刀,布分别存活下来的概率。
思路:
定义dp[i][j][k]:还剩下i个石头,s个剪刀,p个布的概率。
初始dp[r][s][p] = 1;
转移:
总情况数:sum= i*j+j*k+i*k;
则dp[i-1][j][k] = (i*k/sum)*dp[i][j][k];dp[i][j-1][k] = (i*j/sum)*dp[i][j][k];dp[i][j][k-1] = (j*k/sum)*dp[i][j][k];
如果只剩下两种生物在岛上,则可以不用转移了。因为任意两种生物遇到可以肯定谁赢。
因此最后求和(看代码)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define N 110
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))
double dp
;
int main ()
{
int r, s, p;
while (scanf ("%d %d %d", &r, &s, &p) != EOF)
{
met (dp, 0);
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--)
{
double sum = i*j + j*k + i*k;
dp[i-1][j][k] += dp[i][j][k] * i*k*1.0/sum;
dp[i][j-1][k] += dp[i][j][k] * j*i*1.0/sum;
dp[i][j][k-1] += dp[i][j][k] * k*j*1.0/sum;
}
double ans1 = 0, ans2 = 0, ans3 = 0;
for (int i=1; i<=100; i++)
for (int j=0; j<=100; j++)
{
ans1 += dp[i][j][0];
ans2 += dp[0][i][j];
ans3 += dp[j][0][i];
}
printf ("%.10f %.10f %.10f\n", ans1, ans2, ans3);
}
return 0;
}
Bad Luck Island
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.
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个石头,s个剪刀,p个布的概率。
初始dp[r][s][p] = 1;
转移:
总情况数:sum= i*j+j*k+i*k;
则dp[i-1][j][k] = (i*k/sum)*dp[i][j][k];dp[i][j-1][k] = (i*j/sum)*dp[i][j][k];dp[i][j][k-1] = (j*k/sum)*dp[i][j][k];
如果只剩下两种生物在岛上,则可以不用转移了。因为任意两种生物遇到可以肯定谁赢。
因此最后求和(看代码)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
#define N 110
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))
double dp
;
int main ()
{
int r, s, p;
while (scanf ("%d %d %d", &r, &s, &p) != EOF)
{
met (dp, 0);
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--)
{
double sum = i*j + j*k + i*k;
dp[i-1][j][k] += dp[i][j][k] * i*k*1.0/sum;
dp[i][j-1][k] += dp[i][j][k] * j*i*1.0/sum;
dp[i][j][k-1] += dp[i][j][k] * k*j*1.0/sum;
}
double ans1 = 0, ans2 = 0, ans3 = 0;
for (int i=1; i<=100; i++)
for (int j=0; j<=100; j++)
{
ans1 += dp[i][j][0];
ans2 += dp[0][i][j];
ans3 += dp[j][0][i];
}
printf ("%.10f %.10f %.10f\n", ans1, ans2, ans3);
}
return 0;
}
相关文章推荐
- Spring Boot:在Spring Boot中使用Mysql和JPA
- Linux+PHP+GD
- Codeforces Round #330 (Div. 2)C. Warrior and Archer(博弈,贪心)
- Android ListView的Item点击失效问题
- 关于Android M RuntimePermission的问题
- Android 如何调用自写APK和非自写APK
- quartz设置时间表达式
- CAS4 之 集成RESTful API
- react js分析2
- 插入mysql时重复数据处理方法
- 普法知识(6):普法教育电子化
- tomcat内存溢出
- iOS开发者面试必备题汇总
- MT7620原生SDK支持HUAWEI 4G dongle
- ZOJ 3551 Bloodsucker [概率DP]
- linux下rsync命令参数讲解整理
- Android判断屏幕锁屏的方法总结
- laravel5——事件
- 黑马程序员——Java基础---面向对象(一)
- 安装lamp进行一些修改