Did you test your gcd() function? Here is a program with indentation that reflects the actual block structure and a few tests of gcd(). Seen this way, it's pretty clear that gcd() isn't right. For one thing, it never gets past line 13.
Your gcd implementation is incorrect. Not only do you have a misplaced "return a" that causes it to return a if neither a nor b are 0, but it uses subtraction where it would normally use the modulus op. A more usual recursive implementation is:
1 2 3 4
longlongint gcd(longlongint a, longlongint b)
{
return b == 0 ? a : gcd(b, a % b);
}
However, I don't think that's the way to solve the problem. It's really just a matter of calculating how many numbers up to and including N are divisible by A, how many are divisible by B, and how many are divisible by A * B. With that you can solve the problem.
If it's giving the wrong answer, then it must be wrong. I was thinking of this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
int main()
{
int t;
std::cin >> t;
while (t--)
{
int n, a, b, k;
std::cin >> n >> a >> b >> k;
int na = n / a;
int nb = n / b;
int nab = n / (a * b);
if (na + nb - 2 * nab >= k)
std::cout << "Win\n";
else
std::cout << "Lose\n";
}
}
Note that the ones divisible by a*b (nab) are counted in both na and nb, so you need to subtract twice nab.