#K85622. Rearrangement Divisibility
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:
- The first line contains the string (S) which is a sequence of digits.
- 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!