#P6921. Cheating with Bases
Cheating with Bases
Cheating with Bases
My birthday is coming up and I want to look younger by writing my age in a different base. However, I must choose the base carefully. When I write my age in base \(b\), the representation must use only the decimal digits (0 to 9) and, when this representation is interpreted as a base-10 number, it must be at least a given lower bound \(\ell\).
For example, if my age in base \(10\) is 32, then writing it in base 16 gives "20". But note that when using bases larger than 10, the conversion may produce characters beyond 9, which is not allowed. In fact, since the digits in base \(b\) are always in the range \(0\) to \(b-1\), to ensure that every digit is a decimal digit we need \(b-1 \le 9\), i.e. \(b \le 10\>.
Given two positive integers \(y\) (my age in base 10) and \(\ell\) (the minimum value I want my age to appear as when read in base 10 after conversion), find the largest base \(b\) (with \(2 \le b \le 10\)) such that:
- The representation of \(y\) in base \(b\) uses only decimal digits (0 to 9).
- When this representation is interpreted as a base-10 number, its value is at least \(\ell\).
If no such base exists, output -1.
Note: The representation of \(y\) in base \(b\) is obtained by repeatedly dividing \(y\) by \(b\) and taking remainders. The remainders form the digits (in reverse order). This representation is then read as a usual decimal number.
inputFormat
The input consists of two space-separated positive integers \(y\) and \(\ell\), where:
- \(y\) is your age in base 10.
- \(\ell\) is the lower bound for the decimal value of the age representation.
outputFormat
Output the largest base \(b\) (with \(2 \le b \le 10\)) that satisfies the conditions. If no valid base exists, output -1.
sample
32 21
10