結果

問題 No.3 ビットすごろく
コンテスト
ユーザー Araki Tomohiro
提出日時 2017-05-25 11:02:44
言語 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
コード長 813 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 986 ms
コンパイル使用メモリ 179,212 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-03-22 05:22:41
合計ジャッジ時間 2,021 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

static const int INF = 1e8;
bool flag[10001];
int cnt[10001];

int bitcnt(int num) {
  int ans=0;
  while(num>0) {
    if(num%2==1) ans++;
    num /= 2;
  }
  return ans;
}

int main() {
  for (int i=0; i<10001; i++) {
    flag[i] = false;
    cnt[i] = INF;
  }

  cnt[1]=1;

  int n;
  cin >> n;

  queue<int> q;
  q.push(1);

  while(!q.empty()) {
    int now=q.front();
    q.pop();
    if (flag[now]) continue;
    flag[now] = true;
    int d=bitcnt(now);

    if(1<=now-d&&now-d<=n&&flag[now-d]==false) {
      cnt[now-d] = min(cnt[now]+1,cnt[now-d]);
      q.push(now-d);
    }
    if(1<=now+d&&now+d<=n&&flag[now+d]==false) {
      cnt[now+d] = min(cnt[now]+1,cnt[now+d]);
      q.push(now+d);
    }
  }

  printf("%d\n", (flag[n])?cnt[n]:-1);

  return 0;
}
0