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

· · 题解

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

Consider the Inclusion-Exclusion Principle.

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

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

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

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

Time complexity \mathcal{O}(n).

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)