A 100 [qaswed12 OI R1] Bot Problem

· · 题解

I'll analyze this problem carefully. We need to find positive integers b, c, d \le 10^9 with b \ne c such that:

ab + cd = \text{lcm}(a,c) + \text{lcm}(b,d)

Let me first verify the sample and explore patterns by brute-forcing small values of a.

思路

构造一组恒成立的解。取 b=1,\ d=a,并令 c 为 \ge 2 且与 a 互质的最小整数(即 \gcd(a,c)=1)。

验证等式:

两边相等 ✓。同时 b=1\ne c(因 c\ge2),三者均为正整数。

关于范围:b=1\le 10^9,d=a\le 10^9;而 c 是“最小的、不被 a 的质因子整除的数”,它不超过第一个不整除 a 的素数。即使 a 含前若干个素因子,对 a\le 10^9 而言 c 也极小(实测最大仅约 30),远小于 10^9。因此该构造对所有输入都合法。

实现时只需从 c=2 开始递增找第一个 \gcd(a,c)=1 即可,每次询问复杂度极低。

C++ 代码

#include <bits/stdc++.h>
using namespace std;

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

    int T;
    if(!(cin >> T)) return 0;
    while(T--){
        long long a;
        cin >> a;
        // b = 1, d = a, c = smallest >=2 coprime to a
        long long b = 1, d = a;
        long long c = 2;
        while(std::gcd(a, c) != 1) ++c;   // c 非常小,循环次数极少
        cout << b << " " << c << " " << d << "\n";
    }
    return 0;
}

正确性说明:当 \gcd(a,c)=1 时 \operatorname{lcm}(a,c)=ac,代入原式左右均为 a(c+1),恒成立;且 b=1<c、d=a\le 10^9、c 很小,满足全部约束。