Playoff Restoration
Contest: Educational Codeforces Round 113 (Rated for Div. 2)
Difficulty rating: 2600
Tags: bitmasks, brute force, hashing, implementation, meet-in-the-middle

$$$2^k$$$ teams participate in a playoff tournament. The tournament consists of $$$2^k - 1$$$ games. They are held as follows: first of all, the teams are split into pairs: team $$$1$$$ plays against team $$$2$$$, team $$$3$$$ plays against team $$$4$$$ (exactly in this order), and so on (so, $$$2^{k-1}$$$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $$$2^{k-1}$$$ teams remain. If only one team remains, it is declared the champion; otherwise, $$$2^{k-2}$$$ games are played: in the first one of them, the winner of the game "$$$1$$$ vs $$$2$$$" plays against the winner of the game "$$$3$$$ vs $$$4$$$", then the winner of the game "$$$5$$$ vs $$$6$$$" plays against the winner of the game "$$$7$$$ vs $$$8$$$", and so on. This process repeats until only one team remains.

After the tournament ends, the teams are assigned places according to the tournament phase when they were eliminated. In particular:

- the winner of the tournament gets place $$$1$$$;
- the team eliminated in the finals gets place $$$2$$$;
- both teams eliminated in the semifinals get place $$$3$$$;
- all teams eliminated in the quarterfinals get place $$$5$$$;
- all teams eliminated in the 1/8 finals get place $$$9$$$, and so on.

For example, this picture describes one of the possible ways the tournament can go with $$$k = 3$$$, and the resulting places of the teams:

After a tournament which was conducted by the aforementioned rules ended, its results were encoded in the following way. Let $$$p_i$$$ be the place of the $$$i$$$-th team in the tournament. The hash value of the tournament $$$h$$$ is calculated as $$$h = (\sum \limits_{i=1}^{2^k} i \cdot A^{p_i}) \bmod 998244353$$$, where $$$A$$$ is some given integer.

Unfortunately, due to a system crash, almost all tournament-related data was lost. The only pieces of data that remain are the values of $$$k$$$, $$$A$$$ and $$$h$$$. You are asked to restore the resulting placing of the teams in the tournament, if it is possible at all.

Input format:
The only line contains three integers $$$k$$$, $$$A$$$ and $$$h$$$ ($$$1 \le k \le 5$$$; $$$100 \le A \le 10^8$$$; $$$0 \le h \le 998244352$$$).

Output format:
If it is impossible to find any placing of the teams that is consistent with the data you have, print one integer $$$-1$$$.

Otherwise, print $$$2^k$$$ integers, where $$$i$$$-th integer should be equal to $$$p_i$$$ (the place of the $$$i$$$-th team). Note that your answer should be consistent with one of the possible ways the tournament could go, and note that the initial structure of the tournament is fixed (for example, teams $$$1$$$ and $$$2$$$ always play in the first phase of the tournament against each other). If there are multiple ways to restore the places of the teams which are consistent with the data you have, print any of them.

Constraints:
Time limit: 4.0 s
Memory limit: 512.0 MB

Note:
The tournament for the first example is described in the statement.

For the third example, the placing $$$[1, 2, 3, 3]$$$ (team $$$1$$$ gets place $$$1$$$, team $$$2$$$ gets place $$$2$$$, teams $$$3$$$ and $$$4$$$ get place $$$3$$$) could result in a hash value of $$$7020100$$$ with $$$A = 100$$$, but no tournament can result in such placing since teams $$$1$$$ and $$$2$$$ play against each other in the semifinals, so they cannot get two first places.

Example input:
3 1337 75275197

Example output:
5 3 5 2 1 5 5 3

Example input:
2 100 5040100

Example output:
1 3 3 2

Editorial:
There are exactly $$$2^k-1$$$ games in the tournament, each game has only two possible outcomes. So it's possible to bruteforce all $$$2^k-1$$$ possible ways the tournament could go if $$$k$$$ is not large. In fact, this solution is fast enough when $$$k < 5$$$, so if we somehow can handle the case $$$k = 5$$$, we will have a working solution.
To handle $$$k = 5$$$, let's divide the teams into two groups: teams from $$$1$$$ to $$$16$$$ and teams from $$$17$$$ to $$$32$$$. There will be exactly $$$15$$$ matches in each group, and the winners of these two groups will play in the finals. The number of possible ways the games in a group can go is just $$$2^{15}$$$, so let's try to bruteforce all possible results in each group and somehow "merge" them into the results of the whole tournament.
The main idea is to rewrite $$$h$$$ as $$$(h_1 + h_2) \bmod 998244353$$$, where $$$h_1 = (\sum \limits_{i=1}^{16} i \cdot A^{p_i}) \bmod 998244353$$$, and $$$h_2 = (\sum \limits_{i=17}^{32} i \cdot A^{p_i}) \bmod 998244353$$$, find all possible values for $$$h_1$$$ and $$$h_2$$$, and choose a pair of values that yields exactly the given value of $$$h$$$.
We will handle two separate cases: the winner of the first group wins the whole tournament, or the winner of the second group wins the whole tournament. Suppose we are handling the first case (the second is symmetrical). By choosing the results of matches in the first group, we determine the places of the teams from the first group in the whole tournament: the winner of the first group gets place $$$1$$$, the team eliminated in the last match of the first group gets place $$$3$$$, and so on. It means that by choosing one of the $$$2^{15}$$$ possible results in the first group, we can calculate $$$h_1$$$. Let's bruteforce these $$$2^{15}$$$ combinations of results in the first group and store them in some data structure that allows to check whether some value of $$$h_1$$$ is achievable (in the model solution, it's a std::map which maps reachable values of $$$h_1$$$ to combinations of results that yield these values). Then, by choosing the results of matches in the second group, we can calculate $$$h_2$$$; so the remaining part of the solution is to bruteforce all $$$2^{15}$$$ possible results in the second group, calculate $$$h_2$$$ for them, and check that $$$h_1$$$ such that $$$(h_1 + h_2) \bmod 998244353 = h$$$ can be achieved by choosing the results in the first group. Don't forget to also handle the case when the team which wins in the first group loses in the finals (it is almost the same, but the winner in the first group gets place $$$2$$$ and the winner in the second group gets place $$$1$$$).
The technique I've described here (instead of bruteforcing all possible variants, split the thing we try to bruteforce into two parts, bruteforce them separatedly, and then try to "merge" the parts) is called meet-in-the-middle and can be used to solve a large variety of problems.