这里提供一个自认为很简单的做法。
一道长得很像的题P4548 [CTSC2006]歌唱王国。
然后有一个有趣的理论。
因为这道题里硬币出正反面的概率是不等的,所以我们把赌场的赔率改一下。
举个例子,如果正面的概率是 ,反面的概率是 ,那么如果一个赌徒用 元压了正面,那么如果它赌对了将获得 元,如果压反面赌对了将获得 元。
可以看到我们的这个赌博还是“公平”的。
再叙述一下我们开赌场的过程:
假设当前的指定序列是 。(设 1 为正面 0 为反面)
每时刻会进来一位带着 元钱的赌徒,他会把自己的全部钱财压在 上,如果压中了再在下一个时间单位把把全部的钱压在 上……直到一次压错了退场或一直压到 最后一位大胜离场。
假设现在第 位赌徒第一个完成了直到 的最后一位全部压中的成就,在这时我们关闭赌场,思考一下一共会有多少赌徒带着多少钱离开赌场。
第 位带着 元离场。
第 位带着 元离场。(他在压 时第 名赌徒正好在压 ,而 和 是相等的)
第 为带着 元离场。( 和 相等)
看到这你应该已经想到用 KMP 来匹配满足 的 了。(设序列长度为 )
我们的赌场应该是不亏不赚的,而一共有 元被带了出去,那么也应该有这么多钱被带进来。
而每时刻都会有 元钱被带进来,所以总的时间期望就是 。
因为答案要保留分数,所以别忘了写高精属实毒瘤。
tips:因为所有的分子分母都是 100 以内的数乘出来的,所以化简时不用去求 ,只要用 之间的数去试除就好了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
const int MAXN = 2e3 + 50;
const int base = 100000000;
inline int gcd(int x, int y)
{
if (!y)
return x;
return gcd(y, x % y);
}
struct pp
{
int a[MAXN];
int siz;
void pushup()
{
for (int i = 0; i < siz; ++i)
if (a[i] >= base)
{
a[i + 1] += a[i] / base;
a[i] %= base;
if (i == siz - 1)
siz++;
}
}
void pushdown()
{
for (int i = siz - 1; i; --i)
{
if (a[i])
break;
siz--;
}
}
void print()
{
printf("%lld", a[siz - 1]);
for (int i = siz - 2; i >= 0; --i)
{
printf("%08lld", a[i]);
}
}
};
pp operator+(pp a, pp b)
{
if (a.siz < b.siz)
swap(a, b);
for (int i = 0; i < a.siz; ++i)
a.a[i] += b.a[i];
a.pushup();
return a;
}
pp operator-(pp a, pp b)
{
int siz = max(a.siz, b.siz);
for (int i = 0; i < siz; ++i)
a.a[i] -= b.a[i];
for (int i = 0; i < siz; ++i)
if (a.a[i] < 0)
{
a.a[i] += base;
a.a[i + 1]--;
}
a.pushdown();
return a;
}
pp operator*(pp a, pp b)
{
pp c;
memset(c.a, 0, sizeof(c.a));
c.siz = a.siz + b.siz;
for (int i = 0; i < a.siz; ++i)
for (int j = 0; j < b.siz; ++j)
c.a[i + j] += a.a[i] * b.a[j];
c.pushup();
c.pushdown();
return c;
}
pp operator*(pp a, int b)
{
for (int i = 0; i < a.siz - 1; ++i)
a.a[i] *= b;
a.pushup();
return a;
}
pp operator/(pp a, int b)
{
pp c;
memset(c.a, 0, sizeof(c.a));
c.siz = a.siz;
int now = 0;
for (int i = a.siz - 1; i >= 0; --i)
{
now = now * base + a.a[i];
if (now >= b)
{
c.a[i] = now / b;
now %= b;
}
}
c.pushdown();
return c;
}
int operator%(pp a, int b)
{
int now = 0;
for (int i = a.siz - 1; i >= 0; --i)
{
now = now * base + a.a[i];
if (now >= b)
now %= b;
}
return now;
}
struct node
{
pp a, b;
void simp()
{
for (int i = 2; i <= 100; ++i)
{
// a.print();
// cout << " ";
// b.print();
// cout << i << ":";
// cout << a % i << " " << b % i << endl;
while (!(a % i) && !(b % i))
{
// cout << i << ":";
// a.print();
// cout << "*";
// b.print();
// cout << endl;
a = a / i;
b = b / i;
}
}
}
node(){}
node(int x, int y)
{
memset(a.a, 0, sizeof(a.a));
memset(b.a, 0, sizeof(b.a));
// cout << x << " " << y << " ---" << endl;
a.a[0] = x;
b.a[0] = y;
a.siz = 1;
b.siz = 1;
// cout << "--" << endl;
simp();
}
void print()
{
a.print();
putchar('/');
b.print();
putchar('\n');
}
};
node operator+(node x, node y)
{
node c;
c.a = x.a * y.b + y.a * x.b;
c.b = y.b * x.b;
c.simp();
return c;
}
node operator*(node x, node y)
{
node c;
c.a = x.a * y.a;
c.b = x.b * y.b;
c.simp();
return c;
}
int N;
node p1, p2, n1, n2;
node pren[MAXN];
char s[MAXN];
int nxt[MAXN];
signed main()
{
int t1, t2;
scanf("%lld%lld", &t1, &t2);
n1 = node(t2, t1);
n2 = node(t2, t2 - t1);
// n1.print();
// n2.print();
// cout << "---" << endl;
scanf("%s", s + 1);
N = strlen(s + 1);
for (int i = 2, j = 0; i <= N; ++i)
{
while (j && s[j + 1] != s[i])
j = nxt[j];
if (s[j + 1] == s[i])
++j;
nxt[i] = j;
}
// cout << "--" << endl;
pren[0] = node(1, 1);
for (int i = 1; i <= N; ++i)
{
if (s[i] == 'H')
pren[i] = pren[i - 1] * n1;
else
pren[i] = pren[i - 1] * n2;
// pren[i].print();
}
node ans = node(0, 1);
for (int i = N; i; i = nxt[i])
ans = ans + pren[i];
ans.print();
return 0;
}