結果

問題 No.3 ビットすごろく
ユーザー saitaman
提出日時 2018-07-15 22:37:50
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,064 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 09:04:32
合計ジャッジ時間 1,710 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_map>
#include <utility>
using namespace std;

int bitcount(int n){
    int count = 0;
    
    while(n != 0){
        count += n%2;
        n /= 2;
    }
    
    return count;
}

int main(){
    int n;
    cin >> n;
    int now;
    int ans = 0;
    
    vector<int> flag(n+1, 114514);
    
    queue<int>  que;
    
    flag[1] = 1;
    que.push(1);
    
    while(!que.empty()){
        now = que.front();
        que.pop();
        if(now-bitcount(now) > 0 && flag[now-bitcount(now)] == 114514){
            flag[now-bitcount(now)] = flag[now] + 1;
            que.emplace(now-bitcount(now));
        }
        if(now+bitcount(now) <= n && flag[now+bitcount(now)] == 114514){
            flag[now+bitcount(now)] = flag[now] + 1;
            que.emplace(now+bitcount(now));
        }
    }
    
    if(flag[n] < 114514){
        ans = flag[n];
    }else{
        ans = -1;
    }
    
    cout << ans << endl;
}
0