結果

問題 No.3 ビットすごろく
ユーザー kenta255
提出日時 2019-11-19 17:18:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 948 bytes
コンパイル時間 1,230 ms
コンパイル使用メモリ 110,340 KB
最終ジャッジ日時 2025-01-08 04:08:34
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <stack>
#include <vector>
#include <random>
#include <queue>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(int I = int(A); I < int(B); ++I)


int main(){
    int N;
    cin >> N;
    queue<int> q,d;
    q.push(1);
    d.push(1);
    vector<bool> did(N+1,false);
    did[1]=true;
    while(q.size()){
        int x = q.front();
        int y = d.front();
        q.pop();d.pop();
        if(x==N){
            cout << y << endl;
            return 0;
        }
        int cnt=0,z=x;
        while(z){
            cnt+=z&1;
            z/=2;
        }
        if(x-cnt>0 && !did[x-cnt]){
            q.push(x-cnt);
            d.push(y+1);
            did[x-cnt] = true;
        }
        if(x+cnt<=N && !did[x+cnt]){
            q.push(x+cnt);
            d.push(y+1);
            did[x+cnt] = true;
        }
    }
    cout << -1 << endl;
}
0