Problem link:- PrepBytes
My Approach :-
-
lcm of a/b and c/d is (product of both)/(gcd of both)
-
To find lcm of two fractions we have to find gcd
of those fractions.
formula to find gcd of (a/b) and (c/d) is :-
gcd(a,c)/lcm(b,d)
-
Suppose we get pro1/pro2 as ( product of both )
and gcd1/gcd2 as ( gcd of both ) -
Lcm of two fraction = (pro1 * gcd2)/(pro2 * gcd1)
let us consider it as lcm1/lcm2
Now to reduce it into simplest form we can devide lcm1 and
lcm2 with the gcd(lcm1,lcm2)
My Code :-
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll findGCD(ll a,ll b)
{
if(a==0)
return b;
return findGCD(b%a,a);
}
ll findLCM(ll a,ll b)
{
ll gcd=findGCD(a,b);
return((a*b)/gcd);
}
int main()
{
//write your code here
int t ; cin>>t;
for(int i=0;i<t;i++)
{
ll a,b,c,d; cin>>a>>b>>c>>d;
ll gcd=findGCD(a,c);
ll lcm=findLCM(b,d);
ll gcd1=gcd;
ll gcd2=lcm;
//cout<<" gcd1/gcd2 "<<gcd1<<"/"<<gcd2<<endl;
ll pro1=a*c;
ll pro2=b*d;
ll final_num=(pro1*gcd2);
ll final_den=(pro2*gcd1);
//cout<<"final_num and final_den "<<final_num<<" "<<final_den<<endl;
ll final_divisor=findGCD(final_num,final_den);
cout<<final_num/final_divisor<<" "
<<final_den/final_divisor<<endl;
}
return 0;
}