結果

問題 No.3 ビットすごろく
ユーザー CsTarepandaCsTarepanda
提出日時 2017-05-03 23:08:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 523 ms / 5,000 ms
コード長 767 bytes
コンパイル時間 821 ms
コンパイル使用メモリ 69,648 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 00:41:10
合計ジャッジ時間 6,217 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 15 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 80 ms
4,376 KB
testcase_10 AC 201 ms
4,376 KB
testcase_11 AC 49 ms
4,376 KB
testcase_12 AC 14 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 173 ms
4,380 KB
testcase_15 AC 443 ms
4,376 KB
testcase_16 AC 273 ms
4,380 KB
testcase_17 AC 412 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 515 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 182 ms
4,376 KB
testcase_23 AC 523 ms
4,380 KB
testcase_24 AC 515 ms
4,380 KB
testcase_25 AC 455 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 252 ms
4,380 KB
testcase_29 AC 52 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 33 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;
int n;
int field[10001];

int cnt1(int n){
  int result = 0;
  while(n){
    if(n & 1) result += 1;
    n >>= 1;
  }
  return result;
}

int solve(int now, int depth){
  if(now < 1 || now > n) return -1;
  if(now == n){
    return depth;
  }
  if(field[now] == -1 || field[now] >= depth) {
    field[now] = depth;
    int s1 = solve(now + cnt1(now), depth + 1);
    int s2 = solve(now - cnt1(now), depth + 1);
    if(s1 == -1) return s2;
    if(s2 == -1) return s1;
    return min(s1, s2);
  }else{
    return -1;
  }
}

int main(){
  cin >> n;
  for(int i = 0; i < n; i++){
    field[i] = -1;
  }
  cout << solve(1, 1) << endl;
}
0