結果

問題 No.3 ビットすごろく
コンテスト
ユーザー DialBird
提出日時 2016-12-09 18:32:59
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 864 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 571 ms
コンパイル使用メモリ 87,844 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-22 04:45:13
合計ジャッジ時間 1,486 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <array>
#include <queue>

using namespace std;

#define REP(i,first,last) for (int i=first;i<last;i++)

const int MAX_N = 10000;
vector<int> dp(MAX_N + 1, 0);
int N;

int bit_num(int n){
  int count = 0;
  while (n > 0) {
    n &= n-1;
    count++; 
  }
  return count;
}

int solve(int N){
  queue<int> que;
  que.push(1);
  while (!que.empty()) {
    int q = que.front();
    int move_count = bit_num(q);
    array<int, 2> list{move_count, -move_count};
    for (auto li: list) {
      if ((q+li) == N) {
        return dp[q] + 1;
      } else if (0 < (q + li) && (q + li) < N && dp[q+li] == 0) {
        dp[q+li] = dp[q] + 1;
        que.push(q+li);
      }
    }
    que.pop();
  }
  return -1;
}

int main(){
  dp[1] = 1;
  cin >> N;
  if (N == 1) {
    cout<<1<<endl; 
  } else {
    cout<<solve(N)<<endl;
  }
}
0