Official

D - FizzBuzz Sum Hard Editorial by en_translator


By Inclusion-Exclusion Principle, the answer can be expressed as:

(The sum of integers no more than \(N\) which is not a multiple of \(A\) or \(B\)) = (The sum of multiples of \(A\) no more than \(N\)) + (The sum of multiples of \(B\) no more than \(N\)) - (The sum of multiples of both \(A\) and \(B\) no more than \(N\)).

Moreover, “multiples of both \(A\) and \(B\)” is equivalent to “multiples of the least common multiple (LCM) of \(A\) and \(B\).”

Here, the sum of multiples of \(A\) no more than \(N\) can be written as \(A(1+2+\ldots+\lfloor N / A \rfloor)\), so if you let \(f(n) := 1 + 2 + \ldots + n = n(n+1)/2\) and \(L := \mathrm{LCM}(A,B)\), the answer can be obtained as \(f(N) - Af(\lfloor N / A \rfloor) - Bf(\lfloor N / B \rfloor) + Lf(\lfloor N / L \rfloor)\).

Therefore, the problem can be solved in a total of \(\mathrm{Ο}(\log \mathrm{min}(A,B))\) time, where the bottleneck is finding the LCM.

posted:
last update: