結果

問題 No.3 ビットすごろく
ユーザー atn112323
提出日時 2016-05-03 00:01:45
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 542 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 64,864 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-05 02:37:05
合計ジャッジ時間 1,847 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>

using namespace std;

const int INF = 1000000;

int N;
int d[10001];

int main() {
	cin >> N;
	for (int i = 0; i <= 10000; i++) {
		d[i] = INF;
	}
	queue<int> que;
	d[1] = 1;
	que.push(1);
	while (!que.empty()) {
		int n = que.front();
		que.pop();
		int bn = __builtin_popcount(n);
		if (n - bn >= 1 && d[n - bn] == INF) {
			d[n - bn] = d[n] + 1;
			que.push(n - bn);
		}
		if (n + bn <= N) {
			d[n + bn] = d[n] + 1;
			que.push(n + bn);
		}
	}
	cout << (d[N] != INF ? d[N] : -1) << endl;
	return 0;
}
0