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, — how many such days are m?

Answer to Hint 1: Alice visits m/a times. Similarly, Bob m/b, Carol m/c. Call these ca, cb, cc.

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 lcm(a,b) in [1,m], i.e. m/lcm(a,b). Similarly for (B,C) and (C,A). Days when all three visit = m/lcm(a,b,c).

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: cab=m/lcm(a,b)cabc, cbc and cca similarly (each minus cabc). So cab is “exactly A and B (and not necessarily C)”. Then “only A” = cacabccacabc (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) ×6 + (days Alice and one other) ×3 + (days all three) ×2. Same for Bob and Carol.

Answer to Hint 5: In code: compute ca,cb,cc, cab,cbc,cca (pairwise lcm counts minus cabc), and cabc. Then “only A” = cacabccacabc, etc. Then sa=onlyA6+(cab+cca)3+cabc2, and similarly for sb, sc. Use 64-bit integers because m can be up to 1017.