結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-02-10 02:55:08 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 961 bytes | 
| コンパイル時間 | 1,544 ms | 
| コンパイル使用メモリ | 173,572 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-07 11:48:15 | 
| 合計ジャッジ時間 | 2,512 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
#include <iostream>
#include<math.h>
using namespace std;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int N;
vector<int>seen;
int main()
{
    cin >> N;
    seen.resize(N+1);
    seen[1] = 1;
    queue<int>q;
    q.push(1);
    while(!q.empty()) {
        int n =q.front();
        q.pop();
        int num = __builtin_popcount(n);
        int add_num = n + num;
        int sub_num = n - num;
        if (N >= add_num && seen[add_num] == 0) {
            seen[add_num] = seen[n] + 1;
            q.push(add_num);
        }
        if (sub_num > 0 && seen[sub_num] == 0) {
            seen[sub_num] = seen[n] + 1;
            q.push(sub_num);
        }
    }
    if (seen[N] == 0) {
        cout << -1 << endl;
    } else {
        cout << seen[N] << endl;
    }
    return 0;
}
            
            
            
        