Back to Problem Solutions forum
Hi, I don't really understand the maths behind this solution 👇 (it is not mine). Could someone help me?
n = int(input())
result = [int] * n
for i in range(n):
x, y, n = [int(x) for x in input().split()]
t = n / (1.0 / x + 1.0 / y)
r0, r1 = t % x, t % y
if r0 and r1:
result[i] = int(t + min(x - r0, y - r1))
else:
result[i] = int(t)
print (' '.join(map(str, result)))
Code belongs to user Yang Lei.
I poorly remember the task, but this approach is roughly similar to yours. Core difference is in that you decided to calculate division result by successively adding the divisor to itself, which takes quite a time :)
And moreover recalculating this divisor every iteration (gcd
doesn't work for nothing).
I think it's a situation when approach is difficult to explain unless you start rewriting your solution to make it more efficient and come to similar code yourself.