CHFGCD - Editorial

The goal is to make both numbers, even so that their gcd>1, here 17+1=18 & 29+1=30, total 2 operations.

Oh, got it.
I interpreted the question wrong.

1 Like

why is this logic wrong ?
ll gcd(ll a,ll b)
{
if(a==0)
return b;
return gcd(b%a,a);
}

int main()
{
fio
wit{
ll x,y;
cin>>x>>y;
if(gcd(x,y)>1)
cout<<“0”<<endl;
else if(x%2==0||(y%2==0))
{
cout<<“1”<<endl;
}
else
{ cout<<“2”<<endl;}

}
return 0; 

}

Half of the code is missing, so I’ll use CodeChef: Practical coding for everyone, in which case: consider the test input:

1
3 9
1 Like

OMG this was a bad mistake , thanks for the help.

1 Like

Why 7 27 output is 2 instead of 1?
gcd(7, 28) is 7 > 1

It should be 1 - where are you seeing that the output should be 2?

sorry my bad :upside_down_face:

1 Like

though used the same approach but could not get ac. can any one explain what was my mistake?
my code - Fj10CB - Online C++0x Compiler & Debugging Tool - Ideone.com

It fails on the test input:

1
5 21

Try this
1
3 5

Output should be 1 but your code outputs 2

but my code is giving ans 2 for 5 and 21. isn’t correct?

No: 5+1=6, and \gcd(6,21)=3.

1 Like

Please Help. Why is this giving WA. Cant think of any case it will fail.
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
int t;cin>>t;
while(t–)
{
ll x,y,c=0;cin>>x>>y;
while(__gcd(x,y)==1)
{
if(x<y)swap(x,y);
if(x%2==0)y++;
else x++;
c++;
}
cout<<c<<endl;
}
}

https://www.codechef.com/viewsolution/49217035
Pls help it’s giving me TLE

Maybe stop running sieve()?

1 Like

17 29 becomes 18 and 30 after 2 operations then gcd(18,30)>1… We can change both values but in different operation.

Didnt’t worked for me…still giving me TLE…pls help

For god sake, watch your code - the indentation sucks. Provide a cleaner code. It becomes easy to find out where it went wrong.