#P8412. Perfect Digital Root Interval

    ID: 21589 Type: Default 1000ms 256MiB

Perfect Digital Root Interval

Perfect Digital Root Interval

Consider the following digital root function in base (k) defined on a number (A):

  1. Convert (A) to its (k)-ary representation (B).
  2. Replace (A) by the sum of the digits of (B).
  3. Repeat step 1 until (B) has only one digit.
  4. Let (x) be the final value (in decimal). Then we define (f(A)=x).

For example, in base (k=10), (f(18)=f(1+8=9)=9).

Given four integers (k, l, r, p), one has to compute (S = \sum_{i=l}^{r} f(i^i) \bmod p). In particular, if (\sum_{i=l}^{r} f(i^i) = p) (i.e. the sum itself equals (p)), then the output should be the string “perfect" instead of the numerical answer.

In this hack construction problem, you are given the values of (k) and (p) only. Your task is to construct an interval ([l, r]) such that (\sum_{i=l}^{r} f(i^i) = p) (so that the answer of the original problem is exactly “perfect"). If no such interval exists, output two copies of (-1).

Note that in base (k) the function (f(\cdot)) is actually the digital root in base (k). For any positive integer (A), one may show that (f(A)) is an integer in the range ([1, k-1]) (with the convention that if (A) is a multiple of (k-1), then (f(A)=k-1)). This observation is crucial in our hack construction.

The hack construction is as follows:

  • If (k = 2), then note that in base 2 every positive number eventually has digital root 1. Hence, choosing (l=1) and (r=p) gives (\sum_{i=1}^{p} 1 = p).
  • If (k > 2) and (p \le k-1), then one may choose an interval of length 1. In particular, if (p < k-1) we output (l=r=p); if (p = k-1) we output (l=r=k-1). In both cases, we guarantee (f(l^l)=p).
  • Otherwise, output (-1 -1) if no solution can be constructed by our method.

Your task is to implement this hack generator.

inputFormat

The input consists of two space-separated integers (k) and (p) on a single line.

outputFormat

Output two space-separated integers (l) and (r) that form an interval such that (\sum_{i=l}^{r} f(i^i) = p). If no such interval can be constructed, output two copies of (-1).

sample

2 5
1 5