扔骰子问题 1

· · 题解

Description

有 n 个六面骰子,投掷时,每个面朝上的概率均相同,且每个骰子的投掷结果相互独立。

现在投掷出这些骰子,请问恰好有 m 个骰子的朝上面为一号面(仅有一个点的面)的概率是多少?

请你求出这个概率对 998,244,353 取模的结果。

Solution

首先我们要从 n 个骰子中选出 m 个骰子,其方案数为 \dbinom nm。

其次,对于选出的 m 个骰子而言,每个都有 \dfrac 16 的概率朝上的面为 1。因此 m 个骰子都朝上的概率为 \left( \dfrac 16 \right)^m。

再其次,对于没有选出的 n - m 个骰子而言,由于是 1 的都已经被选走了,因此它们只能填 2 \sim 6 中的一个数,也就是有 \dfrac 56 的概率合法。因此它们全部合法的概率为 \left( \dfrac 56 \right)^{n - m}。

因此答案为:

\dbinom nm \times \left( \dfrac 16 \right)^m \times \left( \dfrac 56 \right)^{n - m}

Code

对于这三部分答案,可以分别来求:

typedef long long LL;

const int N = 5e6 + 10;

int fpm(int a, int b)
{
    int res = 1;
    while (b)
    {
        if (b & 1) res = (LL)res * a % P;
        b >>= 1, a = (LL)a * a % P;
    }
    return res;
}

int T, n, m;
int k, res;
int fac[N], inv[N];
int p5[N];      // p5[i] = 5 ^ i
int inv6[N];    // inv6[i] = 6 ^ i 的逆元

void init(int n)
{
    fac[0] = p5[0] = 1;
    fup (i, 1, n)
        fac[i] = (LL)fac[i - 1] * i % P;

    inv[n] = fpm(fac[n], P - 2);
    fdw (i, n - 1, 0)
        inv[i] = (LL)inv[i + 1] * (i + 1) % P;

    fup (i, 1, n)
        p5[i] = (LL)p5[i - 1] * 5 % P;

    inv6[n] = fpm(fpm(6, n), P - 2);
    fdw (i, n - 1, 0)
        inv6[i] = (LL)inv6[i + 1] * 6 % P;
}

int C(int n, int m)
{
    return (LL)fac[n] * inv[m] % P * inv[n - m] % P;
}

int main()
{
    init(N - 1);

    puts("tou");        // 骰 念 tou
    T = read();

    while (T -- )
    {
        n = read(), m = read();
        k = (LL)C(n, m) * inv6[m] % P * p5[n - m] % P * inv6[n - m] % P;
        res ^= k;
    }

    write(res);

    return 0;
}