結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー tnakao0123tnakao0123
提出日時 2022-12-04 13:21:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 42,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-01 21:16:43
合計ジャッジ時間 1,359 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2147.cc:  No.2147 ハノイの塔のおもちゃ - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 1000;
const int MOD = 1000000007;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

/* global variables */

char s[MAX_N + 4];
int ps[MAX_N];
mi dp[MAX_N + 1];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d%s", &n, s);

  for (int i = 0; i < n; i++) ps[i] = s[i] - 'A';

  dp[0] = 0;
  for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] * 2 + 1;

  mi sum = 0;
  int pp = 0;

  for (int i = n; i >= 1; i--)
    if (ps[i - 1] != pp) {
      sum += dp[i - 1] + 1;
      pp = 3 - (pp + ps[i - 1]);
    }

  printf("%d\n", sum.v);
  return 0;
}
0