#P14502. [NCPC 2025] Hidden Permutation

[NCPC 2025] Hidden Permutation

题目描述

When a fixed shuffle is applied to a deck of cards repeatedly, the deck will eventually return to its original order. The number of times the shuffle must be applied before this happens is called the period\textit{period} of the deck for that shuffle. In the present task, we consider the inverse question: rather than starting with a shuffle and finding its period, you are told, for each possible period, how many decks of cards have that period. Your task is to construct any shuffle consistent with this information.

Formally, there is a hidden permutation f=(f1,f2,,fN)f = (f_1, f_2, \dots, f_N), where NN is a given positive integer. A permutation is a list of numbers where each number from 11 to NN occurs exactly once. Let x=x1x2xNx = x_1x_2 \dots x_N be a binary string. We say that f(x)f(x) (ff applied to xx) is the new binary string xf1xf2xfNx_{f_1}x_{f_2} \dots x_{f_N}, and the period\textit{period} of xx is the smallest positive integer pp such that

$$\underbrace{f(f(\cdots f(x)\cdots))}_{p\ \text{times}} = x.$$

It can be proven that for any permutation ff and any binary string xx of the same length, the period of xx exists. In other words, if you apply ff to xx enough times, you eventually get back xx.

You are given the number NN, and for each possible period pp you are given the number of binary strings (out of all 2N2^N possible binary strings) that have period pp. Your task is to find any permutation ff that corresponds to the given information, or conclude that there is no such permutation.

输入格式

The input consists of:

  • One line with integers NN, KK (2N1002 \leq N \leq 100, 1K10001 \leq K \leq 1000), the length of the permutation and the number of possible periods.
  • One line with KK integers p1,p2,,pKp_1, p_2, \dots, p_K (1pi1091 \leq p_i \leq 10^9). These are all the possible periods that binary strings of length NN can have, with respect to the hidden permutation. The numbers pip_i are all distinct, and are sorted in increasing order.
  • One line with KK integers m1,m2,,mKm_1, m_2, \dots, m_K (1mi21001 \leq m_i \leq 2^{100}). This means that there are mim_i binary strings with period pip_i.

Note that the numbers mim_i can be quite large!

输出格式

If there is no permutation ff that corresponds to the given information, print impossible\verb|impossible|.

Otherwise, print one line with NN integers f1,f2,,fNf_1, f_2, \dots, f_N, the permutation that you chose. Recall that the permutation should contain every integer from 11 to NN exactly once. If there are multiple solutions, you can print any one of them.

7 6
1 2 3 4 6 12
4 4 12 24 12 72
2 3 1 5 6 7 4
91 4
1 7 13 91
2 126 8190 2475880078570760549798240131
impossible
2 1
1
4
1 2

提示

In the second sample, the input almost corresponds to the permutation (2,3,4,,91,1)(2,3,4,\dots, 91, 1). The only error is that the last digit in the last number of the input should be 00 instead of 11.

In the third sample, all 44 binary strings have period 11, which means that the identity permutation (1,2)(1,2) is a valid answer.