結果

問題 No.3 ビットすごろく
ユーザー UrtuseaUrtusea
提出日時 2024-07-12 19:27:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,014 bytes
コンパイル時間 3,328 ms
コンパイル使用メモリ 279,532 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-12 19:28:04
合計ジャッジ時間 4,726 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int  = int64_t;
using uInt = uint64_t;

template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;

void solve( /* Copyright by Urtusea */ )
{
    int n;
    cin >> n;

    vector<int> vis(n + 1);
    
    queue<int> q;
    q.push(vis[1] = 1);

    while (!q.empty()) {
        auto u = q.front(); q.pop();

        int v = __popcount(u);

        if (u + v <= n && !vis[u + v]) {
            vis[u + v] = vis[u] + 1;
            q.push(u + v);
        }

        if (u - v > 0 && !vis[u - v]) {
            vis[u - v] = vis[u] + 1;
            q.push(u - v);
        }
    }

    if (vis[n]) {
        cout << vis[n] << '\n';
    } else {
        cout << -1 << '\n';
    }
}

int main(int argc, char *argv[], char *envp[])
{
    cin.tie(nullptr)->sync_with_stdio(false);

    // for (int i = 1, n = (cin >> n, n); i <= n; i++)
        solve();

    return 0;
}
0