結果

問題 No.3 ビットすごろく
ユーザー elphe
提出日時 2024-08-09 20:04:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 868 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 78,408 KB
最終ジャッジ日時 2025-02-23 21:10:30
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdint>
#include <vector>
#include <queue>

using namespace std;

constexpr int16_t pop_count(const int16_t a)
{
	return a == INT16_C(0) ? INT16_C(0) : (a & INT16_C(1)) + pop_count(a >> INT16_C(1));
}

int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int16_t N, i;
	cin >> N;
	vector<int16_t> dp(N, INT16_C(-1));
	dp[0] = INT16_C(1);
	queue<int16_t> q;
	q.push(INT16_C(0));

	while (!q.empty())
	{
		const auto temp = pop_count(q.front() + INT16_C(1));
		if (q.front() - temp >= INT16_C(0) && dp[q.front() - temp] == INT16_C(-1))
			dp[q.front() - temp] = dp[q.front()] + INT16_C(1), q.push(q.front() - temp);
		if (q.front() + temp < N && dp[q.front() + temp] == INT16_C(-1))
			dp[q.front() + temp] = dp[q.front()] + INT16_C(1), q.push(q.front() + temp);

		q.pop();
	}

	cout << dp[N - INT16_C(1)] << '\n';
	return 0;
}
0