結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー umezoumezo
提出日時 2022-12-04 00:58:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 2,049 ms
コンパイル使用メモリ 198,112 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 11:06:05
合計ジャッジ時間 2,945 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'll f(std::string, char)':
main.cpp:31:1: warning: control reaches end of non-void function [-Wreturn-type]
   31 | }
      | ^

ソースコード

diff #

#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;
}
0