QOJ.ac

QOJ

Time Limit: 10 s - 20 s Memory Limit: 1024 MB Total points: 16

#5954. 幂交换器

Statistics

在一个平行宇宙中,人们热衷于使用 2 的幂次作为数字,并为 $1$ 到 $2^N$ 的排列定义了一种令人兴奋的排序策略。他们定义了一种交换操作,规则如下:

  • 一个待交换的数字区间是合法的,当且仅当它是一个长度为 $2^k$ 的相邻数字区间,且其起始位置(区间中第一个元素的位置)是 $2^k$ 的倍数(位置从 $0$ 开始计数)。
  • 一次 $k$ 阶交换操作定义为交换两个不同的、合法的、长度均为 $2^k$ 的数字区间。

为了对给定的排列进行排序,你最多可以使用每个阶数 $k$($k \in [0, N)$)的一次交换操作。此外,注意不允许将一个区间与自身交换。

例如,给定排列 $[3, 6, 1, 2, 7, 8, 5, 4]$($1$ 到 $2^3$ 的一个排列),该排列可以通过以下方式排序:

  • $[3, 6, 1, 2, 7, 8, 5, 4]$:对区间 $[3, 6, 1, 2]$ 和 $[7, 8, 5, 4]$ 进行一次 $2$ 阶交换。
  • $[7, 8, 5, 4, 3, 6, 1, 2]$:对 $[5]$ 和 $[3]$ 进行一次 $0$ 阶交换。
  • $[7, 8, 3, 4, 5, 6, 1, 2]$:对 $[7, 8]$ 和 $[1, 2]$ 进行一次 $1$ 阶交换。
  • $[1, 2, 3, 4, 5, 6, 7, 8]$:排序完成。

上述步骤中,每个阶数的交换($0, 1, 2$)最多使用了一次。同时请注意,所有的交换都是合法的,因为对于每个阶数 $k$,两个区间的起始位置都是 $2^k$ 的倍数。

计算通过上述规则对给定排列进行排序的方法数。一种方法是指交换操作的有序序列,仅当两个序列完全相同时,才认为这两种方法相同。

输入格式

输入的第一行包含测试用例的数量 $T$。接下来是 $T$ 个测试用例。每个测试用例的第一行包含一个整数 $N$。接下来一行包含 $2^N$ 个空格分隔的整数:$1, 2, \dots, 2^N$ 的一个排列。

输出格式

对于每个测试用例,输出一行 "Case #x: y",其中 $x$ 是测试用例编号(从 $1$ 开始),$y$ 是使用上述规则对给定排列进行排序的方法数。

数据范围

$1 \le T \le 200$。

小数据

$1 \le N \le 4$。

大数据

$1 \le N \le 12$。

样例

样例输入 1

4
1
2 1
2
1 4 3 2
3
7 8 5 6 1 2 4 3
2
4 3 2 1

样例输出 1

Case #1: 1
Case #2: 3
Case #3: 6
Case #4: 0

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.