atcoder#ARC060B. [ABC044D] 桁和
[ABC044D] 桁和
Score : points
Problem Statement
For integers and , let the function be defined as follows:
- , when
- $f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b)$, when
Here, denotes the largest integer not exceeding , and denotes the remainder of divided by .
Less formally, is equal to the sum of the digits of written in base . For example, the following hold:
You are given integers and . Determine if there exists an integer such that . If the answer is positive, also find the smallest such .
Constraints
- are integers.
Input
The input is given from Standard Input in the following format:
Output
If there exists an integer such that , print the smallest such .
If such does not exist, print -1
instead.
87654
30
10
87654
138
100
87654
45678
-1
31415926535
1
31415926535
1
31415926535
-1