题解:B4394 [常州市赛 2025] 公约数与公倍数
Farmer_Nhoj · · 题解
一道数学题。
题目传送门。
Part 1 暴力
考虑枚举。
不妨假设
代码如下:
#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 正解
我们可以对所给式子进行移项。
而由于
因为
所以可以得出构造方案。
因为模
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 记录。