#K85622. Rearrangement Divisibility

    ID: 36683 Type: Default 1000ms 256MiB

Rearrangement Divisibility

Rearrangement Divisibility

You are given a sequence (S) consisting of digits and an integer (k). Your task is to determine whether there exists any rearrangement of the digits in (S) such that the resulting number (N) is divisible by (k). In mathematical terms, you need to check if there is a permutation of (S) for which

[ N \bmod k = 0 ]

If such a permutation exists, output Possible!, otherwise output Impossible!.

Note: Since the number formed by rearranging digits can be large, you should compute the remainder modulo (k) without constructing the entire number if possible. However, for this problem the input size is small, so a brute-force approach via permutations is acceptable.

inputFormat

The input is provided via standard input (stdin) in two lines:

  1. The first line contains the string (S) which is a sequence of digits.
  2. The second line contains the integer (k).

outputFormat

Print a single line to standard output (stdout):

  • Possible! if there exists a rearrangement of (S) such that the resulting number is divisible by (k).
  • Impossible! otherwise.## sample
123
3
Possible!