跳到正文

题解

扔骰子问题 1

2huk约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年8月23日
收录
2025年12月12日
更新
2026年10月24日
篇幅
约 3 分钟
获赞
4
题目
B3718

版本

  1. v1当前

Description

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

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

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

Solution

首先我们要从 nn 个骰子中选出 mm 个骰子,其方案数为 (nm)\dbinom nm。

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

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

因此答案为:

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

Code

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

  • 组合数可以预处理阶乘和阶乘的逆元;

  • (16)m\left( \dfrac 16 \right)^m 可以写作 16m\dfrac 1{6^m},因此需要预处理 66 的次幂的逆元。

  • (56)n−m\left( \dfrac 56 \right)^{n - m} 可以写作 5n−m6n−m\dfrac {5^{n - m}}{6^{n - m}} ,因此需要预处理 55 的次幂。

C++
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;
}
原文