hdu5974 A Simple Math Problem(数学)
题目链接
大意:给你两个数 X,Y,让你找两个数 a,b,满足 a+b=X,lcm(a,b)=Y.
思路:枚举 gcd(a,b),假设 gcd(a,b)=k,那么a=xa∗k,b=xb∗k,化简上面给的两个式子即可得到
xa+xb=kX,xa∗xb=kY,显然这是一个方程组,将 xb=kX−xa代入第二个式子即可得到一个一元二次方程,解这个方程即可。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
const int N = 2e5 +11;
int a,b;
int main(){
ios::sync_with_stdio(false);
while(cin>>a>>b){
int q=sqrt(a);
int sta=0,anL,anR;
for(int i=1;i<=q;i++){
if(a%i)continue;
int A=a/i;
int B=b/i;
if(A*A<4*B)continue;
int T=floor(sqrt(A*A-4*B));
if(T*T!=A*A-4*B)continue;
else {
if((A-T)%2||(A+T)%2)continue;
int L=(A-T)/2*i;
int R=(A+T)/2*i;
if(lcm(L,R)!=b)continue;
anL=(A-T)/2*i;
anR=(A+T)/2*i;
sta=1;
break;
}
}
for(int i=1;i<=q&&!sta;i++){
if(a%i)continue;
int A=a/(a/i);
int B=b/(a/i);
if(A*A<4*B)continue;
int T=floor(sqrt(A*A-4*B));
if(T*T!=A*A-4*B){continue;}
else {
if((A-T)%2||(A+T)%2){continue;}
int L=(A-T)/2*(a/i);
int R=(A+T)/2*(a/i);
if(lcm(L,R)!=b){continue;}
anL=(A-T)/2*(a/i);
anR=(A+T)/2*(a/i);
sta=1;
break;
}
}
if(!sta)cout<<"No Solution\n";
else cout<<anL<<' '<<anR<<endl;
}
return 0;
}