eolymp
bolt
Try our new interface for solving problems
Problems

Algorithm of Professor Ivanov

Algorithm of Professor Ivanov

Professor Ivanov has told professor Petrov about a new sorting algorithm, based on the following function \textbf{change}. \textbf{function change(a: integer);} \textbf{begin} \textbf{for j := 0 to n - 1 do} \textbf{begin} \textbf{if p\[j\] = a then} \textbf{k := j} \textbf{end} \textbf{swap(k, (k + p\[k\]) mod n);} \textbf{end;} Function \textbf{swap(i, j)} swaps two elements \textbf{p_i} and \textbf{p_j}. Professor Ivanov states that any permutation \textbf{p_0}, …, \textbf{p_\{n−1\}}, of integers \textbf{1}, ..., \textbf{n}, can be sorted in ascending order with several calls of function \textbf{change}. All you need is to find for each call of function \textbf{change} right integer argument a in limits from \textbf{1} to \textbf{n}. Professor Petrov trusts to his colleague, but he hasn’t understood the algorithm well. So he has suggested a permutation \textbf{p_0}, …, \textbf{p_\{n−1\}} and asked professor Ivanov to sort it in ascending order using function \textbf{change}. \InputFile The first line of input contains an integer \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{500}). The second line contains the permutation \textbf{p_0}, …, \textbf{p_\{n−1\}} of integers \textbf{1} to \textbf{n, suggested by professor Petrov.} \OutputFile If professor Ivanov can’t sort the given permutation using function \textbf{change }print "\textbf{Impossible}". Otherwise, on the first line print integer \textbf{m}: the number of calls to the function (\textbf{0 }≤ \textbf{m} ≤ \textbf{10^6}). On the next \textbf{m} lines, output argument a that should be used in each call (\textbf{1} ≤ \textbf{a} ≤ \textbf{n}). It is guaranteed that if it is possible to sort the given permutation, it can be done in no more than \textbf{10^6} function calls.
Time limit 1 second
Memory limit 64 MiB
Input example #1
5
1 3 5 2 4
Output example #1
37
1
1
2
1
1
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
2
Author O.Soboleva, D.Dublennykh
Source 2013 Petrozavodsk, Winter, Ural FU contest, Kontur Cup, Problem H