結果

問題 No.3 ビットすごろく
コンテスト
ユーザー himueu
提出日時 2026-10-15 15:53:28
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 5,000 ms
+ 915µs
コード長 711 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,185 ms
コンパイル使用メモリ 193,488 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2026-10-15 15:53:37
合計ジャッジ時間 5,234 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bit>
#include <iostream>
#include <queue>
#include <vector>

int main() {
    using namespace std;
    int n;
    cin >> n;
    
    constexpr int INF = 1e6;
    vector<int> ans(n + 1, INF);
    ans[1] = 1;
    queue<unsigned int> que;
    que.push(1);
    while (!que.empty()) {
        unsigned int v = que.front();
        que.pop();
        unsigned int b = popcount(v);
        if (b < v && ans[v - b] == INF) {
            ans[v - b] = ans[v] + 1;
            que.push(v - b);
        }
        if (v + b <= static_cast<unsigned int>(n) && ans[v + b] == INF) {
            ans[v + b] = ans[v] + 1;
            que.push(v + b);
        }
    }
    cout << (ans[n] == INF ? -1 : ans[n]) << endl;
}
0