結果

問題 No.3 ビットすごろく
ユーザー Forested
提出日時 2020-05-25 15:21:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 859 bytes
コンパイル時間 883 ms
コンパイル使用メモリ 99,012 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 09:49:19
合計ジャッジ時間 1,830 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;

int GO(int x) {
    int res = 0;
    while (x) {
        res += x % 2;
        x /= 2;
    }
    return res;
}

int main() {
    int N;
    cin >> N;
    vector<int> ans(N + 1, -1);
    ans[1] = 1;
    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int n = q.front();
        q.pop();
        if (ans[n + GO(n)] == -1) {
            ans[n + GO(n)] = ans[n] + 1;
            q.push(n + GO(n));
        }
        if (ans[n - GO(n)] == -1) {
            ans[n - GO(n)] = ans[n] + 1;
            q.push(n - GO(n));
        }
    }
    cout << ans[N] << endl;
}
0