結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー H3PO4H3PO4
提出日時 2022-12-22 21:22:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 588 bytes
コンパイル時間 447 ms
コンパイル使用メモリ 10,892 KB
実行使用メモリ 8,388 KB
最終ジャッジ日時 2023-08-11 11:23:34
合計ジャッジ時間 2,075 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,840 KB
testcase_01 AC 15 ms
7,792 KB
testcase_02 AC 15 ms
7,804 KB
testcase_03 AC 15 ms
7,964 KB
testcase_04 AC 14 ms
7,840 KB
testcase_05 AC 15 ms
8,268 KB
testcase_06 AC 16 ms
8,332 KB
testcase_07 AC 16 ms
8,384 KB
testcase_08 AC 15 ms
7,804 KB
testcase_09 AC 15 ms
7,832 KB
testcase_10 AC 17 ms
8,388 KB
testcase_11 AC 17 ms
8,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

N = int(input())
S = input()
MOD = 10 ** 9 + 7

sys.setrecursionlimit(10 ** 5)


def rest(s1: str, s2: str):
    st = set("ABC")
    st.remove(s1)
    st.remove(s2)
    return st.pop()


pow_table = [1] * (N + 1)
for i in range(1, N + 1):
    pow_table[i] = pow_table[i - 1] * 2 % MOD


def f(i: int, s: str):
    if S[i] == s:
        if i:
            return f(i - 1, s)
        else:
            return 0
    else:
        if i:
            res = f(i - 1, rest(S[i], s)) + pow_table[i]
            return res % MOD
        else:
            return 1


print(f(N - 1, "A"))
0