跳到正文

题解

题解:P11036 【MX-X3-T3】「RiOI-4」GCD 与 LCM 问题

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

文章信息

收录
2025年12月12日
更新
2026年5月14日
篇幅
约 3 分钟
题目
P11036

版本

  1. v1当前

做这道题需要用到的:The On-Line Encyclopedia of Integer Sequences, OEIS。


首先 77 分暴力是极易的。

C++
cin >> a;

bool flg = false;
for (int b = 1; b <= 100 && !flg; ++ b )
	for (int c = 1; c <= 100 && !flg; ++ c )
		for (int d = 1; d <= 100 && !flg; ++ d ) {
			if (a + b + c + d == __gcd(a, b) + c * d / __gcd(c, d)) {
				flg = true;
				cout << /*a << ' ' <<*/ b << ' ' << c << ' ' << d << '\n';
				break;
			}
		}

你打一下表,输出所有 a∈[1,50]a \in [1, 50] 的答案。(为了美观放云剪贴板里了,你可以分屏看。)

然后就是 OEIS 了。我们约定 lowbit⁡x=\operatorname{lowbit}x = x & -x。

第二列 b=1b = 1。这显然。

第三列 cc 是 A171977。OEIS 告诉我们:

c(a)=2k+1c(a) = 2^{k+1} where 2k2^k is the highest power of 22 dividing aa.

即 c(a)=2k+1c(a) = 2^{k + 1},其中 2k∣a2^k \mid a 且 kk 最大。

你发现 2k2^k 实际上就是 lowbit⁡a\operatorname{lowbit} a。所以 c(a)=lowbit⁡a×2c(a) = \operatorname{lowbit} a\times 2。

第四列 dd 是 A349102。OEIS 告诉我们:

Increase the odd part of aa to the next greater odd number to get d(a)d(a).

即设 a=b×2ka = b \times 2^k,其中 bb 是奇数(或者说 kk 最大)。将 bb 增加到较大的下一个奇数 b′b'。那么 d(a)=b′×2kd(a) = b' \times 2^k。

显然 b′=b+2b' = b + 2,即 d(a)=(b+2)×2kd(a) = (b + 2) \times 2^k。其中 2k2^k 还是 lowbit⁡a\operatorname{lowbit} a。即 d(a)=(alowbit⁡a+2)×lowbit⁡ad(a) = \left(\dfrac{a}{\operatorname{lowbit} a} + 2\right) \times \operatorname{lowbit} a。

所以:

{b=1c=lowbit⁡a×2d=(alowbit⁡a+2)×lowbit⁡a\left\{ \begin{matrix} b = 1 \\ c = \operatorname{lowbit} a\times 2 \\ d = \left(\dfrac{a}{\operatorname{lowbit} a} + 2\right) \times \operatorname{lowbit} a \end{matrix} \right.

单词询问 O(1)\mathcal O(1)。

C++
#include <bits/stdc++.h>

using namespace std;

signed main() {
	int T;
	cin >> T;
	while (T -- ) {
		int a;
		cin >> a;
		cout << 1 << ' ' << (a & -a) * 2 << ' ' <<  (a & -a) * (a / (a & -a) + 2) << '\n';
	}
	return 0;
}
原文