LA 3521

Problem Number - Problem Name
http://acmicpc-live-archive.uva.es/nuevoportal/data/problem.php?p=3521

Summary
A mathematical problem

Explanation
We will accumulate the final result into RES from the following observations:

First observation : Initially, if n > k then (k mod i) gives k for all i >= k. Hence, RES = (n-k) * k. From now on, we will only consider the case n <= k.

Second observation: Let's take k = 80 and i = 18 as an example. We have: 80 = 18    * 4 + 8     --> 80 mod 18 = 8 80 = (18+1) * 4 - 4 + 8 --> 80 mod 19 = 4 80 = (19+1) * 4 - 4 + 4 --> 80 mod 20 = 0

From this, we see that the sequence of positive remainders decreases by the same amount (i.e. 4 in our example above). Hence, the following pseudocode:

while i <= n  r = k mod i;  q = k div i;  if q > 0  // q must be positive anyway because of our assumption that n <= k    RES = RES + sum of (r, r-q, r-2q, ... r-mq) where r-(m+1)q < 0 i = i + (m+1)

The remaining job is finding m and this is left up to the readers.