| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 2048 MB | 5 | 1 | 1 | 100.000% |
Researcher Isadora loves playing cards with her friends. More specifically, she plays a version called Baralho Alho, in which there are $N$ cards (duplicates are allowed). Initially, the $N$ cards are in a specific order: the $i$-th card has value $A_i$. Two cards are considered equal if they have the same value.
Before the game starts, Isadora declares: “I always shuffle Baralho Alho.” Naively, her friends agree and let her command the shuffling. Little do they know that Researcher Isadora loves to cheat. Her goal is to shuffle in such a way that, at the end of the process, the $i$-th card has value $B_i$.
However, she only knows one type of shuffling: it maps the card originally at position $i$ to position $P_i$. For example, if $P = [3, 2, 4, 1],ドル then the first card goes to the third position, the second remains in place, the third goes to the fourth, and the fourth goes to the first. Thus, if the initial deck is $[4, 2, 6, 1],ドル after applying the shuffling, Isadora gets $[1, 2, 4, 6]$.
Even with this limitation, Isadora is quite intelligent and plans to repeat the shuffling several times in order to reach new deck configurations.
Write a program that, given $A_i,ドル $B_i,ドル and $P_i,ドル determines the minimum number of times Isadora needs to apply the shuffling so that the deck is in the desired order. If this is impossible, print “IMPOSSIVEL” (without quotes). If the minimum number of shuffles is greater than 10ドル^9,ドル print “DEMAIS” (without quotes).
The first line of input contains an integer $N$ (1ドル ≤ N ≤ 10^6$).
The second line contains $N$ integers $A_i$ (1ドル ≤ A_i ≤ 10^9$), representing the initial configuration of the deck.
The third line contains $N$ integers $B_i$ (1ドル ≤ B_i ≤ 10^9$), representing the desired final configuration of the deck.
The fourth line contains $N$ distinct integers $P_i$ (1ドル ≤ P_i ≤ N$), indicating that the card in position $i$ goes to position $P_i$ after one application of the shuffling.
Print a single integer $k$: the minimum number of times the shuffling must be applied, starting from $A_i,ドル until the resulting configuration is $B_i$.
If this is impossible, print “IMPOSSIVEL” (without quotes).
If the minimum $k$ is greater than 10ドル^9,ドル print “DEMAIS” (without quotes).
6 8 6 5 5 1 3 5 1 8 5 3 6 2 3 6 5 1 4
2
We can see the deck’s configuration after each number of shuffles $k$:
Therefore, the answer is $k = 2$.
2 3 3 3 3 1 2
0
In this case, the deck is already in the desired configuration, so no shuffle is needed.
5 6 3 8 4 2 3 6 4 2 8 2 1 4 5 3
5
4 1 2 1 2 1 2 2 1 2 1 4 3
IMPOSSIVEL
3 1 2 3 2 1 4 1 2 3
IMPOSSIVEL