結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー 👑 chro_96chro_96
提出日時 2022-12-04 00:20:45
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 799 bytes
コンパイル時間 534 ms
コンパイル使用メモリ 30,720 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 10:35:15
合計ジャッジ時間 865 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>

void func (int n, char *s, char t, long long *ans, long long *pow2, long long mod_num) {
  if (n < 0) {
    return;
  }
  
  if (s[n] == t) {
    func(n-1, s, t, ans, pow2, mod_num);
  } else {
    char nxt = 'A';
    while (nxt == s[n] || nxt == t) {
      nxt += (char)1;
    }
    func(n-1, s, nxt, ans, pow2, mod_num);
    *ans += pow2[n];
    *ans %= mod_num;
  }
  
  return;
}

int main () {
  int n = 0;
  char s[1001] = "";
  
  int res = 0;
  
  long long ans = 0LL;
  long long mod_num = 1000000007LL;
  
  long long pow2[1001] = {};
  
  res = scanf("%d", &n);
  res = scanf("%s", s);
  
  pow2[0] = 1LL;
  for (int i = 0; i < n; i++) {
    pow2[i+1] = (2LL*pow2[i])%mod_num;
  }
  
  func(n-1, s, 'A', &ans, pow2, mod_num);
  
  printf("%lld\n", ans);
  return 0;
}
0