結果

問題 No.1481 Rotation ABC
ユーザー 👑 tamatotamato
提出日時 2021-04-16 22:05:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 1,541 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 79,580 KB
最終ジャッジ日時 2023-09-16 00:47:02
合計ジャッジ時間 5,086 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
78,564 KB
testcase_01 AC 79 ms
78,548 KB
testcase_02 AC 81 ms
78,708 KB
testcase_03 AC 79 ms
78,500 KB
testcase_04 AC 79 ms
78,716 KB
testcase_05 AC 80 ms
78,524 KB
testcase_06 AC 80 ms
78,816 KB
testcase_07 AC 79 ms
78,564 KB
testcase_08 AC 81 ms
78,628 KB
testcase_09 AC 81 ms
78,556 KB
testcase_10 AC 79 ms
78,560 KB
testcase_11 AC 79 ms
78,480 KB
testcase_12 AC 80 ms
78,452 KB
testcase_13 AC 78 ms
78,712 KB
testcase_14 AC 79 ms
78,424 KB
testcase_15 AC 79 ms
78,428 KB
testcase_16 AC 79 ms
78,812 KB
testcase_17 AC 78 ms
78,524 KB
testcase_18 AC 79 ms
78,560 KB
testcase_19 AC 77 ms
78,568 KB
testcase_20 AC 79 ms
78,704 KB
testcase_21 AC 79 ms
78,560 KB
testcase_22 AC 79 ms
78,652 KB
testcase_23 AC 79 ms
78,632 KB
testcase_24 AC 79 ms
78,656 KB
testcase_25 AC 79 ms
78,452 KB
testcase_26 AC 78 ms
78,444 KB
testcase_27 AC 79 ms
78,520 KB
testcase_28 AC 84 ms
78,636 KB
testcase_29 AC 84 ms
78,700 KB
testcase_30 AC 85 ms
78,560 KB
testcase_31 AC 90 ms
79,332 KB
testcase_32 AC 92 ms
79,580 KB
testcase_33 AC 89 ms
79,204 KB
testcase_34 AC 91 ms
79,276 KB
testcase_35 AC 89 ms
79,156 KB
testcase_36 AC 89 ms
79,392 KB
testcase_37 AC 88 ms
79,248 KB
testcase_38 AC 89 ms
79,196 KB
testcase_39 AC 89 ms
79,376 KB
testcase_40 AC 94 ms
79,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    # comb init
    nmax = 10 ** 5 + 10  # change here
    fac = [0] * nmax
    finv = [0] * nmax
    inv = [0] * nmax
    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1
    for i in range(2, nmax):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod

    def comb(n, r):
        if n < r:
            return 0
        else:
            return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod

    N = int(input())
    S = input().rstrip('\n')

    ok = 0
    # ABC
    flg = 0
    for s in S:
        if flg == 0 and s == "A":
            flg = 1
        elif flg == 1 and s == "B":
            flg = 2
        elif flg == 2 and s == "C":
            ok = 1
            break

    # BCA
    flg = 0
    for s in S:
        if flg == 0 and s == "B":
            flg = 1
        elif flg == 1 and s == "C":
            flg = 2
        elif flg == 2 and s == "A":
            ok = 1
            break

    # CAB
    flg = 0
    for s in S:
        if flg == 0 and s == "C":
            flg = 1
        elif flg == 1 and s == "A":
            flg = 2
        elif flg == 2 and s == "B":
            ok = 1
            break

    if not ok:
        print(1)
        exit()

    a = S.count("A")
    b = S.count("B")
    c = N - a - b
    ans = (comb(N, a) * comb(N-a, b) - a - b - c)%mod
    print(ans)


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