結果

問題 No.3 ビットすごろく
ユーザー erieri
提出日時 2015-07-25 22:53:41
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 590 bytes
コンパイル時間 517 ms
コンパイル使用メモリ 64,220 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 07:27:21
合計ジャッジ時間 1,512 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>
using namespace std;
typedef long long LL;
int popcount(int x) {
	int ret = 0;
	while (x) { x &= x - 1; ++ret; }
	return ret;
}
int dp[10001];
int main() {
	int n; cin >> n;
	fill(dp, dp + 10001, -1);
	dp[1] = 1;
	queue<int> q;
	q.push(1);
	while (!q.empty()) {
		int x = q.front(); q.pop();
		int d = popcount(x);
		if (x - d > 0) {
			if (dp[x - d] == -1) {
				dp[x - d] = dp[x] + 1;
				q.push(x - d);
			}
		}
		if (x + d <= n) {
			if (dp[x + d] == -1) {
				dp[x + d] = dp[x] + 1;
				q.push(x + d);
			}
		}
	}
	cout << dp[n] << endl;
	return 0;
}
0