#P1112. Multi‐Base Wave Numbers

    ID: 13179 Type: Default 1000ms 256MiB

Multi‐Base Wave Numbers

Multi‐Base Wave Numbers

A wave number is defined as a number whose representation in a given base alternates between two distinct digits. In other words, if the number’s representation (without leading zeroes) has at least two digits then it should have the form

$a\,b\,a\,b\,\ldots$

where $a$ and $b$ are two different digits. Single‐digit numbers are also considered wave numbers.

A number is called an m-fold wave number if its representation is a wave number in exactly m different bases among the bases 2 through 16. For example:

  • The decimal number 191919 is a wave number in base 10 (since its digits form 1 9 1 9 1 9) and in base 11 (its representation is 121212), so it is a double wave number.
  • The decimal number 300 has the following representations:
    Base 10: 300
    Base 7: 606
    Base 9: 363
    Base 8: 454
    Base 13: 1A1 Hence, although 300 is not a wave number in base 10 (since 300 does not follow the alternating pattern), it is a wave number in exactly 4 bases (7, 8, 9 and 13) and is called a quadruple wave number.

Your task is: Given a range [L, R] and an integer k (which may be 2, 3, or 4), list in ascending order all the decimal numbers in the range that are wave numbers in exactly k bases among bases 2 through 16. If there is no such number, output -1.

Note on base conversion: When converting a number n into base b (2 ≤ b ≤ 16), use the digits 0–9 and then A, B, C, D, E, F for digits 10 to 15. For example, to convert 300 to base 7, repeatedly divide by 7 to obtain the representation 606.

Input/Output format: see below.

inputFormat

The input consists of a single line with three integers L R k separated by spaces, where L and R (with L ≤ R) define the range of decimal numbers, and k is either 2, 3, or 4, representing the m‑fold wave number type to search for (double, triple, or quadruple, respectively).

outputFormat

Output all the numbers in the range [L, R] that are wave numbers in exactly k bases (from 2 to 16). The numbers should be printed in ascending order separated by a single space. If there is no such number, print -1.

sample

300 300 4
300