結果

問題 No.3 ビットすごろく
ユーザー cormoran
提出日時 2016-11-14 02:43:51
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 193 ms / 5,000 ms
コード長 676 bytes
コンパイル時間 1,584 ms
コンパイル使用メモリ 173,512 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 08:09:55
合計ジャッジ時間 4,059 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    int N; cin >> N;
    vector<int> visited(N);
    queue<int> que;
    que.push(0);
    int ans = 1;
    for(; que.size(); ans++) {
        queue<int> nxt_que;
        while(que.size()) {
            int a = que.front(); que.pop();
            visited[a] = true;
            if(a == N - 1) goto OK;
            int b = __builtin_popcount(a + 1);
            if(a + b < N and not visited[a + b]) nxt_que.push(a + b);
            if(a - b >= 0 and not visited[a - b]) nxt_que.push(a - b);
        }
        swap(que, nxt_que);
    }
    cout << -1 << endl;
    return 0;
OK:
    cout << ans << endl;
    return 0;
}
0