跳到正文

题解

A 100 [qaswed12 OI R1] Bot Problem

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

文章信息

发布
2026年10月6日
收录
2026年10月6日
更新
2026年11月16日
篇幅
约 3 分钟

版本

  1. v1当前

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

ab+cd=lcm(a,c)+lcm(b,d)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 aa.

思路

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

验证等式:

  • 左边 ab+cd=a⋅1+c⋅a=a(1+c)ab+cd = a\cdot 1 + c\cdot a = a(1+c)
  • 右边 lcm⁡(a,c)+lcm⁡(b,d)=acgcd⁡(a,c)+lcm⁡(1,a)=ac+a=a(c+1)\operatorname{lcm}(a,c)+\operatorname{lcm}(b,d) = \dfrac{ac}{\gcd(a,c)} + \operatorname{lcm}(1,a) = ac + a = a(c+1)

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

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

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

C++ 代码

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\gcd(a,c)=1 时 lcm⁡(a,c)=ac\operatorname{lcm}(a,c)=ac,代入原式左右均为 a(c+1)a(c+1),恒成立;且 b=1<cb=1<c、d=a≤109d=a\le 10^9、cc 很小,满足全部约束。

原文