Lost Permutation | 프로그래밍의 벗 PivotOJ
PivotOJ

Lost Permutation

시간 제한: 2000ms메모리 제한: 512MB출처: ICPC 2020-2021 Northwestern Russia Regional ContestBOJ 20243

문제

You once had a permutation π\pi of size nn. And now it's gone. All you have left is an old device you made while studying group theory. To try and recover π\pi you can input a permutation ff of size nn into this device. This device will then display a permutation π1fπ\pi^{-1} \circ f \circ \pi. Find π\pi using at most two interactions with the device.

A permutation of size nn is a sequence of nn distinct integers from 11 to nn. The composition of two permutations aa and bb is a permutation aba \circ b such that (ab)i=bai(a \circ b)_i = b_{a_i}. That is, if we consider a permutation as an action on nn elements, moving element at position ii to aia_i, then aba \circ b is the action that applies aa, then applies bb, so that element at position ii first moves to aia_i, then moves to baib_{a_i}. Note that some definitions of composition use the reverse order.

The inverse permutation π1\pi^{-1} is a permutation σ\sigma such that σπi=i\sigma_{\pi_i} = i. The composition of a permutation and its inverse is equal to an identity permutation: (ππ1)i=(π1π)i=i(\pi \circ \pi^{-1})_i = (\pi^{-1} \circ \pi)_i = i for all ii from 11 to nn. For example, if a=(4,1,3,2)a = (4, 1, 3, 2) and b=(3,2,1,4)b = (3, 2, 1, 4), then ab=(4,3,1,2)a \circ b = (4, 3, 1, 2), a1=(2,4,3,1)a^{-1} = (2, 4, 3, 1) and a1ba=(1,2,4,3)a^{-1} \circ b \circ a = (1, 2, 4, 3).

힌트

There are two test cases in the first test. In the first test case, π=(4,1,3,2)\pi = (4, 1, 3, 2) is the only permutation that satisfies π1(3,2,1,4)π=(1,2,4,3)\pi^{-1} \circ (3, 2, 1, 4) \circ \pi = (1, 2, 4, 3) and π1(2,4,3,1)π=(2,4,3,1)\pi^{-1} \circ (2, 4, 3, 1) \circ \pi = (2, 4, 3, 1). In the second test case, based on the interaction, π\pi can be equal to either (1,3,2)(1, 3, 2), (2,1,3)(2, 1, 3), or (3,2,1)(3, 2, 1). The solution got lucky and guessed the correct one: (3,2,1)(3, 2, 1).

예제

예제 1

입력
2
4

1 2 4 3

2 4 3 1

3

3 1 2

2 3 1
출력
? 3 2 1 4

? 2 4 3 1

! 4 1 3 2

? 2 3 1

? 3 1 2

! 3 2 1
이 문제는 채점 준비 중입니다. 테스트 데이터가 확보되면 제출이 가능합니다.