Bzoj2318 Game With Probability Problem

Description

https://www.luogu.com.cn/problem/SP4060

注意到 $p,q\ge 0.5$。

Solution

感觉题解说的都不是很明白,都是建立在他们同时选 $p,q$ 或者同时选 $1-p,1-q$ 上的。

前面都和题解区一样,令 $p’,q’$ 表示他们抛硬币向上的概率,并不是题目中的 $p,q$。$f_i,g_i$ 表示第 $i$ 轮 $A,B$ 分别先手,$A$ 的胜率。有转移:
$$
f_i=\dfrac{p’g_{i-1}+q’(1-p’)f_{i-1}}{1-(1-p’)(1-q’)}
$$
$g_i$ 同理。

如果 $f_{i-1}<g_{i-1}$,自然的 A 要取,转移到 $g_{i-1}$。这时候对于 B 的胜率 $1-f_{i-1}>1-g_{i-1}$,所以 B 也想取。此时两者都是取的概率 $p,q$。

反之为 $1-p,1-q$。

观察到这种简单博弈在很多轮以后都不太会变。精度要求比较低的时候做到 $1000$ 左右即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define maxn
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
const int mod=1e9+7;
int power(int x,int y=mod-2) {
int sum=1;
while (y) {
if (y&1) sum=sum*x%mod;
x=x*x%mod;y>>=1;
}
return sum;
}
int n;
double f[1005][2];
double p,q;
void solve(void) {
int i;
scanf("%d%lf%lf",&n,&p,&q);
f[0][0]=0;f[0][1]=1;n=min(n,1000);
for (i=1;i<=n;i++) {
double a=p,b=q;
if (f[i-1][1]<f[i-1][0]) a=1-a,b=1-b;
f[i][0]=(a*f[i-1][1]+f[i-1][0]*(1-a)*b)/(1-(1-a)*(1-b));
f[i][1]=(b*f[i-1][0]+f[i-1][1]*(1-b)*a)/(1-(1-a)*(1-b));
// gdb(i,f[i][0],f[i][1]);
}
printf("%.6lf\n",f[n][0]);
}
signed main(void){
int T
read(T);
while (T--) solve();
return 0;
}