Indra is thinking of a number with
With every guess Josh makes, Indra will tell him:
- The number of digits in their exact positions, and
- The number of digits still in the number, but at the wrong position.
Josh has made a total of
Do note that Indra will sometimes troll and give hints that will lead to no possible answers!
Input
Your first line will contain two space-separated integers,
Your next
- Josh's guess of the number,
- The number of digits in their exact positions, and
- The number of digits still in the number, but at the wrong position.
Output
If there is only one possible answer, output the number.
If there are multiple possible answers, output "AMBIGUOUS".
If there is no possible answer, output "IMPOSSIBLE".
Constraints
For all test cases:
- The digits in each of Josh's guesses and in Indra's solution are unique.
Example 1
Input
5 3
128 1 0
157 0 1
841 0 2
362 0 0
624 0 1
Output
478
Explanation
Using the first two hints, Josh can determine that the digit
Afterwards, since
Looking at the third hint, we can determine that the
Back to the second hint where one of them is the correct digit but in the wrong place. The only missing digit is the middle digit, and it cannot be the
Combining everything above, the solution must be
Example 2
Input
4 2
12 0 1
34 0 0
56 0 1
78 0 0
Output
AMBIGUOUS
Explanation
In this scenario, there are two possible answers:
Example 3
Input
5 4
6843 0 3
4038 0 2
0821 0 0
5364 2 1
9712 0 0
Output
IMPOSSIBLE
Explanation
The clues given by Indra are contradictory, so there is no possible answer.
Python Template
n, d = map(int, input().split())
josh = list()
for _ in range(n):
josh.append(map(int, input().split()))
Comments