跳到正文

题解

题解:AT_arc122_c [ARC122C] Calculator

Nelofus约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年5月22日
收录
2026年3月2日
更新
2026年11月14日
篇幅
约 2 分钟

版本

  1. v1当前

摆在这个位置肯定是有通用的简易构造方法的。

想一想怎么样才能快速把数值升到差不多和 NN 一个量级,很显然最快的做法就是先令 x=1,y=1x=1,y=1,然后交替做 33 和 44。

在这个过程中 xx 和 yy 的变化是什么?观察得到这个是斐波那契数列。

那事情就简单了,如果 NN 恰好是一个斐波那契数,那么令 xx 或 yy 之中的一个为 11,然后交替这样做就可以凑出 NN 了。

进一步扩展到一般的 NN,它肯定可以被分解成若干个斐波那契数之和。一边交替做,一边加上新的斐波那契数起点即可。

C++
constexpr int M = 88;
i64 fib[M];
i64 n;
std::vector<int> Div;
int tag[M][2];

int main() {
#ifdef HeratinoNelofus
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	std::cin >> n;

	fib[0] = 1, fib[1] = 1;
	for (int i = 2; i < M; i++)
		fib[i] = fib[i - 1] + fib[i - 2];
	for (int i = M - 1; i >= 1; i--) {
		if (n >= fib[i])
			n -= fib[i], Div.push_back(i);
	}

	int lst = (Div[0] - 1) / 2 + 1;
	for (const int &x : Div) {
		int p = (x - 1) / 2 + 1;
		if (x & 1) {
			tag[lst + 1 - p][1] = 1;
		} else {
			tag[lst + 1 - p][0] = 1;
		}
	}

	std::vector<int> ope;
	for (int i = 1; i <= lst; i++) {
		ope.push_back(3);
		if (tag[i][0])
			ope.push_back(1);
		ope.push_back(4);
		if (tag[i][1])
			ope.push_back(2);
	}
	ope.push_back(3);
	std::cout << ope.size() << '\n';
	for (const int &x : ope)
		std::cout << x << '\n';
	return 0;
}
原文