310019: CF1773A. Amazing Trick

Memory Limit:1024 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Amazing Trick

题意翻译

给定一个排列 $a_{1\sim n}$,请你求出两个排列 $p_{1\sim n}$ 和 $q_{1\sim n}$,使 $a'_i=a_{p_{q_i}}$,得到的新排列 $a'_{1\sim n}$ 满足 $a'_i=i$,但由于一些特殊的原因,你需要保证 $p_i\not=i$ 且 $q_i\not=i$。若无解则输出 `Impossible`,有解则输出 `Possible` 并输出 $p$ 和 $q$。

题目描述

Alice is a magician and she creates a new trick. She has $ n $ cards with different numbers from $ 1 $ to $ n $ written on them. First, she asks an audience member to shuffle the deck and put cards in a row. Let's say the $ i $ -th card from the left has the number $ a_i $ on it. Then Alice picks two permutations $ p $ and $ q $ . There is a restriction on $ p $ and $ q $ — permutations can't have fixed points. Which means $ \forall i: p_i \ne i\ and\ q_i \ne i $ . After permutations are chosen, Alice shuffles the cards according to them. Now the $ i $ -th card from the left is the card $ a[p[q[i]] $ . The trick is considered successful if $ i $ -th card from the left has the number $ i $ on it after the shuffles. Help Alice pick the permutations $ p $ and $ q $ or say it is not possible for the specific starting permutation $ a $ .

输入输出格式

输入格式


The first line of the input contains the number of tests $ t $ ( $ 1 \leq t \leq 10^5 $ ). Each test is described in two lines. The first line contains one integer $ n $ — the number of cards ( $ 1 \leq n \leq 10^5 $ ). The second line contains $ n $ integers $ a_i $ — the initial permutation of the cards ( $ 1 \leq a_i \leq n $ ; $ \forall i \neq j: a_i \neq a_j $ ). It is guaranteed that the sum of $ n $ over all tests does not exceed $ 10^5 $ .

输出格式


Print the answer for each test case in the same order the cases appear in the input. For each test case, print "Impossible" in a single line, if no solution exists. Otherwise, print "Possible" in the first line, and in the following two lines print permutations $ p $ and $ q $ .

输入输出样例

输入样例 #1

4
2
2 1
3
1 2 3
4
2 1 4 3
5
5 1 4 2 3

输出样例 #1

Impossible
Possible
3 1 2
2 3 1
Possible
3 4 2 1
3 4 2 1
Possible
4 1 2 5 3
3 1 4 5 2

Input

题意翻译

给定一个排列 $a_{1\sim n}$,请你求出两个排列 $p_{1\sim n}$ 和 $q_{1\sim n}$,使 $a'_i=a_{p_{q_i}}$,得到的新排列 $a'_{1\sim n}$ 满足 $a'_i=i$,但由于一些特殊的原因,你需要保证 $p_i\not=i$ 且 $q_i\not=i$。若无解则输出 `Impossible`,有解则输出 `Possible` 并输出 $p$ 和 $q$。

Output

**题目大意:**

给定一个长度为 $ n $ 的排列 $ a_1 \sim a_n $,要求找到两个排列 $ p_1 \sim p_n $ 和 $ q_1 \sim q_n $,使得经过两次排列变换后,得到的新排列 $ a'_1 \sim a'_n $ 满足 $ a'_i = i $。其中,第一次变换是按照 $ q $ 排列对 $ a $ 进行重排,第二次变换是按照 $ p $ 排列对第一次变换的结果进行重排。特别要求 $ p $ 和 $ q $ 两个排列都不能有固定的点,即对于所有的 $ i $,都有 $ p_i \neq i $ 和 $ q_i \neq i $。如果不存在这样的排列对,则输出 `Impossible`;如果存在,则输出 `Possible` 以及排列 $ p $ 和 $ q $。

**输入输出数据格式:**

**输入格式:**
- 第一行包含一个整数 $ t $($ 1 \leq t \leq 10^5 $),表示测试用例的数量。
- 每个测试用例包含两行:
- 第一行是一个整数 $ n $($ 1 \leq n \leq 10^5 $),表示卡片的数量。
- 第二行包含 $ n $ 个整数 $ a_i $($ 1 \leq a_i \leq n $),表示卡片的初始排列,且每个数字在排列中只出现一次。
- 所有测试用例的 $ n $ 之和不超过 $ 10^5 $。

**输出格式:**
- 对于每个测试用例,如果无解,则输出一行 `Impossible`。
- 如果有解,则首先输出一行 `Possible`,接着在接下来的两行中分别输出排列 $ p $ 和 $ q $。

**输入输出样例:**

**输入样例 #1:**
```
4
2
2 1
3
1 2 3
4
2 1 4 3
5
5 1 4 2 3
```

**输出样例 #1:**
```
Impossible
Possible
3 1 2
2 3 1
Possible
3 4 2 1
3 4 2 1
Possible
4 1 2 5 3
3 1 4 5 2
```

**注意:** 输入输出样例中的排列是直接给出的,实际编程时需要按照题目要求进行计算得到。**题目大意:** 给定一个长度为 $ n $ 的排列 $ a_1 \sim a_n $,要求找到两个排列 $ p_1 \sim p_n $ 和 $ q_1 \sim q_n $,使得经过两次排列变换后,得到的新排列 $ a'_1 \sim a'_n $ 满足 $ a'_i = i $。其中,第一次变换是按照 $ q $ 排列对 $ a $ 进行重排,第二次变换是按照 $ p $ 排列对第一次变换的结果进行重排。特别要求 $ p $ 和 $ q $ 两个排列都不能有固定的点,即对于所有的 $ i $,都有 $ p_i \neq i $ 和 $ q_i \neq i $。如果不存在这样的排列对,则输出 `Impossible`;如果存在,则输出 `Possible` 以及排列 $ p $ 和 $ q $。 **输入输出数据格式:** **输入格式:** - 第一行包含一个整数 $ t $($ 1 \leq t \leq 10^5 $),表示测试用例的数量。 - 每个测试用例包含两行: - 第一行是一个整数 $ n $($ 1 \leq n \leq 10^5 $),表示卡片的数量。 - 第二行包含 $ n $ 个整数 $ a_i $($ 1 \leq a_i \leq n $),表示卡片的初始排列,且每个数字在排列中只出现一次。 - 所有测试用例的 $ n $ 之和不超过 $ 10^5 $。 **输出格式:** - 对于每个测试用例,如果无解,则输出一行 `Impossible`。 - 如果有解,则首先输出一行 `Possible`,接着在接下来的两行中分别输出排列 $ p $ 和 $ q $。 **输入输出样例:** **输入样例 #1:** ``` 4 2 2 1 3 1 2 3 4 2 1 4 3 5 5 1 4 2 3 ``` **输出样例 #1:** ``` Impossible Possible 3 1 2 2 3 1 Possible 3 4 2 1 3 4 2 1 Possible 4 1 2 5 3 3 1 4 5 2 ``` **注意:** 输入输出样例中的排列是直接给出的,实际编程时需要按照题目要求进行计算得到。

加入题单

算法标签: