結果

問題 No.3 ビットすごろく
ユーザー かぼちゃ
提出日時 2018-04-20 23:21:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 340 ms / 5,000 ms
コード長 981 bytes
コンパイル時間 786 ms
コンパイル使用メモリ 78,736 KB
実行使用メモリ 6,112 KB
最終ジャッジ日時 2024-07-01 08:59:11
合計ジャッジ時間 4,457 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include <queue>

#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define REP(i,a,n) for(int i = (a);i < (n);i++)
#define PI 3.14159265359
#define MOD 1000000007
#define chmax(a,b) if(a < b)a = b

using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> a(n+1,-1);
    queue<pair<int,int>> q;
    q.push({1,1});
    while(!q.empty()){
        int index = q.front().first;
        int count = q.front().second;
        a[index] = count;
        if(index == n)break;
        
        int add = 0,b = index;
        while(b > 0){
            add += b % 2;
            b /= 2;
        }
        
        if(a[index + add] == -1 && index + add <= n) q.push({index + add,count + 1});
        if(a[index - add] == -1 && index - add > 0) q.push({index - add,count + 1});
        q.pop();
    }
    cout << a[n] << endl;
    return 0;
}
0