結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2024-10-30 19:26:55 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 759 bytes | 
| コンパイル時間 | 1,933 ms | 
| コンパイル使用メモリ | 198,304 KB | 
| 最終ジャッジ日時 | 2025-02-25 01:52:52 | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i)
#define all(a) (a).begin(), (a).end()
int main() {
    cin.tie(nullptr);
    
    int N;
    cin >> N;
    
    vector<int> dist(N, -1);
    queue<int> que;
    
    dist[0] = 1;
    que.push(0);
    
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        
        int d = __builtin_popcount(v + 1);
        
        if (v + d < N && dist[v + d] == -1) {
            dist[v + d] = dist[v] + 1;
            que.push(v + d);
        }
        if (v - d >= 0 && dist[v - d] == -1) {
            dist[v - d] = dist[v] + 1;
            que.push(v - d);
        }
    }
    
    cout << dist[N - 1] << '\n';
}
            
            
            
        