题解:B4394 [常州市赛 2025] 公约数与公倍数

· · 题解

一道数学题。
题目传送门。

Part 1 暴力

考虑枚举。
不妨假设 \gcd(n,a)=1,然后直接枚举 c,d 即可,由此你可以获得 20 分的好成绩。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
    int n;
    cin>>n;
    for(int j=1;j<=1000;j++){
        for(int k=1;k<=1000;k++){
            if(1+j*k/__gcd(j,k)==n+1+j+k){
                cout<<1<<" "<<j<<" "<<k<<'\n';
                return ;
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--) solve();
    return 0;
}

Part 2 正解

我们可以对所给式子进行移项。

\begin{aligned} a+b+c+d &= \gcd(a,b)+\operatorname{lcm}(c,d) \\ a+b-\gcd(a,b) &= \operatorname{lcm}(c,d)-c-d \end{aligned}

而由于

\begin{aligned} \operatorname{lcm}(3k,4k) &= 12k\\ 3k+4k+5k &= 12k \end{aligned}

因为 5 整除 a+b-\gcd(a,b)。
所以可以得出构造方案。

\begin{aligned} c = \frac{3}{5} \times (a+b-\gcd(a,b))\\ d = \frac{4}{5} \times (a+b-\gcd(a,b)) \end{aligned}

因为模 5 的余数只有五种,根据抽屉原理,只需枚举从 1 开始的 20 个正整数必然可找到满足条件的 b。

Part 3 完整代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
    int a;
    cin>>a;
    for(int b=1;;b++){
        if((a+b-__gcd(a,b))%5==0){
            cout<<b<<" "<<(a+b-__gcd(a,b))*3ll/5<<" "<<(a+b-__gcd(a,b))*4ll/5<<"\n";
            return ;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--) solve();
    return 0;
}

AC 记录。