Calculate expectation with $fail$.

Description

Given a probability $p$ of throwing a coin and the $H$ surface is on the top. Accordingly, $1-p$ of $T$ surface.

Now given a target sequence consisting of $H$ and $T$, calculate how many times (in expectation) you need to make the result as such a sequence.

Solution

The target sequence must be the suffix of our result, so must a substring. We can evaluate how our result transforms when trying to fit the target sequence.

Equation is :

$f(i) = f(i-1) + 1 + (1-p_i)(f(i) - f(file_i))$.

Swap items to get expression of $f(i)$.

Need to implement high-precision number class.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1005;

typedef long long ll;

int n, a, b;
int nxt[maxn], tow[maxn];
char s[maxn];

struct bigint
{
static const int width = 9;
static const ll base = 1000000000;
ll a[1005]; int len;
bigint() {
memset(a, 0, sizeof a); len = 1;
}
ll& operator [] (int x) {
return a[x];
}
bigint(int x) {
len = 0;
if (!x) len = 1;
else {
while (x) a[++len] = x % base, x /= base;
}
}
bigint operator + (bigint b) {
bigint ret; ret.len = max(len, b.len);
for (int i = 1; i <= ret.len; ++i) {
ret[i] = a[i] + b[i];
//ret[i + 1] += ret[i] / base;
//ret[i] %= base;
}

for (int i = 1; i <= ret.len; ++i)
ret[i + 1] += ret[i] / base, ret[i] %= base;
for (; ret[ret.len + 1]; ret.len++) ret[ret.len + 2] += ret[ret.len + 1] / base, ret[ret.len + 1] %= base;
/*while (ret[ret.len] >= base) {
ret[ret.len + 1] += ret[ret.len] / base;
ret[ret.len] %= base;
ret.len++;
}*/
return ret;
}
bigint operator * (bigint b) {
bigint ret;
for (int i = 1; i <= len; ++i)
for (int j = 1; j <= b.len; ++j)
ret[i + j - 1] += a[i] * b[j], ret[i + j] += ret[i + j - 1] / base, ret[i + j - 1] %= base;
ret.len = len + b.len - 1;
/*while (ret[ret.len] >= base) {
ret[ret.len + 1] += ret[ret.len] / base;
ret[ret.len] %= base;
ret.len++;
}*/
for (; ret[ret.len + 1]; ret.len++) ret[ret.len + 2] += ret[ret.len + 1] / base, ret[ret.len + 1] %= base;
return ret;
}
bigint operator / (int b) {
bigint ret; ret.len = len; ll bit = 0;
for (int i = len; i; --i) {
ret[i] = (a[i] + bit * base) / b;
bit = (bit * base + a[i]) % b;
}
while (!ret[ret.len]) ret.len--;
return ret;
}
bigint operator - (bigint b) {
bigint ret; ret.len = max(len, b.len);
for (int i = 1; i <= ret.len; ++i) ret[i] = a[i] - b[i];

for (int i = 1; i <= ret.len; ++i) while (ret[i] < 0) ret[i + 1]--, ret[i] += base;
while (!ret[ret.len]) ret.len--;
return ret;
}
bool operator == (bigint b) {
if (len != b.len) return false;
for (int i = 1; i <= len; ++i) if (a[i] != b[i]) return false;
return true;
}
friend bool operator % (bigint a, int b) {
/*if (b == 4 || b == 2 || b == 3 || b == 6) {
(a / b * bigint(b)).print(); putchar('\n');
a.print(); putchar('\n');
printf("%d\n", b);
}*/
return a / b * bigint(b) == a;
}
inline void print() {
printf("%lld", a[len]);
for (int i = len - 1; i; --i)
printf("%09lld", a[i]);
}
};
struct frac
{
bigint up, dn;
frac() {
//up = bigint(0); dn = bigint(0);
}
frac(int u, int d) : up(u), dn(d) {}
frac(bigint u, bigint d) : up(u), dn(d) {}
frac operator + (frac b) {
frac ret = frac(up * b.dn + b.up * dn, dn * b.dn);
//ret.print();
//ret.cancel();
return ret;
}
frac operator - (frac b) {
frac ret = frac(up * b.dn - b.up * dn, dn * b.dn);
//ret.print();
//ret.cancel();
return ret;
}
frac operator * (frac b) {
//if (up * b.up == bigint(0)) return frac(0, 1);
frac ret = frac(up * b.up, dn * b.dn);
//ret.print();
//ret.cancel();
return ret;
}
frac operator / (frac b) {
frac ret = frac(up * b.dn, dn * b.up);
//ret.print();
//ret.cancel();
return ret;
}
inline void cancel() {
//this->print();
for (int i = 2; i <= 100; ++i) {

while (up % i && dn % i) {
up = up / i, dn = dn / i;
}
}
}
void print() {
up.print(); putchar('/'); dn.print();
putchar('\n');
}
};
frac p[2], f[maxn];

void getnxt() {
int j = 0;
nxt[0] = 0;
tow[1] = 0;
s[0] = '%';
for (int i = 1; i <= n; ++i) {
j = nxt[i - 1];
while (j && s[j + 1] != s[i]) j = nxt[j];
if (i > 1 && s[j + 1] == s[i]) nxt[i] = j + 1, j++;
else nxt[i] = 0;
if (i < n) {
while (j && s[j + 1] == s[i + 1]) j = nxt[j];
if (s[j + 1] != s[i + 1]) tow[i + 1] = j + 1;
else tow[i + 1] = 0;
}
}
}

int main() {
//freopen("3213.out", "w", stdout);
scanf("%d%d", &a, &b);
p[1] = frac(a, b); p[0] = frac(b - a, b);
//p[1].cancel();
//p[0].cancel();
scanf("%s", s + 1);
n = strlen(s + 1);
getnxt();
f[0] = frac(0, 1);
//frac t1 = frac(2147483646, 2147483645);
//frac t2 = t1;
//frac t3 = t2 * t1;
//t3.print();
/*frac t1 = frac(2, 3), t2 = frac(1, 3);
(t1 + t2).print();
frac t3 = t1 + t2; t3.cancel(); t3.print();
t3 = t1 / t2; t3.cancel(); t3.print();
t3 = t1 - t2; t3.cancel(); t3.print();*/
for (int i = 1; i <= n; ++i) {
int typ = (s[i] == 'H');
//printf("%d\n", tow[i]);
//f[tow[i]].print();
f[i] = (f[i - 1] - (p[typ ^ 1] * f[tow[i]]) + frac(1, 1)) / p[typ];
//f[i].print();
f[i].cancel();
//f[i].print();
}
f[n].print();
return 0;
}