My issue
my approach is to find the max of x and y and then replace it with gcd of x and y and then again find the max of x and y and then replace it with lcm of x and y;
this process repeates for k times.
My code
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b);
int lcm(int x, int y);
int main() {
int t;
cin>>t;
while(t--)
{
int x,y,k;
cin>>x>>y>>k;
for(int i=1; i<=k; i++)
{
// alice replace
x=(x>y)?gcd(x,y):x;
y=(y>x)?gcd(x,y):y;
//bob replace
x=(x>y)?lcm(x,y):x;
y=(y>x)?lcm(x,y):y;
}
cout<<x+y<<endl;
}
return 0;
}
int gcd(int a,int b)
{
int mini = min(a,b);
int i=0;
for(i=mini; i>1; i--)
{
if(a%i==0 && b%i==0)
{
return i;
}
}
}
int lcm(int x,int y)
{
int maxi=x*y;
int mini= max(x,y);
int i=0;
for(i=mini; i<=maxi; i++)
{
if(i%x==0 && i%y==0)
{
return i;
}
}
}
Problem Link: GCDLM Problem - CodeChef