結果
| 問題 |
No.2147 ハノイの塔のおもちゃ
|
| コンテスト | |
| ユーザー |
umezo
|
| 提出日時 | 2022-12-04 00:58:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 948 bytes |
| コンパイル時間 | 2,033 ms |
| コンパイル使用メモリ | 195,832 KB |
| 最終ジャッジ日時 | 2025-02-09 04:51:22 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 8 |
コンパイルメッセージ
main.cpp: In function ‘ll f(std::string, char)’:
main.cpp:31:1: warning: control reaches end of non-void function [-Wreturn-type]
31 | }
| ^
ソースコード
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
#include<bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
ll pow2[1010];
ll f(string s,char c){
int k=s.size();
if(k==1){
if(s[0]==c) return 0;
else return 1;
}
string t=s.substr(0,k-1);
if(s[k-1]==c) return f(t,c);
if(c=='A'){
if(s[k-1]=='B') return (f(t,'C')+1+pow2[k-1])%MOD;
else return (f(t,'B')+1+pow2[k-1])%MOD;
}
else if(c=='B'){
if(s[k-1]=='A') return (f(t,'C')+1+pow2[k-1])%MOD;
else return (f(t,'A')+1+pow2[k-1])%MOD;
}
else if(c=='C'){
if(s[k-1]=='A') return (f(t,'B')+1+pow2[k-1])%MOD;
else return (f(t,'A')+1+pow2[k-1])%MOD;
}
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
pow2[0]=1;
for(int i=1;i<1010;i++) pow2[i]=pow2[i-1]*2%MOD;
rep(i,1010) pow2[i]--;
int n;
string s;
cin>>n>>s;
cout<<f(s,'A')<<endl;
return 0;
}
umezo