結果
問題 |
No.2147 ハノイの塔のおもちゃ
|
ユーザー |
|
提出日時 | 2022-12-04 00:35:12 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 906 bytes |
コンパイル時間 | 872 ms |
コンパイル使用メモリ | 79,684 KB |
最終ジャッジ日時 | 2025-02-09 04:50:24 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 8 |
ソースコード
#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'; }