結果

問題 No.3 ビットすごろく
ユーザー 古寺いろは
提出日時 2015-04-16 01:47:15
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 615 bytes
コンパイル時間 1,554 ms
コンパイル使用メモリ 163,048 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 07:19:01
合計ジャッジ時間 2,522 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

#define MAX 9999999

int bitcount(int a){
	int ret = 0;
	while (a > 0){
		ret++;
		a &= a - 1;
	}
	return ret;
}

int main() {
	int N;
	cin >> N;
	vector<int> dp(N + 1, MAX);
	dp[1] = 1;
	queue<int> q;
	q.push(1);
	while (!q.empty()){
		int now = q.front(); q.pop();
		int move = bitcount(now);
		for (int i = -move; i <= move; i += move * 2)
		{
			int next = now + i;
			if (next < 1 || next > N) continue;
			if (dp[next] != MAX) continue;
			dp[next] = dp[now] + 1;
			q.push(next);
		}
	}

	if (dp[N] == MAX) cout << -1 << endl;
	else cout << dp[N] << endl;
}



0