C - Candy Tribulation 解説 by spheniscine

Alternative Solution

Let \(S_i\) represent the set of possible weights that the \(i\)-th child might receive (i.e. the set of possible sums of weights for \(A_i\) candies)

It’s plain to see that \(S_i\) ranges from \(X \cdot A_i\) to \(Y \cdot A_i\) inclusive, and form an arithmetic progression with gap \(Y - X\). Thus we can find the intersection of all \(S_1 \cap S_2 \cap ... \cap S_N\); it’s empty if \(X \cdot A_i\) don’t all have the same remainders modulo \(Y - X\), or if \(\max _i X \cdot A_i > \min_i Y \cdot A_i \), otherwise it’s the arithmetic progression between \(\max _i X \cdot A_i\) to \(\min_i Y \cdot A_i \) with gap \(Y - X\).

If the intersection exists, we get the heaviest weight from it \(w = \min_i Y \cdot A_i\), then we may calculate the number of large candies that each child should receive to get that weight: \(\dfrac {w - X \cdot A_i} {Y - X}\)

投稿日時:
最終更新: