結果

問題 No.3 ビットすごろく
コンテスト
ユーザー moti
提出日時 2020-03-08 14:59:22
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 686 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,948 ms
コンパイル使用メモリ 172,168 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-03-22 07:18:56
合計ジャッジ時間 4,752 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

#define chmin(a, x) a=min(a,x)

int main() {
  
  int N; cin >> N;
  
  int const INF = 1<<29;
  bool vis[10001] = {};
  queue<pair<int, int> > q;
  q.push(make_pair(1, 1));
  while(!q.empty()) {
    int now = q.front().first;
    int cost = q.front().second; q.pop();
    
    if(now == N) {
      cout << cost << endl;
      return 0;
    }
    
    int a = now - __builtin_popcount(now);
    if(a > 0 && !vis[a]) {
      vis[a] = 1;
      q.push(make_pair(a, cost+1));
    }
    int b = now + __builtin_popcount(now);
    if(b < N+1 && !vis[b]) {
      q.push(make_pair(b, cost+1));
    }
  }
  
  cout << -1 << endl;
  
  return 0;
}
0