Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least Common Multiple (X, Y) =b
x+y=a
lcm(x,y)=b
令
x=nd
y=md
有
gcd(n,m)=1
(n+m)d=a
n*m*d=b
=>
n+m=a/d
n*m=b/d
枚举a的因子d,然后就是韦达定理
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
const int N = 20000 ;
std::vector<int> fac[N+8] ;
void init(){
for(int i = 1 ; i <= N ; i++){
for(int j = i ; j <= N ; j += i) fac[j].push_back(i) ;
}
}
void gao(LL a , LL b){
std::vector<std::pair<LL , LL> > res ;
for(std::vector<int>::iterator it = fac[a].begin() ; it != fac[a].end() ; it++){
int d = *it ;
if((b%d) != 0) continue ;
LL A = 1;
LL B = -a/d ;
LL C = b/d ;
LL delt = B*B - A*C*4 ;
LL de = (LL)std::sqrt(delt + 0.5) ;
if(de*de != delt) continue ;
if((-B + de) % (A*2) == 0){
LL m = (-B + de) / (A*2) ;
LL n = a/d - m ;
if(std::__gcd(n,m) == 1 && n*m == b/d){
if(n > m) std::swap(n , m) ;
res.push_back(std::make_pair(n*d , m*d)) ;
}
}
if((-B - de) % (A*2) == 0){
LL m = (-B - de) / (A*2) ;
LL n = a/d - m ;
if(std::__gcd(n,m) == 1 && n*m == b/d){
if(n > m) std::swap(n , m) ;
res.push_back(std::make_pair(n*d , m*d)) ;
}
}
}
if(res.empty()) puts("No Solution") ;
else{
std::sort(res.begin() , res.end()) ;
printf("%I64d %I64d\n" , res[0].first , res[0].second) ;
}
}
int main() {
init() ;
LL a , b ;
while(scanf("%I64d%I64d" , &a , &b) != EOF){
gao(a , b) ;
}
return 0 ;
}