跳到正文

题解

P3334 [ZJOI2013]抛硬币 (概率+鞅论+高精)

DrBit约 11 分钟
原文
文章信息与版本

文章信息

发布
2021年3月24日
收录
2026年10月23日
更新
2026年11月13日
篇幅
约 11 分钟
获赞
14
题目
P3334

版本

  1. v1当前

题面

这里提供一个自认为很简单的做法。

一道长得很像的题P4548 [CTSC2006]歌唱王国。

然后有一个有趣的理论。

因为这道题里硬币出正反面的概率是不等的,所以我们把赌场的赔率改一下。

举个例子,如果正面的概率是 13\frac{1}{3},反面的概率是 23\frac{2}{3},那么如果一个赌徒用 xx 元压了正面,那么如果它赌对了将获得 3x3x 元,如果压反面赌对了将获得 32x\frac{3}{2} x 元。

可以看到我们的这个赌博还是“公平”的。

再叙述一下我们开赌场的过程:

假设当前的指定序列是 S={1,0,0,1,0,0,1}S=\{ 1,0,0,1,0,0,1 \}。(设 1 为正面 0 为反面)

每时刻会进来一位带着 11 元钱的赌徒,他会把自己的全部钱财压在 S1S_1 上,如果压中了再在下一个时间单位把把全部的钱压在 S2S_2 上……直到一次压错了退场或一直压到 SS 最后一位大胜离场。

假设现在第 NN 位赌徒第一个完成了直到 SS 的最后一位全部压中的成就,在这时我们关闭赌场,思考一下一共会有多少赌徒带着多少钱离开赌场。

第 NN 位带着 33×(32)43^3 \times (\frac{3}{2})^4 元离场。

第 N+3N+3 位带着 32×(32)23^2 \times (\frac{3}{2})^2 元离场。(他在压 S1S_1 时第 NN 名赌徒正好在压 S4S_4,而 S[1,4]S_{[1,4]} 和 S[4,7]S_{[4,7]} 是相等的)

第 N+6N+6 为带着 33 元离场。(S[1,1]S_{[1,1]} 和 S[7,7]S_{[7,7]} 相等)

看到这你应该已经想到用 KMP 来匹配满足S[1,a]=S[N−a+1,N]S_{[1,a]} =S_{[N-a+1,N]} 的 aa 了。(设序列长度为 NN)

我们的赌场应该是不亏不赚的,而一共有 33×(32)4+32×(32)2+3 3^3 \times (\frac{3}{2})^4 + 3^2 \times (\frac{3}{2})^2 + 3 元被带了出去,那么也应该有这么多钱被带进来。

而每时刻都会有 11 元钱被带进来,所以总的时间期望就是 33×(32)4+32×(32)2+3 3^3 \times (\frac{3}{2})^4 + 3^2 \times (\frac{3}{2})^2 + 3 。

因为答案要保留分数,所以别忘了写高精属实毒瘤。

tips:因为所有的分子分母都是 100 以内的数乘出来的,所以化简时不用去求 gcd⁡\gcd,只要用 [1,100][1,100] 之间的数去试除就好了。

C++
#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;
}
原文
存档评论2 条抓取于