結果

問題 No.3 ビットすごろく
ユーザー Mister
提出日時 2020-04-10 19:11:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 702 bytes
コンパイル時間 1,004 ms
コンパイル使用メモリ 77,840 KB
最終ジャッジ日時 2025-01-09 15:44:19
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> dist(n, -1);
    dist[0] = 1;
    std::queue<int> que;
    que.push(0);

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        int d = __builtin_popcount(v + 1);

        for (int i = -1; i <= 1; ++i) {
            int nv = v + d * i;
            if (nv < 0 || n <= nv ||
                dist[nv] != -1) continue;

            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }

    std::cout << dist.back() << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0