#P11637. Minimizing A and B with Modular Incrementation

    ID: 13723 Type: Default 1000ms 256MiB

Minimizing A and B with Modular Incrementation

Minimizing A and B with Modular Incrementation

You are given three integers \(a\), \(b\), and \(p\). You can perform the following operation any number of times (possibly zero):

  1. Replace \(a\) with \((a+1) \bmod p\);
  2. Decrease \(b\) by 1.

After each operation, it is required that \(b\) remains a nonnegative integer (a natural number).

Your task is to determine two values after performing some operations optimally:

  • The minimum possible value of \(a\) (interpreted modulo \(p\)).
  • Under the condition that \(a\) is minimized, the minimum possible value of \(b\) (after subtracting the number of operations performed).
  • </p>

    Note: An operation is defined as updating \(a \leftarrow (a+1) \bmod p\) and \(b \leftarrow b-1\). The assignment \(a\leftarrow b\) means that the value of \(b\) is assigned to \(a\) (this is just standard assignment).

    Explanation: Let \(k\) be the number of operations performed, with the constraint \(0 \le k \le b\). Then the final value of \(a\) is \((a+k) \bmod p\) and the final value of \(b\) will be \(b-k\). To minimize \(a\) (which is taken modulo \(p\) and therefore lies between 0 and \(p-1\)), one strategy is to achieve \(a=0\) if possible. Note that \(a+k \equiv 0 \pmod{p}\) if \(k \equiv -a \pmod{p}\). In other words, a candidate for \(k\) is \((p-a)\bmod{p}\) (if \(a=0\), then \((p-0) \bmod p = 0\)). If it is possible to perform at least \( (p-a) \bmod p \) operations (i.e. \(b \ge (p-a) \bmod p\)), then you can achieve \(a=0\). Furthermore, to minimize the remaining \(b = b-k\) under the condition \(a=0\), you should perform the maximum number of operations \(k\) that still satisfies \(k \equiv (p-a)\bmod{p}\) and \(k \le b\). That is, choose the maximum integer \(m\) such that \(k = (p-a)\bmod{p} + m\,p \le b\). Otherwise, if \(b

    inputFormat

    The input consists of a single line containing three space-separated integers: \(a\), \(b\), and \(p\).

    \(0 \le a < p\), \(b \ge 0\), and \(p \ge 1\).

    outputFormat

    Output two integers separated by a space: the minimum achievable value of \(a\) (modulo \(p\)) and the corresponding minimum value of \(b\) after performing the optimal operations.

    sample

    2 1 7
    
    2 1