logo AlgoBeat OnlineJudge
登录 注册

#213378. [GCJ 2014 Finals] Power Swapper

内存限制:1024 MiB 时间限制:5000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

In a parallel universe, people are crazy about using numbers that are powers of two, and they have defined an exciting sorting strategy for permutations of the numbers from 1 to . They have defined a swapping operation in the following way:

  • A range of numbers to be swapped is valid if and only if it is a range of adjacent numbers of size , and its starting position (position of the first element in the range) is a multiple of (where positions are 0 -indexed).
  • A valid swap operation of size- is defined by swapping two distinct, valid ranges of numbers, each of size .

To sort the given permutation, you are allowed to use at most one swap operation of each size , for in . Also, note that swapping a range with itself is not allowed.

For example, given the permutation (a permutation of the numbers from 1 to ), the permutation can be sorted as follows:

  • : make a size-2 swap of the ranges and .
  • : make a size-0 swap of and .
  • : make a size-1 swap of and .
  • : done.

The previous steps used every swap size (0,1 , and 2) at most once. Also, notice that all the swaps were valid because both ranges for each size started at indices that were multiples of .

Count how many ways there are to sort the given permutation by using the rules above. A way is an ordered sequence of swaps, and two ways are the same only if the sequences are identical.

输入格式

The first line of the input gives the number of test cases, . test cases follow. The first line of each test case contains a single integer . The following line contains space-separated integers: a permutation of the numbers .

输出格式

For each test case, output one line containing "Case #x: y", where is the test case number (starting from 1) and is the number of ways to sort the given permutation using the rules above.

样例

样例输入 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

数据范围与提示

Limits

  • .

Small dataset(4 Pts)

  • Time limit: 60 3 seconds.
  • .

Large dataset(12 Pts)

  • Time limit: 120 5 seconds.
  • .