Help me in solving GCDLM problem

My issue

include
include
using namespace std;

int main() {
int t;
cin >> t;
while (t–) {
int x, y, k;
cin >> x >> y >> k;

``````    for (int i = 1; i <= k; i++) {
int gcd = __gcd(x, y);
if (x > y) {
x = gcd;
y = (x * y) / gcd;
} else {
y = gcd;
x = (x * y) / gcd;
}
}

cout << (x + y) << endl;
}
return 0;
``````

}

My code

``````#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int t;
cin >> t;
while (t--) {
int x, y, k;
cin >> x >> y >> k;

for (int i = 1; i <= k; i++) {
int gcd = __gcd(x, y);
if (x > y) {
x = gcd;
y = (x * y) / gcd;
} else {
y = gcd;
x = (x * y) / gcd;
}
}

cout << (x + y) << endl;
}
return 0;
}

``````

Problem Link: GCDLM Problem - CodeChef

I dont think there is an error in your code rather it is a brute-force approach of solving it. I think it will prompt a tle error.
The problem halts after 2 steps as after that, u are replacing hcf and lcm with the same elements again and again.
Try visualising by taking an example

@anon58721962
U can’t loop through K since it will give u tle.
Hint:- the answer will be constant after 2 operations.
and think about when k==1 too.