Official

E - Simple Math 3 Editorial by evima


The problem asks the number of positive integers \(i\) such that:

  • there is no multiple of \(D\) (call it \(x\)) that satisfies \(A + B\times i \leq x \leq A + C \times i\).

First, note that the segment of available numbers always contains a multiple of \(D\) when it is wide enough.

More formally, when the width of the segment is \(D - 1\) or greater, the segment always contains a multiple of \(D\). Thus, we can assume \(i \le \frac{D - 2}{C - B}\) (, from which we can show the answer is finite.)

The essence of the problem is to notice that the following two conditions are equivalent:

  • there is no multiple of \(D\) (call it \(x\)) that satisfies \(Y \leq x \leq Z\);
  • \(\lfloor \frac{Y - 1}{D} \rfloor = \lfloor \frac{Z}{D} \rfloor\).

Furthermore, if the width of the segment is less than \(D - 1\), \(\lfloor \frac{Z}{D} \rfloor - \lfloor \frac{Y - 1}{D} \rfloor\) is \(0\) or \(1\).

Thus, if we let \(K = \lfloor \frac{D - 2}{C - B} \rfloor\), the answer to the problem is:

\[K - \sum_{i=1}^{K} (\lfloor \frac{A + C \times i}{D} \rfloor - \lfloor \frac{A + B \times i - 1}{D} \rfloor)\]

.

We can compute it fast with floor_sum in AC Library.

posted:
last update: