結果
| 問題 | 
                            No.2147 ハノイの塔のおもちゃ
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2022-12-18 02:14:12 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 18 ms / 2,000 ms | 
| コード長 | 426 bytes | 
| コンパイル時間 | 1,800 ms | 
| コンパイル使用メモリ | 192,880 KB | 
| 最終ジャッジ日時 | 2025-02-09 15:29:31 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 8 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin>>N;
  string S;
  cin>>S;
  long long ans=0;
  long long beki[N+1];
  beki[0]=1;
  for(int i=1;i<=N;i++)beki[i]=beki[i-1]*2%mod;
  int m=0;
  for(int i=N-1;i>=0;i--){
    if(S[i]-'A'!=m){
      ans=(ans+beki[i])%mod;
      m=3^m^(S[i]-'A');
    }
  }
  cout<<(ans+mod)%mod<<'\n';
}