結果

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>

using namespace std;

int co(int n){
	int cnt=0;
	while(1){
		if(n==0)break;
		if(n%2==1)cnt++;
		n/=2;
		
	}
	return cnt;
}


int main(){
	int N;
	const int inf=1e9;
	cin>>N;
	queue<int> que;
	vector<int> d(N + 1, inf);
	que.push(1);
	d[1]=1;
	while(!que.empty()){
		int now = que.front();que.pop();
		if(now==N)break;

		int w=co(now);
		for(int nxt=now-w;nxt<=now+w;nxt+=2*w){
			if(nxt<1||nxt>N)continue;
			if(d[nxt]==inf){
				que.push(nxt);
				d[nxt]=d[now]+1;
			}
		}
	}
	if(d[N]==inf)cout<<"-1"<<endl;
	else cout<<d[N]<<endl;
	return 0;
}
0