結果

問題 No.2766 Delicious Multiply Spice
ユーザー elphe
提出日時 2025-05-17 12:15:42
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,136 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 118,284 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-17 12:15:45
合計ジャッジ時間 2,806 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 8
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdint>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

static inline string solve(const uint64_t N) noexcept
{
	vector<uint64_t> stack;
	unordered_map<uint64_t, char> cake_status;
	if (N % 2 == 1)
		cake_status.insert({ (N - 1) / 2, 'A' }), stack.push_back((N - 1) / 2);
	if (N % 3 == 1)
		cake_status.insert({ (N - 1) / 3, 'B' }), stack.push_back((N - 1) / 3);

	while (!stack.empty())
	{
		const auto cur = stack.back();
		stack.pop_back();
		if (cur % 2 == 1 && !cake_status.contains((cur - 1) / 2))
			cake_status[(cur - 1) / 2] = 'A', stack.push_back((cur - 1) / 2);
		if (cur % 3 == 1 && !cake_status.contains((cur - 1) / 3))
			cake_status[(cur - 1) / 3] = 'B', stack.push_back((cur - 1) / 3);
	}

	string ans;
	uint64_t cur = 1;
	while (cur != N)
	{
		ans.push_back(cake_status[cur]);
		switch (cake_status[cur])
		{
		case 'A':
			cur = cur * 2 + 1;
			break;

		case 'B':
			cur = cur * 3 + 1;
			break;
		}
	}

	return ans;
}

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

	uint64_t N;
	cin >> N;

	cout << solve(N) << '\n';
	return 0;
}
0