跳到正文

题解

Solution of P10986 [蓝桥杯 2023 国 Python A] 2023

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

文章信息

发布
2024年8月26日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 3 分钟
题目
P10986

版本

  1. v1当前

你可以浏览这个题解的中文版本。

Consider the Inclusion-Exclusion Principle.

Let f(x)f(x) be the number of schemes with at least xx substrings 2023\tt2023, and by binomial inversion, the number of schemes with exactly mm substrings 2023\tt2023 is:

ans=∑x(−1)x−m(xm)f(x)\text{ans}=\sum_x(-1)^{x-m}\binom xm f(x)

Consider f(x)f(x) that is, xx numbers of 2023\tt2023 are admixed, and the remaining n−4xn-4x numbers are inserted in the spaced x+1x+1 positions, taking 0∼90\sim 9 for each number. Thus, by the insertion method:

f(x)=10n−4x((n−4x)+(x+1)−1(x+1)−1)f(x)=10^{n-4x}\binom{(n-4x)+(x+1)-1}{(x+1)-1}

Time complexity O(n)\mathcal{O}(n).


Python
N = int(1e6 + 5)
M = 998244353
f = [0 for _ in range(N)]
v = [0 for _ in range(N)]
def qpow(x : int, y : int) -> int: 
	r = 1
	while y > 0:
		# print(r, y, x)
		if y % 2:
			r *= x
			r %= M
		y //= 2
		x *= x
		x %= M
	return r
def init() -> None:
	f[0] = 1
	for i in range(1, N - 5 + 1):
		f[i] = f[i - 1] * i % M
	# print(f)
	v[N - 5] = qpow(f[N - 5], M - 2)
	# print(v[N - 5])
	for i in range(N - 5 - 1, 0 - 1, -1):
		v[i] = v[i + 1] * (i + 1) % M
	# print(v, v[0], v[1], v[2])
def get(a : int, b : int) -> int:
	if a < b:
		return 0
	else:
		# print("get", a, b, (f[a] * v[b] % M) * v[a - b] % M, f[a], v[b])
		return (f[a] * v[b] % M) * v[a - b] % M
def work(x : int) -> int: 
	return get(n - 4 * x + x, x) * qpow(10, n - 4 * x) % M
init()
line = list(map(int, input().split()))
n = line[0]
m = line[1]
ans = 0
for i in range(n // 4, m - 1, -1):
	ans += qpow(-1, i - m) * get(i, m) * work(i) % M
	ans %= M
print(ans)
原文