結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー suisensuisen
提出日時 2022-12-04 00:35:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 906 bytes
コンパイル時間 836 ms
コンパイル使用メモリ 81,948 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-01 11:13:54
合計ジャッジ時間 1,507 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <deque>
#include <iostream>
#include <vector>

#include <atcoder/modint>

using mint = atcoder::modint1000000007;

mint solve(std::array<std::deque<int>, 3> &target, int pos, int n) {
    if (n == 0) return 0;

    if (target[pos].size() and target[pos].front() == n) {
        target[pos].pop_front();
        return solve(target, pos, n - 1);
    }

    int target_pos = 0;
    for (int i = 0; i < 3; ++i) {
        if (target[i].size() and target[i].front() == n) {
            target_pos = i;
        }
    }
    target[target_pos].pop_front();
    return mint(2).pow(n - 1) + solve(target, 3 ^ pos ^ target_pos, n - 1);
}

int main() {
    int n;
    std::string s;
    std::cin >> n >> s;

    std::array<std::deque<int>, 3> target{};
    for (int i = 0; i < n; ++i) {
        target[s[i] - 'A'].push_front(i + 1);
    }
    std::cout << solve(target, 0, n).val() << '\n';
}
0