結果

問題 No.3 ビットすごろく
ユーザー byami
提出日時 2018-01-17 15:10:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 839 bytes
コンパイル時間 1,600 ms
コンパイル使用メモリ 171,216 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 08:53:23
合計ジャッジ時間 2,550 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,n) for (int i = a; i < n; i++)
#define per(i,n,a) for (int i = n - 1; i >= a; i--)
using namespace std;

#define MAX 9999999

int bitcount(int a){
    int ret = 0;
    while (a > 0){
        ret++;
        a &= a - 1;
    }
    return ret;
}

int main() {
    int N;
    cin >> N;
    vector<int> dp(N + 1, MAX);
    dp[1] = 1;
    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        int move = bitcount(now);
        for (int i = -move; i <= move; i += move * 2) {
            int next = now + i;
            if (next < 1 || next > N) continue;
            if (dp[next] != MAX) continue;
            dp[next] = dp[now] + 1;
            q.push(next);
        }
    }

    if (dp[N] == MAX) cout << -1 << endl;
    else cout << dp[N] << endl;
}
0