結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー Naoto FujiwaraNaoto Fujiwara
提出日時 2024-04-20 11:32:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,301 bytes
コンパイル時間 999 ms
コンパイル使用メモリ 106,028 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-04-20 11:32:19
合計ジャッジ時間 1,867 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<cassert>
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll INF=1LL<<60;


const int mod = 1e9+7;
struct mint {
  long long x; // typedef long long long long;
  mint(long long x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}//後ろのconstはメンバxを変更しないことを示す
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  bool operator==(const mint a) const {return a.x==x;}
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};

std::istream& operator>>(std::istream& is, mint& a) { return is >> a.x;}
std::ostream& operator<<(std::ostream& os, const mint& a) { return os << a.x;}



struct combination {
  std::vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

enum{
    A,
    B,
    C
};


mint dp[100005][3];

int main(){
    int N;
    cin>>N;
    string S;
    cin>>S;

    vector<mint> pow2(N+2,1);
    for(int i=1;i<=N;i++){
        pow2[i]=pow2[i-1]*2;
    }

    for(int i=1;i<=N;i++){
        if(S[i-1]=='B'){

            //i番目の円盤がBにある時 1~iを杭Aへ移動
            dp[i][A]=(i>=2?dp[i-1][C]+pow2[i-1]:1);

            //i番目の円盤がBにある時 1~iを杭Bへ移動
            dp[i][B]=dp[i-1][B];


            //i番目の円盤がBにある時 1~iを杭Cへ移動
            dp[i][C]=(i>=2?(dp[i-1][A]+pow2[i-1]):1); 
        }
        else if(S[i-1]=='A'){
            
            //i番目の円盤がAにある時 1~iを杭Aへ移動
            dp[i][A]=dp[i-1][A];

            //i番目の円盤がAにある時 1~iを杭Bへ移動
            dp[i][B]=(i>=2?(dp[i-1][C]+pow2[i-1]):1);

            //i番目の円盤がAにある時 1~iを杭Cへ移動
            dp[i][C]=(i>=2?dp[i-1][B]+pow2[i-1]:1);
        
        }else if(S[i-1]=='C'){

            //i番目の円盤がCにある時 1~iを杭Aへ移動
            dp[i][A]=(i>=2?(dp[i-1][B]+pow2[i-1]):1);

            //i番目の円盤がCにある時 1~iを杭Bへ移動
            dp[i][B]=(i>=2?(dp[i-1][A]+pow2[i-1]):1);

            //i番目の円盤がCにある時 1~iを杭Cへ移動
            dp[i][C]=dp[i-1][C];
        }

    };

    mint ans=0;
    cout<<dp[N][A]<<endl;


    
}
0