Solving efficiently using gcd, lcm