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

· · 题解

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

首先说一句题外话,~这道题当时我想了将近两个半小时才打出来呢~。

题目大意

## 思路分析 首先发现, $a$ , $b$ 需要互质,所以我们让 $b = 1$(简化一波)。 然后发现,$a + b - \gcd(a,b) = \operatorname{lcm}(c,d) - c - d$。 所以,$\operatorname{lcm}(c,d)-c-d=a$。 那么很简单啊,$c = 2 \times a,d=3 \times a$,能够达到要求。 于是开开心心的打完代码(~~期望得分:24pts~~) ```cpp #include<bits/stdc++.h> using namespace std; #define int long long signed main() { ios::sync_with_stdio(0); cin.tie(), cout.tie(); int t; cin >> t; while (t--) { int a; cin >> a; cout << 1 << " " << 2 * a << " " << 3 * a << "\n"; } } ``` ~~完 damn 了,MatrixGroup 无耻的卡了大小~~ 那么再次思考,发现奇数可以的 $c = 2,d = a + 2$(由于不是最后公式,所以请读者自证此公式)。 好,$53pts$ 到手了。 ~~完 damn 了,公式又写不出来了。~~ 那么在小 X 的疯狂计算(~~红温了~~)下,终于: $$c = 2(a \And -a),d=a+(2(a\And-a)),b=1

完成啦!(我是不会告诉你我是打完 10 以内的数字的表才发现的) 。

那么,这个公式是怎么推导的拟?

首先,把目光放回 \operatorname{lcm}(c,d)-c-d=a 上。

由于 \operatorname{lcm}(c,d) = x \times y \times \gcd(c,d)。那么我们就把 \gcd(c,d) 当成 z。

发现,为了让 gcd(c,d) = z,则我们让 c = 2z(再次简化一波),d 就需要是 zq(q 为奇数),保证了 z 和 q 互质。

那怎么才能让 2qz = 2z + qz + a 呢?

那么 2q - q - 2 = a \div z , 就是 q - 2 = a \div z。

啊!那就是 (a + 2z) \div z = q 。

那就是 d = a + 2z。

(好开心啊)。

那 z 是什么呢?为了让 a \div z = q - 2(也就是一个奇数),那就让他等于 \operatorname{lowbit}(a),也就是 a \And -a。

(好开心啊)。

那么,这个公式对不对呢?

让我们来证明一下:

假设它是对的,则:

\operatorname{lcm}(a+(2(a\And-a)),2(a\And-a)) - (a+4(a\And-a)) = a\\ \therefore (a+(2(a\And-a)) \times 2(a\And-a)) \div \gcd(a+(2(a\And-a)),2(a\And-a)) - (a+4(a\And -a))=a\\ \therefore (a+(2(a\And-a))\times 2(a\And-a)) \div (a \And -a) - (a+4(a\And -a)) = a\\ \therefore (a+(2(a\And-a))\times 2(a\And-a)) \div (a \And -a) = 2a+4(a \And -a)\\ \therefore 2(a\And -a) \cdot a + 4(a\And -a)^2\div (a \And -a) = 2a+4(a\And-a)\\ \therefore (2a + 4(a\And -a)) \cdot (a \And-a) \div (a \And -a) = 2a+4(a\And-a)\\ \therefore 2a + 4(a\And -a) = 2a+4(a\And-a)\\

证毕!

那么,取值呢?

取值的最大值 d 的最大值为 2^{29} \times 3 = 1610612736,刚好不超。

代码

#include<bits/stdc++.h>

using namespace std;
#define int long long


signed main() {
    ios::sync_with_stdio(0);
    cin.tie(), cout.tie();
    int t;
    cin >> t;
    while (t--) {
        int a;
        cin >> a;
        cout << 1 << " " << (a & -a) * 2 << " " << a + (a & -a) * 2 << "\n";
    }
}

再次证明

注意到:

\operatorname{lcm}(a+(2(a\And-a)),2(a\And-a)) - (a+4(a\And-a)) = a

证毕!

华丽收尾。