Reachable Sequences
Time limit4sMemory limit512 MB
Count ordered pairs of permutations (i, j) where a^(i) can be reached from a^(j) by repeatedly swapping an out-of-order pair.
- Level
Hard8 of 10
- Topics
- Combinatorics, Bit manipulation, Array
- Solved
- No attempts yet
Problem
ZZX has a sequence , which is a permutation of . ZZX wants to modify this sequence. In one modification, he chooses a pair of integers and with and , then swaps and .
If a permutation can be obtained from the initial sequence by performing some (possibly zero) modifications, then ZZX says is reachable from .
JRY has sequences . Each of them is a permutation of . He wants to know how many pairs with and satisfy that is reachable from .
Input
The first line contains an integer . Then test cases follow. In each test case, the first line contains two integers and . After that, lines follow. The -th of them contains integers . Each is a permutation of .
There are at most small test cases and large test case. The small test cases satisfy and . The large test case satisfies and .
Output
For each test case, print the answer on a separate line.