#P8361. Magic Permutation Numbers in Base B

    ID: 21540 Type: Default 1000ms 256MiB

Magic Permutation Numbers in Base B

Magic Permutation Numbers in Base B

Little Z loves programming. One day, while solving a problem, she discovered the magical property of the integer \(142857\):

\(142857 \times 2 = 285714\), and the digits of \(285714\) are a permutation of those of \(142857\).

Curious to see if there exists a larger number with the same property, she searched further and found intriguing examples such as:

\(26835741 \times 2 = 53671482\) and \(0987312654 \times 2 = 1974625308\).

Now, given a base \(B\) and a digit count \(n\), your task is to find an \(n\)-digit positive integer \(x\) (in base \(B\), with leading zeros allowed) such that:

  • The digits of \(2x\) (when written in base \(B\) with exactly \(n\) digits) form a permutation of the digits of \(x\).
  • For every position \(1 \leq i \leq n\), the \(i\)th digit of \(x\) and the \(i\)th digit of \(2x\) are not both zero.

If such an \(x\) exists, output its \(n\)-digit representation (preserving any leading zeros); otherwise, output \(-1\).

inputFormat

The input consists of a single line containing two integers \(n\) and \(B\) separated by a space, where \(1 \leq n \leq 10\) and \(2 \leq B \leq 10\).

outputFormat

Output the \(n\)-digit number \(x\) that meets the stated conditions. If no such number exists, output \(-1\).

sample

6 10
142857