题意:会出石头、剪刀、布的人分别有r,s,p个,他们相互碰到的概率相同,输的人死掉,问最终活下去的人是三种类型的概率
设状态dp(i,j,k)为还有i个石头,j个剪刀,k个布时的概率,dp(r,s,p)=1.0
状态转移方程:
d(i-1,j,k)+=d(i,j,k)*(i*k)/(i*j+i*k+j*k);
d(i,j-1,k)+=d(i,j,k)*(i*j)/(i*j+i*k+j*k);
d(i,j,k-1)+=d(i,j,k)*(j*k)/(i*j+i*k+j*k);
因为状态dp(i,j,k)可以由dp(i+1,j,k)、dp(i,j+1,k)和dp(i,j,k+1)转移过来,所以用+=
一个三重循环解决,复杂度O(n^3)
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<stack> #include<queue> #include<deque> #include<set> #include<vector> #include<map> #include<functional>#define fst first #define sc second #define pb push_back #define mp(a,b) make_pair(a,b) #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lc root<<1 #define rc root<<1|1 #define lowbit(x) ((x)&(-x)) using namespace std;typedef double db; typedef long double ldb; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> PI; typedef pair<ll,ll> PLL;const int maxn = 5e5 + 100; const int maxm = 5e5 + 100; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-6; inline int read(){int num;char ch;while((ch=getchar())<'0' || ch>'9');num=ch-'0';while((ch=getchar())>='0' && ch<='9'){num=num*10+ch-'0';}return num; } double dp[101][101][101]; int main(){int r, s ,p;scanf("%d %d %d", &r, &s, &p);mem(dp, 0);dp[r][s][p] = 1;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;printf("%d %d %d %lf\n", i, j, k, dp[i][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, ans2, ans3;ans1 = ans2 = ans3 = 0;for(int i = 1; i <= 100; i++){for(int j = 1; j <= 100; j++){ans1 += dp[i][j][0];ans2 += dp[0][i][j];ans3 += dp[j][0][i];}}printf("%.17lf %.17lf %.17lf", ans1, ans2, ans3);return 0; } /**/