結果

問題 No.3 ビットすごろく
ユーザー tonyu0
提出日時 2019-10-14 11:45:13
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 491 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 74,552 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 09:31:59
合計ジャッジ時間 1,650 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
	int N;
	vector<int> dist(11000, -1);
	cin >> N;

	dist[1] = 1;
	dist[2] = 2;
	queue<int> q;
	q.push(2);
	while (q.size()) {
		int n = q.front(); q.pop();
		int b = __builtin_popcount(n);
		if (n > N) continue;
		if (dist[n + b] == -1) {
			dist[n + b] = dist[n] + 1;
			q.push(n + b);
		}
		if (dist[n - b] == -1) {
			dist[n - b] = dist[n] + 1;
			q.push(n - b);
		}
	}
	
	cout << dist[N] << endl;
}
0