結果

問題 No.2291 Union Find Estimate
ユーザー AkariAkari
提出日時 2023-05-05 22:42:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 2,120 bytes
コンパイル時間 578 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 78,976 KB
最終ジャッジ日時 2024-05-02 17:40:37
合計ジャッジ時間 3,000 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,480 KB
testcase_01 AC 36 ms
52,096 KB
testcase_02 AC 110 ms
76,928 KB
testcase_03 AC 84 ms
78,976 KB
testcase_04 AC 92 ms
76,160 KB
testcase_05 AC 92 ms
76,160 KB
testcase_06 AC 74 ms
75,392 KB
testcase_07 AC 73 ms
75,392 KB
testcase_08 AC 62 ms
67,200 KB
testcase_09 AC 67 ms
69,632 KB
testcase_10 AC 73 ms
76,032 KB
testcase_11 AC 81 ms
76,416 KB
testcase_12 AC 81 ms
76,160 KB
testcase_13 AC 56 ms
66,560 KB
testcase_14 AC 95 ms
76,288 KB
testcase_15 AC 100 ms
77,568 KB
testcase_16 AC 92 ms
76,032 KB
testcase_17 AC 77 ms
72,576 KB
testcase_18 AC 78 ms
73,088 KB
testcase_19 AC 77 ms
76,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
# import numpy as np

mod = 998244353


def inv_mod(a):
    a %= mod
    if a == 0: return 0
    s, t = mod, a
    m0, m1 = 0, 1
    while t:
        u = s // t
        s -= t * u
        m0 -= m1 * u
        s, t = t, s
        m0, m1 = m1, m0
    if m0 < 0: m0 += mod // s
    return m0


def main():
    input = sys.stdin.readline
    n, qn = map(int, input().split())
    uf = [-1] * n
    fixed = [-1] * n
    i10 = inv_mod(10)
    ans = pow(10, n, mod)

    def red10():
        nonlocal ans
        ans *= i10
        ans %= mod

    def find(x):
        nonlocal uf
        y = x
        while uf[x] >= 0: x = uf[x]
        while uf[y] >= 0:
            z = uf[y]
            uf[y] = x
            y = z
        return x

    def union(x, y):
        nonlocal uf, ans, fixed
        x, y = find(x), find(y)
        if x == y: return False
        if uf[x] > uf[y]: x, y = y, x
        uf[x] += uf[y]
        uf[y] = x
        if fixed[x] == -1:
            if fixed[y] == -1:
                red10()
            else:
                fixed[x] = fixed[y]
                red10()
        else:
            if fixed[y] == -1:
                red10()
            else:
                if fixed[x] != fixed[y]:
                    ans = 0
        return True

    zero, nine = ord('0'), ord('9')
    a = ord('a')
    q = ord('?')
    for _ in range(qn):
        S = map(ord, input().rstrip())
        d = [-1] * 26
        for i, s in enumerate(S):
            if s == q:
                continue
            i = find(i)
            if zero <= s <= nine:
                s -= zero
                if fixed[i] == -1:
                    fixed[i] = s
                    red10()
                else:
                    if fixed[i] != s:
                        ans = 0
            else:
                s -= a
                if d[s] != -1:
                    j = d[s]
                    union(i, j)
                else:
                    d[s] = i
            # if _ == 3:
            #     print(ans, end = ' ')
        print(ans)
                            



if __name__ == '__main__':
    main()
0