Official

B - Make N Editorial by evima


We explain the case in which \(AX \ge Y\) and \(BX \ge Z\). (Otherwise, it is optimal to greedily use the operations in descending order of (increment)/(cost), so the problem can be solved in \(\mathrm{O}(1)\) time.)
Assume \( \frac{Y}{A} \le \frac{Z}{B}\) without loss of generality. (Otherwise, swap \(Y, Z\) and swap \(A, B\).)

The operation of increasing \(P\) by \(A\) will be used at most \(\left \lfloor \frac{N}{A} \right \rfloor\) times, because using it more than \(\left \lfloor \frac{N}{A} \right \rfloor\) times would make \(P > N\), making the objective unachievable.

The operation of increasing \(P\) by \(B\) can be assumed to be used at most \(A-1\) times, because if it was used \(A\) or more times, we could replace \(A\) uses of the operation “increase \(P\) by \(B\)” with \(B\) uses of the operation “increase \(P\) by \(A\)” without increasing the total cost.

Thus, we can solve the problem by trying all possible numbers of uses of the operation “increase \(P\) by \(A\)” if \( \left \lfloor \frac{N}{A} \right \rfloor < A - 1\), and those of the operation “increase \(P\) by \(B\)” otherwise, in \(\mathrm{O}(T\sqrt N)\) time.

posted:
last update: