結果

問題 No.3 ビットすごろく
コンテスト
ユーザー yuppe19 😺
提出日時 2015-08-21 20:03:57
言語 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
コード長 895 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 516 ms
コンパイル使用メモリ 93,112 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2026-03-22 03:37:44
合計ジャッジ時間 1,528 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}};

int main(void) {
  int n; scanf("%d", &n);
  queue<pair<int, int>> que;
  // 点番号、手数
  que.push(make_pair(1, 1));
  vector<int> memo(n+1, -1);
  int res = -1;
  while(!que.empty()) {
    pair<int, int> cur = que.front(); que.pop();
    int v = cur.first,
        cnt = cur.second;
    if(v == n) { res = cnt; break; }
    if(memo[v] != -1) { continue; }
    memo[v] = cnt;
    int x = __builtin_popcount(v);
    if(v+x <= n) { que.push(make_pair(v+x, cnt+1)); }
    if(v-x >= 1) { que.push(make_pair(v-x, cnt+1)); }
  }
  printf("%d\n", res);
  return 0;
}
0