結果

問題 No.2291 Union Find Estimate
ユーザー AkariAkari
提出日時 2023-05-05 22:42:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 2,120 bytes
コンパイル時間 490 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 79,868 KB
最終ジャッジ日時 2023-08-15 05:33:54
合計ジャッジ時間 3,466 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
71,072 KB
testcase_01 AC 63 ms
71,068 KB
testcase_02 AC 117 ms
78,136 KB
testcase_03 AC 95 ms
79,868 KB
testcase_04 AC 96 ms
77,180 KB
testcase_05 AC 98 ms
77,132 KB
testcase_06 AC 86 ms
76,092 KB
testcase_07 AC 79 ms
76,504 KB
testcase_08 AC 75 ms
76,520 KB
testcase_09 AC 79 ms
76,640 KB
testcase_10 AC 79 ms
77,176 KB
testcase_11 AC 84 ms
77,244 KB
testcase_12 AC 90 ms
77,152 KB
testcase_13 AC 70 ms
76,708 KB
testcase_14 AC 96 ms
77,004 KB
testcase_15 AC 107 ms
78,376 KB
testcase_16 AC 103 ms
76,872 KB
testcase_17 AC 88 ms
77,100 KB
testcase_18 AC 90 ms
77,412 KB
testcase_19 AC 80 ms
77,204 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