Problem 22 - Two Printers

Back to Problem Solutions forum

trodrigueza     2021-11-28 23:38:41

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.

Rodion (admin)     2021-11-29 06:46:14
User avatar

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.

Please login and solve 5 problems to be able to post at forum