# CHFGCD - Editorial

Tester: Aryan Choudhary
Editorialist: Vichitr Gandas

CAKEWALK

GCD

# PROBLEM:

Given two positive integers X and Y. Now perform some operations (possibly zero) on them. In each operation, choose X or Y and add 1 to it. Find the minimum number of operations such that the greatest common divisor of X and Y is greater than 1.

# EXPLANATION

We can make gcd > 1 in at most 2 operations by making both numbers even, if they are odd and hence gcd \ge 2. But its possible that we are able to get it with less operations.
So check

• If gcd(A,B)>1, then 0 operations needed.
• Else if gcd(A+1, B) > 1 or gcd(A, B+1)>1 then 1 operation needed.
• Otherwise 2 operations needed.

# TIME COMPLEXITY:

O(\log{(min(A,B))}) per test case

# SOLUTIONS:

Setter's Solution
``````#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1005;

int32_t main()
{
IOS;
int t;
cin >> t;
while(t--)
{
int x, y;
cin >> x >> y;
if(__gcd(x, y) > 1)
cout << 0 << endl;
else
{
if(__gcd(x + 1, y) > 1 || __gcd(x, y + 1) > 1)
cout << 1 << endl;
else
cout << 2 << endl;
}
}
return 0;
}
``````
Tester's Solution
``````/* in the name of Anton */

/*
Compete against Yourself.
Author - Aryan (@aryanc403)
Atcoder library - https://atcoder.github.io/ac-library/production/document_en/
*/

#ifdef ARYANC403
#else
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma GCC optimize ("-ffloat-store")
#include<bits/stdc++.h>
#define dbg(args...) 42;
#endif

using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"

typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403()
{
#ifdef ARYANC403
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time-start_time;
cerr<<"Time Taken : "<<diff.count()<<"\n";
#endif
}

long long readInt(long long l, long long r, char endd) {
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true) {
char g=getchar();
if(g=='-') {
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g&&g<='9') {
x*=10;
x+=g-'0';
if(cnt==0) {
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd) {
if(is_neg) {
x=-x;
}
assert(l<=x&&x<=r);
return x;
} else {
assert(false);
}
}
}
string readString(int l, int r, char endd) {
string ret="";
int cnt=0;
while(true) {
char g=getchar();
assert(g!=-1);
if(g==endd) {
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt&&cnt<=r);
return ret;
}
long long readIntSp(long long l, long long r) {
}
long long readIntLn(long long l, long long r) {
}
string readStringLn(int l, int r) {
}
string readStringSp(int l, int r) {
}

assert(getchar()==EOF);
}

vi a(n);
for(int i=0;i<n-1;++i)
return a;
}

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
auto jt=m.find(x);
if(jt==m.end())         m.insert({x,cnt});
else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
auto jt=m.find(x);
if(jt->Y<=cnt)            m.erase(jt);
else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;
// const lli maxN = 1000000007L;

lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
lli m;
string s;
vi a;
//priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

int main(void) {
ios_base::sync_with_stdio(false);cin.tie(NULL);
// freopen("txt.in", "r", stdin);
// freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
while(T--)
{

if(__gcd(x,y)>1){
cout<<0<<endl;
continue;
}

if(__gcd(x+1,y)>1||__gcd(x,y+1)>1){
cout<<1<<endl;
continue;
}
cout<<2<<endl;
}   aryanc403();
return 0;
}

``````
Editorialist's Solution
``````/*
* @author: vichitr
* @date: 25th July 2021
*/

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fast ios::sync_with_stdio(0); cin.tie(0);

void solve() {
int A, B; cin >> A >> B;
int ans;
if (__gcd(A, B) > 1)
ans = 0;
else if (__gcd(A + 1, B) > 1 or __gcd(A, B + 1) > 1)
ans = 1;
else
ans = 2;
cout << ans << '\n';
}

signed main() {
fast;

#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif

int t = 1;
cin >> t;
for (int tt = 1; tt <= t; tt++) {
// cout << "Case #" << tt << ": ";
solve();
}
return 0;
}
``````

If you have other approaches or solutions, letâ€™s discuss in comments.If you have other approaches or solutions, letâ€™s discuss in comments.

2 Likes

What if the input is 17 29?
It will need 12 operations on 17

Iâ€™ve tried the same approach and i was not able to get AC, can anyone tell me what am i doing wrong.

the output will either be 0, 1, 2 only as we try to make these numbers even and the minimum gcd, even numbers can have is 2 .

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 Solution: 49213027 | CodeChef, 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`?

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

#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