GCD Plus LCM


This is a companion discussion topic for the original entry at https://toph.co/p/gcd-plus-lcm
#include<bits/stdc++.h>
using namespace std;

unsigned long long gcd(unsigned long long a, unsigned long long b)
{
    if(a==0) return b;
    return gcd(b%a, a);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    unsigned long long a, b, c, lcm, t;
    cin>>t;
    while(t>0) {
        cin>>a>>b;
        c = gcd(a, b);
        lcm = (a*b)/c;
        if((c+lcm) == (a+b)) cout<<"true\n";
        else cout<<"false\n";
        t--;
    }

    return 0;
}

I am getting WA for that in 2nd test case…
Please help…

The problem might be integer overflow,

assume,

a = 10 ^ 18
b = 10 ^ 18

That means,

a * b = (10 ^ 18) * (10 ^ 18) = 10 ^ (18 + 18) = 10 ^ 36

and 10 ^ 36 is of course out of the limit of unsigned long long.

I think this is the reason of WA.

And, @hjr265, can’t solve this with python3. Can you fix the time limit?

#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,i,a,b;
cin>>n;
for(i=0; i<n; i++){
cin>>a>>b;
if(b%a==0 || a%b==0)
cout<<“true”<<endl;
else
cout<<“false”<<endl;
}
return 0;
I am getting cpu limite ex in 6 test plz help

Try to use scanf and printf instead of using cin and cout.

#include<bits/stdc++.h>
using namespace std;

int gcd(int a, int b){

while(b>0){
	int temp=b;
	b=a%b;
	a=temp;
}
return a;

}

int lcm(int a, int b){

return a*(b/gcd(a,b));

}

int main(){

int t;
	cin>>t;

while(t--){
	
	int a,b;
		cin>>a >>b;
	int temp=gcd(a,b)+lcm(a,b);
	int sum=a+b;
	if( temp == sum)
		cout<< "true" <<endl;
	else
		cout<<"false" <<endl;
}
return 0;

}

Whats the problem in this code???
Please help

Why TLE @hjr265 ??? I haven’t even used a loop!

#include <bits/stdc++.h>

using namespace std;

int main() {
	long long int t;
	cin>>t;
	while(t--){
		long long int a,b;
		cin>>a>>b;
		if(b%a == 0 || a%b==0) cout<<"true"<<endl;
		else cout<<"false"<<endl;
	}
	return 0;
}

Here is the submission: https://toph.co/s/679603

Edit: I have got AC. But why by using printf() and scanf() I have got AC while I got TLE by using cout and cin!!

Possible because of this: Faster I/O | Toph Tutorials

Although, if that really is the case, then the problem statement should have a warning about it. I will check and update the statement.