BANKGLITCH - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: iceknight1093
Editorialist: iceknight1093

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

You have A units of currency 1 and B units of currency 2.
You can exchange X units of currency 1 for Y units of currency 2 (X \lt Y)

Find the maximum possible total amount of currency.

EXPLANATION:

Because X \lt Y, making a trade is strictly profitable, and so must be done as many times as possible.

Thus, as long as A \ge X, we can convert X units of currency 1 to Y units of currency 2.
This can be done using a while loop, as follows:

while (A >= X) {
    // Make the trade
    A -= X;
    B += Y;
}

In the end, report A+B.

TIME COMPLEXITY:

\mathcal{O}(A) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    a, b, x, y = map(int, input().split())
    while a >= x:
        a -= x
        b += y
    print(a+b)
1 Like