QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 128 MB Puntuación total: 100

#4112. Sorting

Estadísticas

Xiao A has a permutation $A[1..2^N]$ of $1 \sim 2^N$, and he wants to sort the array $A$ in ascending order. Xiao A can perform $N$ types of operations, and each type of operation can be performed at most once. For all $i$ ($1 \le i \le N$), the $i$-th type of operation is: divide the sequence from left to right into $2^{N-i+1}$ segments, each containing exactly $2^{i-1}$ numbers, and then swap two of these segments. Xiao A wants to know how many different operation sequences can sort the array $A$ in ascending order. Xiao A considers two operation sequences to be different if and only if the number of operations is different, or at least one operation is different (different type or different position).

Below is an example of the operations: $N=3$, initial permutation $A[1..8]$ is $[3,6,1,2,7,8,5,4]$. First operation: Perform the 3rd type of operation, swap $A[1..4]$ and $A[5..8]$, the resulting $A[1..8]$ is $[7,8,5,4,3,6,1,2]$; Second operation: Perform the 1st type of operation, swap $A[3]$ and $A[5]$, the resulting $A[1..8]$ is $[7,8,3,4,5,6,1,2]$; Third operation: Perform the 2nd type of operation, swap $A[1..2]$ and $A[7..8]$, the resulting $A[1..8]$ is $[1,2,3,4,5,6,7,8]$.

Input

The first line contains an integer $N$. The second line contains $2^N$ integers, $A[1], A[2], \dots, A[2^N]$.

Output

A single line containing an integer, representing the number of different operation sequences that can sort the array $A$ in ascending order.

Constraints

For 30% of the data, $1 \le N \le 4$; For all data, $1 \le N \le 12$.

Examples

Input 1

3
7 8 5 6 1 2 4 3

Output 1

6

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.