Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Hints: Spring

For each person, you need the number of days in $[1, m]$ they visit. Alice visits on $a, 2a, 3a, \ldots$ — how many such days are $\le m$?

Answer to Hint 1: Alice visits $\lfloor m/a \rfloor$ times. Similarly, Bob $\lfloor m/b \rfloor$, Carol $\lfloor m/c \rfloor$. Call these $c_a$, $c_b$, $c_c$.

But the water rule depends on how many people visit on a given day. So you need, for each day, whether 1, 2, or 3 people visit. How do you count days with exactly one visitor? Exactly two? All three?

Answer to Hint 2: Use inclusion–exclusion. Days when at least Alice and Bob visit = multiples of $\mathrm{lcm}(a,b)$ in $[1,m]$, i.e. $\lfloor m / \mathrm{lcm}(a,b) \rfloor$. Similarly for (B,C) and (C,A). Days when all three visit = $\lfloor m / \mathrm{lcm}(a,b,c) \rfloor$.

To get “exactly two”: take pairwise counts and subtract the “all three” count so that days with all three are not counted in “exactly two.”

Answer to Hint 3: Define: $c_{ab} = \lfloor m/\mathrm{lcm}(a,b) \rfloor - c_{abc}$, $c_{bc}$ and $c_{ca}$ similarly (each minus $c_{abc}$). So $c_{ab}$ is “exactly A and B (and not necessarily C)”. Then “only A” = $c_a - c_{ab} - c_{ca} - c_{abc}$ (remove days where A is with someone). Similarly for only B and only C.

Answer to Hint 4: Now assign liters. A day with only one visitor gives that person 6 L. A day with exactly two visitors gives each 3 L. A day with all three gives each 2 L.

So: Alice’s total = (days only Alice) $\times 6$ + (days Alice and one other) $\times 3$ + (days all three) $\times 2$. Same for Bob and Carol.

Answer to Hint 5: In code: compute $c_a, c_b, c_c$, $c_{ab}, c_{bc}, c_{ca}$ (pairwise lcm counts minus $c_{abc}$), and $c_{abc}$. Then “only A” = $c_a - c_{ab} - c_{ca} - c_{abc}$, etc. Then $s_a = \texttt{onlyA} \cdot 6 + (c_{ab}+c_{ca}) \cdot 3 + c_{abc} \cdot 2$, and similarly for $s_b$, $s_c$. Use 64-bit integers because $m$ can be up to $10^{17}$.