标签:end namespace putchar inline lin type mrc tac mem
嘟嘟嘟
友情提示:数据把\(hp1\)和\(hp2\)弄反了!
进入正题。
这题还是比较好想,令\(dp[i][j]\)表示第一个人赢了\(i\)场,第二个人赢了\(j\)的概率,转移就是分别考虑这一场谁赢了。
所以我们要预处理两个人赢的概率。显然有\(winA = \sum _ {i = 1} ^ 6 \sum _ {j = 1} ^ {i - 1} p1[i] * p2[j]\),\(winB = \sum _ {i = 1} ^ 6 \sum _ {j = i + 1} ^ {n} p1[i] * p2[j]\)。但这两种概率加起来并不是1,因为还得考虑平的概率,即\(equal = \sum_ {i = 1} ^ {n} p1[i] * p2[i]\)。所以\(winA' = \frac{winA}{1 - equal}\),\(winB' = \frac{winB}{1 - equal}\)。
(实际上\(equal\)就是\(1 - winA - winB\),把上面的式子加起来就得证了)
注意的是边界条件。一是\(dp[m][i]\)不能从\(dp[m][i - 1]\)转移过来,因为第二个人已经死了。二是每一次循环\(j\)的范围是\([0, n - 1]\),原因和第一条同理。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e3 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
}
int n, m;
db p1[maxn], p2[maxn], dp[maxn][maxn];
int main()
{
//MYFILE();
while(scanf("%d%d", &m, &n) != EOF)
{
for(int i = 1; i <= 6; ++i) scanf("%lf", &p1[i]);
for(int i = 1; i <= 6; ++i) scanf("%lf", &p2[i]);
db winA = 0, winB = 0, eqa = 0;
for(int i = 1; i <= 6; ++i)
for(int j = 1; j <= 6; ++j)
{
if(i > j) winA += p1[i] * p2[j];
if(i < j) winB += p1[i] * p2[j];
if(i == j) eqa += p1[i] * p2[j];
}
winA /= (1 - eqa);
winB /= (1 - eqa);
dp[0][0] = 1;
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
{
if(!i && !j) continue;
dp[i][j] = 0;
if(i > 0) dp[i][j] += dp[i - 1][j] * winA;
if(j > 0) dp[i][j] += dp[i][j - 1] * winB;
}
db ans = 0;
for(int i = 0; i < n; ++i) ans += dp[m - 1][i] * winA;
printf("%.6lf\n", ans);
}
return 0;
}
标签:end namespace putchar inline lin type mrc tac mem
原文地址:https://www.cnblogs.com/mrclr/p/10888139.html