2015北京网络赛 G Boxes BFS+打表
题意:n个位置摆有n个箱子,每次移动只能把相邻的垒起来,且上面的必须小于下面的。求摆成升序需要移动多少步。
思路:这里的n很小,只有7。但是bfs最快的情况需要2s左右,所以就打表了。
诡异的是n = 6时居然都跑不出来都超时,连6也打了个表。
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 #define LL long long 15 #define eps 1e-8 16 #define INF 0x3f3f3f3f 17 #define MAXN 10005 18 using namespace std; 19 const int ten[10] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 }; 20 const int fact[10] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 }; 21 const int u6[730] = { 0, 12, 4, 8, 16, 18, 4, 10, 6, 10, 12, 14, 8, 12, 10, 12, 14, 16, 18, 18, 18, 18, 18, 22, 4, 12, 8, 12, 14, 16, 6, 12, 8, 12, 14, 16, 10, 14, 12, 14, 16, 18, 16, 18, 18, 20, 20, 22, 8, 14, 10, 14, 16, 18, 10, 16, 12, 14, 16, 18, 12, 16, 14, 16, 18, 20, 18, 20, 20, 20, 22, 24, 12, 16, 14, 16, 18, 20, 14, 18, 16, 18, 20, 22, 16, 18, 18, 20, 20, 22, 20, 22, 22, 24, 24, 26, 22, 22, 22, 22, 22, 26, 22, 24, 22, 24, 24, 26, 22, 24, 24, 24, 26, 28, 24, 26, 26, 28, 28, 32, 4, 14, 8, 12, 16, 18, 8, 14, 10, 14, 16, 18, 12, 16, 14, 16, 18, 20, 18, 20, 20, 22, 22, 24, 6, 14, 10, 14, 16, 18, 8, 14, 10, 14, 16, 18, 12, 16, 14, 16, 18, 20, 18, 20, 20, 22, 22, 24, 10, 16, 12, 16, 18, 20, 12, 18, 14, 16, 18, 20, 14, 18, 16, 18, 20, 22, 20, 22, 22, 22, 24, 26, 14, 18, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 18, 20, 20, 22, 22, 24, 22, 24, 24, 26, 26, 28, 20, 22, 22, 24, 24, 26, 22, 24, 24, 26, 26, 28, 24, 26, 26, 26, 28, 30, 26, 28, 28, 30, 30, 32, 8, 16, 12, 16, 18, 20, 10, 16, 12, 16, 18, 20, 14, 18, 16, 18, 20, 22, 20, 22, 22, 24, 24, 26, 10, 18, 14, 18, 20, 22, 12, 16, 14, 16, 18, 20, 16, 18, 18, 20, 20, 22, 20, 22, 22, 24, 24, 26, 12, 18, 14, 18, 20, 22, 14, 20, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 24, 26, 28, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 22, 24, 20, 22, 22, 24, 24, 26, 24, 26, 26, 26, 28, 30, 22, 24, 24, 26, 26, 28, 24, 26, 24, 26, 26, 28, 26, 28, 28, 28, 30, 30, 28, 30, 30, 30, 32, 34, 12, 18, 14, 18, 20, 22, 14, 20, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 24, 26, 28, 14, 20, 16, 20, 22, 24, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 26, 28, 30, 16, 22, 18, 20, 22, 24, 18, 24, 20, 22, 24, 26, 20, 22, 22, 22, 24, 26, 24, 26, 26, 26, 28, 30, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 24, 26, 22, 24, 24, 26, 26, 28, 26, 28, 28, 28, 30, 32, 24, 26, 26, 26, 28, 30, 26, 28, 28, 28, 30, 32, 28, 28, 30, 30, 30, 32, 30, 32, 32, 32, 34, 36, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 20, 22, 22, 24, 24, 26, 24, 26, 26, 28, 28, 30, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 22, 24, 24, 26, 26, 28, 26, 28, 28, 30, 30, 32, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 26, 28, 24, 26, 26, 26, 28, 30, 28, 30, 30, 30, 32, 34, 22, 24, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 32, 34, 34, 32, 34, 34, 34, 36, 38, 26, 26, 26, 26, 26, 30, 26, 28, 26, 28, 28, 30, 26, 28, 28, 28, 30, 32, 28, 30, 30, 32, 32, 36, 26, 28, 28, 28, 28, 32, 26, 28, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 30, 32, 32, 34, 34, 36, 26, 28, 28, 30, 30, 32, 28, 30, 28, 30, 30, 32, 30, 30, 32, 32, 32, 34, 32, 34, 34, 34, 36, 38, 28, 30, 30, 30, 32, 34, 30, 32, 32, 32, 34, 36, 32, 32, 34, 34, 34, 36, 34, 36, 36, 36, 38, 38, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 36, 38, 38, 36, 38, 38, 38, 40, 42 }; 22 const int u7[5045] = { 0, 12, 4, 8, 16, 18, 4, 10, 6, 10, 12, 14, 8, 12, 10, 12, 14, 16, 18, 18, 18, 18, 18, 22, 4, 12, 8, 12, 14, 16, 6, 12, 8, 12, 14, 16, 10, 14, 12, 14, 16, 18, 16, 18, 18, 20, 20, 22, 8, 14, 10, 14, 16, 18, 10, 16, 12, 14, 16, 18, 12, 16, 14, 16, 18, 20, 18, 20, 20, 20, 22, 24, 12, 16, 14, 16, 18, 20, 14, 18, 16, 18, 20, 22, 16, 18, 18, 20, 20, 22, 20, 22, 22, 24, 24, 26, 22, 22, 22, 22, 22, 26, 22, 24, 22, 24, 24, 26, 22, 24, 24, 24, 26, 28, 24, 26, 26, 28, 28, 32, 4, 14, 8, 12, 16, 18, 8, 14, 10, 14, 16, 18, 12, 16, 14, 16, 18, 20, 18, 20, 20, 22, 22, 24, 6, 14, 10, 14, 16, 18, 8, 14, 10, 14, 16, 18, 12, 16, 14, 16, 18, 20, 18, 20, 20, 22, 22, 24, 10, 16, 12, 16, 18, 20, 12, 18, 14, 16, 18, 20, 14, 18, 16, 18, 20, 22, 20, 22, 22, 22, 24, 26, 14, 18, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 18, 20, 20, 22, 22, 24, 22, 24, 24, 26, 26, 28, 20, 22, 22, 24, 24, 26, 22, 24, 24, 26, 26, 28, 24, 26, 26, 26, 28, 30, 26, 28, 28, 30, 30, 32, 8, 16, 12, 16, 18, 20, 10, 16, 12, 16, 18, 20, 14, 18, 16, 18, 20, 22, 20, 22, 22, 24, 24, 26, 10, 18, 14, 18, 20, 22, 12, 16, 14, 16, 18, 20, 16, 18, 18, 20, 20, 22, 20, 22, 22, 24, 24, 26, 12, 18, 14, 18, 20, 22, 14, 20, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 24, 26, 28, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 22, 24, 20, 22, 22, 24, 24, 26, 24, 26, 26, 26, 28, 30, 22, 24, 24, 26, 26, 28, 24, 26, 24, 26, 26, 28, 26, 28, 28, 28, 30, 30, 28, 30, 30, 30, 32, 34, 12, 18, 14, 18, 20, 22, 14, 20, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 24, 26, 28, 14, 20, 16, 20, 22, 24, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 26, 28, 30, 16, 22, 18, 20, 22, 24, 18, 24, 20, 22, 24, 26, 20, 22, 22, 22, 24, 26, 24, 26, 26, 26, 28, 30, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 24, 26, 22, 24, 24, 26, 26, 28, 26, 28, 28, 28, 30, 32, 24, 26, 26, 26, 28, 30, 26, 28, 28, 28, 30, 32, 28, 28, 30, 30, 30, 32, 30, 32, 32, 32, 34, 36, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 20, 22, 22, 24, 24, 26, 24, 26, 26, 28, 28, 30, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 22, 24, 24, 26, 26, 28, 26, 28, 28, 30, 30, 32, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 26, 28, 24, 26, 26, 26, 28, 30, 28, 30, 30, 30, 32, 34, 22, 24, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 32, 34, 34, 32, 34, 34, 34, 36, 38, 26, 26, 26, 26, 26, 30, 26, 28, 26, 28, 28, 30, 26, 28, 28, 28, 30, 32, 28, 30, 30, 32, 32, 36, 26, 28, 28, 28, 28, 32, 26, 28, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 30, 32, 32, 34, 34, 36, 26, 28, 28, 30, 30, 32, 28, 30, 28, 30, 30, 32, 30, 30, 32, 32, 32, 34, 32, 34, 34, 34, 36, 38, 28, 30, 30, 30, 32, 34, 30, 32, 32, 32, 34, 36, 32, 32, 34, 34, 34, 36, 34, 36, 36, 36, 38, 38, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 36, 38, 38, 36, 38, 38, 38, 40, 42, 4, 16, 8, 12, 18, 20, 8, 14, 10, 14, 16, 18, 12, 16, 14, 16, 18, 20, 20, 22, 22, 22, 22, 26, 8, 16, 12, 16, 18, 20, 10, 16, 12, 16, 18, 20, 14, 18, 16, 18, 20, 22, 20, 22, 22, 24, 24, 26, 12, 18, 14, 18, 20, 22, 14, 20, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 24, 26, 28, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 20, 22, 22, 24, 24, 26, 24, 26, 26, 28, 28, 30, 22, 24, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 26, 28, 28, 28, 30, 32, 28, 30, 30, 32, 32, 34, 6, 16, 10, 14, 18, 20, 10, 16, 12, 16, 18, 20, 14, 18, 16, 18, 20, 22, 20, 22, 22, 24, 24, 26, 8, 16, 12, 16, 18, 20, 10, 16, 12, 16, 18, 20, 14, 18, 16, 18, 20, 22, 20, 22, 22, 24, 24, 26, 12, 18, 14, 18, 20, 22, 14, 20, 16, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 24, 26, 28, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 20, 22, 22, 24, 24, 26, 24, 26, 26, 28, 28, 30, 22, 24, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 26, 28, 28, 28, 30, 32, 28, 30, 30, 32, 32, 34, 10, 18, 14, 18, 20, 22, 12, 18, 14, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 26, 26, 28, 12, 20, 16, 20, 22, 24, 14, 18, 16, 18, 20, 22, 18, 20, 20, 22, 22, 24, 22, 24, 24, 26, 26, 28, 14, 20, 16, 20, 22, 24, 16, 22, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 26, 28, 30, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 24, 26, 22, 24, 24, 26, 26, 28, 26, 28, 28, 28, 30, 32, 24, 26, 26, 28, 28, 30, 26, 28, 26, 28, 28, 30, 28, 30, 30, 30, 32, 32, 30, 32, 32, 32, 34, 36, 14, 20, 16, 20, 22, 24, 16, 22, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 26, 28, 30, 16, 22, 18, 22, 24, 26, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 26, 28, 28, 28, 30, 32, 18, 24, 20, 22, 24, 26, 20, 26, 22, 24, 26, 28, 22, 24, 24, 24, 26, 28, 26, 28, 28, 28, 30, 32, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 28, 30, 30, 30, 32, 34, 26, 28, 28, 28, 30, 32, 28, 30, 30, 30, 32, 34, 30, 30, 32, 32, 32, 34, 32, 34, 34, 34, 36, 38, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 22, 24, 24, 26, 26, 28, 26, 28, 28, 30, 30, 32, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 28, 30, 24, 26, 26, 28, 28, 30, 28, 30, 30, 32, 32, 34, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 28, 30, 26, 28, 28, 28, 30, 32, 30, 32, 32, 32, 34, 36, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 32, 34, 36, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 34, 36, 36, 34, 36, 36, 36, 38, 40, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 30, 32, 32, 34, 34, 36, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 32, 34, 36, 32, 34, 34, 36, 36, 38, 28, 30, 30, 32, 32, 34, 30, 32, 30, 32, 32, 34, 32, 32, 34, 34, 34, 36, 34, 36, 36, 36, 38, 40, 30, 32, 32, 32, 34, 36, 32, 34, 34, 34, 36, 38, 34, 34, 36, 36, 36, 38, 36, 38, 38, 38, 40, 40, 32, 34, 34, 36, 36, 38, 34, 36, 36, 38, 38, 40, 36, 38, 38, 38, 40, 40, 38, 40, 40, 40, 42, 44, 8, 18, 12, 16, 20, 22, 12, 18, 14, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 26, 26, 28, 10, 18, 14, 18, 20, 22, 12, 18, 14, 18, 20, 22, 16, 20, 18, 20, 22, 24, 22, 24, 24, 26, 26, 28, 14, 20, 16, 20, 22, 24, 16, 22, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 26, 28, 30, 18, 22, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 22, 24, 24, 26, 26, 28, 26, 28, 28, 30, 30, 32, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 30, 32, 32, 34, 34, 36, 10, 20, 14, 18, 22, 24, 14, 20, 16, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 28, 28, 30, 12, 18, 16, 18, 20, 22, 14, 18, 16, 18, 20, 22, 18, 20, 20, 22, 22, 24, 22, 24, 24, 26, 26, 28, 16, 20, 18, 20, 22, 24, 18, 22, 20, 22, 24, 26, 20, 22, 22, 24, 24, 26, 24, 26, 26, 28, 28, 30, 20, 22, 22, 24, 24, 26, 22, 24, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 12, 20, 16, 20, 22, 24, 14, 20, 16, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 28, 28, 30, 14, 22, 18, 22, 24, 26, 16, 20, 18, 20, 22, 24, 20, 22, 22, 24, 24, 26, 24, 26, 26, 28, 28, 30, 16, 22, 18, 22, 24, 26, 18, 24, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 26, 28, 28, 28, 30, 32, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 28, 30, 30, 30, 32, 34, 26, 28, 28, 30, 30, 32, 28, 30, 28, 30, 30, 32, 30, 32, 32, 32, 34, 34, 32, 34, 34, 34, 36, 38, 16, 22, 18, 22, 24, 26, 18, 24, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 26, 28, 28, 28, 30, 32, 18, 24, 20, 24, 26, 28, 20, 24, 22, 24, 26, 28, 22, 24, 24, 26, 26, 28, 26, 28, 28, 30, 30, 32, 20, 26, 22, 24, 26, 28, 22, 28, 24, 26, 28, 30, 24, 26, 26, 26, 28, 30, 28, 30, 30, 30, 32, 34, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 30, 32, 32, 32, 34, 36, 28, 30, 30, 30, 32, 34, 30, 32, 30, 32, 32, 34, 32, 32, 34, 34, 34, 36, 34, 36, 36, 36, 38, 40, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 28, 30, 24, 26, 26, 28, 28, 30, 28, 30, 30, 32, 32, 34, 22, 26, 24, 26, 28, 30, 24, 26, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 32, 34, 32, 34, 36, 36, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 34, 36, 38, 30, 32, 32, 34, 34, 36, 32, 34, 32, 34, 34, 36, 34, 36, 36, 36, 38, 38, 36, 38, 38, 38, 40, 42, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 32, 34, 36, 32, 34, 34, 36, 36, 38, 28, 30, 30, 32, 32, 34, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 30, 32, 32, 34, 34, 36, 32, 34, 32, 34, 34, 36, 34, 34, 34, 36, 36, 38, 36, 38, 36, 38, 40, 40, 32, 34, 34, 34, 36, 38, 34, 36, 34, 36, 36, 38, 36, 36, 38, 38, 38, 40, 38, 40, 38, 40, 42, 42, 34, 36, 36, 38, 38, 40, 36, 38, 36, 38, 38, 40, 38, 40, 40, 40, 42, 42, 40, 42, 42, 42, 44, 46, 12, 20, 16, 20, 22, 24, 14, 20, 16, 20, 22, 24, 18, 22, 20, 22, 24, 26, 24, 26, 26, 28, 28, 30, 14, 22, 18, 22, 24, 26, 16, 20, 18, 20, 22, 24, 20, 22, 22, 24, 24, 26, 24, 26, 26, 28, 28, 30, 16, 22, 18, 22, 24, 26, 18, 24, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 26, 28, 28, 28, 30, 32, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 26, 28, 24, 26, 26, 28, 28, 30, 28, 30, 30, 30, 32, 34, 26, 28, 28, 30, 30, 32, 28, 30, 28, 30, 30, 32, 30, 32, 32, 32, 34, 34, 32, 34, 34, 34, 36, 38, 14, 22, 18, 22, 24, 26, 16, 22, 18, 22, 24, 26, 20, 24, 22, 24, 26, 28, 26, 28, 28, 30, 30, 32, 16, 22, 20, 22, 24, 26, 18, 22, 20, 22, 24, 26, 22, 24, 24, 26, 26, 28, 26, 28, 28, 30, 30, 32, 18, 24, 20, 24, 26, 28, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 28, 30, 28, 30, 30, 30, 32, 34, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 30, 32, 32, 32, 34, 36, 28, 30, 30, 32, 32, 34, 30, 32, 30, 32, 32, 34, 32, 34, 34, 34, 36, 36, 34, 36, 36, 36, 38, 40, 16, 24, 20, 24, 26, 28, 18, 22, 20, 22, 24, 26, 22, 24, 24, 26, 26, 28, 26, 28, 28, 30, 30, 32, 18, 26, 22, 26, 28, 30, 20, 24, 22, 24, 26, 28, 24, 26, 26, 28, 28, 30, 28, 30, 30, 32, 32, 34, 20, 24, 22, 24, 26, 28, 22, 26, 22, 24, 26, 28, 24, 26, 26, 26, 28, 30, 28, 30, 30, 30, 32, 34, 24, 26, 26, 28, 28, 30, 26, 28, 26, 28, 28, 30, 28, 30, 30, 30, 32, 32, 30, 32, 32, 32, 34, 36, 28, 30, 30, 32, 32, 34, 30, 32, 30, 32, 32, 34, 32, 34, 34, 34, 36, 36, 34, 36, 36, 36, 38, 40, 18, 24, 20, 24, 26, 28, 20, 26, 22, 24, 26, 28, 22, 26, 24, 26, 28, 30, 28, 30, 30, 30, 32, 34, 20, 26, 22, 26, 28, 30, 22, 26, 24, 26, 28, 30, 24, 26, 26, 28, 28, 30, 28, 30, 30, 32, 32, 34, 22, 28, 24, 26, 28, 30, 24, 30, 26, 28, 30, 32, 26, 28, 28, 28, 30, 32, 30, 32, 32, 32, 34, 36, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 30, 32, 28, 30, 30, 32, 32, 32, 32, 34, 34, 34, 36, 36, 30, 32, 32, 32, 34, 36, 32, 34, 32, 34, 34, 36, 34, 34, 36, 36, 36, 36, 36, 38, 38, 38, 40, 40, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 30, 32, 32, 32, 34, 36, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 32, 34, 34, 34, 36, 38, 26, 30, 28, 30, 30, 32, 28, 32, 30, 32, 32, 34, 30, 32, 32, 32, 34, 34, 32, 34, 34, 34, 36, 38, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 36, 34, 36, 36, 36, 38, 40, 32, 34, 34, 34, 36, 38, 34, 36, 34, 36, 38, 38, 36, 36, 38, 38, 38, 38, 38, 40, 40, 40, 42, 42, 28, 30, 30, 32, 32, 34, 30, 32, 30, 32, 32, 34, 32, 34, 34, 34, 36, 36, 34, 36, 36, 36, 38, 40, 30, 32, 32, 34, 34, 36, 32, 34, 32, 34, 34, 36, 34, 36, 36, 36, 38, 38, 36, 38, 38, 38, 40, 42, 32, 34, 32, 34, 34, 36, 34, 36, 34, 36, 36, 38, 34, 36, 36, 36, 38, 38, 36, 38, 38, 38, 40, 42, 34, 36, 36, 36, 38, 38, 36, 38, 36, 38, 38, 40, 38, 38, 40, 40, 40, 40, 40, 40, 42, 40, 42, 44, 36, 38, 38, 38, 40, 42, 38, 40, 38, 40, 42, 42, 40, 40, 42, 42, 42, 42, 42, 44, 44, 44, 46, 46, 16, 22, 18, 22, 24, 26, 18, 24, 20, 22, 24, 26, 20, 24, 22, 24, 26, 28, 26, 28, 28, 28, 30, 32, 18, 24, 20, 24, 26, 28, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 28, 30, 28, 30, 30, 30, 32, 34, 20, 26, 22, 24, 26, 28, 22, 28, 24, 26, 28, 30, 24, 26, 26, 26, 28, 30, 28, 30, 30, 30, 32, 34, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 28, 30, 26, 28, 28, 30, 30, 32, 30, 32, 32, 32, 34, 36, 28, 30, 30, 30, 32, 34, 30, 32, 32, 32, 34, 36, 32, 32, 34, 34, 34, 36, 34, 36, 36, 36, 38, 40, 18, 24, 20, 24, 26, 28, 20, 26, 22, 24, 26, 28, 22, 26, 24, 26, 28, 30, 28, 30, 30, 30, 32, 34, 20, 26, 22, 26, 28, 30, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 30, 32, 30, 32, 32, 32, 34, 36, 22, 28, 24, 26, 28, 30, 24, 28, 26, 28, 30, 32, 26, 28, 28, 28, 30, 32, 30, 32, 32, 32, 34, 36, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 32, 34, 34, 34, 36, 38, 30, 32, 32, 32, 34, 36, 32, 34, 34, 34, 36, 38, 34, 34, 36, 36, 36, 38, 36, 38, 38, 38, 40, 42, 20, 26, 22, 26, 28, 30, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 30, 32, 30, 32, 32, 32, 34, 36, 22, 28, 24, 28, 30, 32, 24, 28, 26, 28, 30, 32, 26, 28, 28, 30, 30, 32, 30, 32, 32, 34, 34, 36, 24, 28, 26, 28, 30, 32, 26, 30, 26, 28, 30, 32, 28, 30, 28, 30, 32, 32, 32, 34, 34, 34, 36, 38, 26, 30, 28, 30, 30, 32, 28, 32, 28, 30, 32, 34, 30, 32, 32, 32, 34, 34, 32, 34, 34, 36, 36, 38, 32, 34, 34, 34, 36, 38, 34, 36, 34, 36, 36, 38, 36, 36, 38, 38, 38, 40, 38, 40, 40, 40, 42, 44, 22, 28, 24, 26, 28, 30, 24, 30, 26, 28, 30, 32, 26, 28, 28, 28, 30, 32, 30, 32, 32, 32, 34, 36, 24, 30, 26, 28, 30, 32, 26, 30, 28, 30, 32, 34, 28, 30, 30, 30, 32, 34, 32, 34, 34, 34, 36, 38, 26, 32, 28, 30, 32, 34, 28, 34, 30, 32, 34, 36, 30, 30, 32, 32, 32, 34, 34, 34, 36, 36, 36, 38, 28, 30, 30, 30, 32, 34, 30, 32, 30, 32, 32, 34, 32, 32, 34, 34, 34, 34, 34, 36, 36, 36, 38, 38, 32, 34, 34, 34, 36, 38, 34, 36, 34, 36, 36, 38, 36, 36, 38, 38, 38, 40, 38, 40, 40, 40, 42, 44, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 32, 34, 34, 34, 36, 38, 26, 30, 28, 30, 32, 34, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 28, 32, 30, 32, 32, 34, 30, 34, 32, 34, 34, 36, 32, 34, 34, 34, 36, 36, 34, 36, 36, 36, 38, 40, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 38, 38, 38, 36, 38, 38, 38, 40, 42, 34, 36, 36, 36, 38, 40, 36, 38, 36, 38, 38, 40, 38, 38, 40, 40, 40, 42, 40, 42, 42, 42, 44, 44, 30, 32, 32, 32, 34, 36, 32, 34, 34, 34, 36, 38, 34, 34, 36, 36, 36, 38, 36, 38, 38, 38, 40, 42, 32, 34, 34, 34, 36, 38, 34, 36, 36, 36, 38, 40, 36, 36, 38, 38, 38, 40, 38, 40, 40, 40, 42, 44, 34, 36, 36, 36, 38, 40, 36, 38, 36, 38, 38, 40, 38, 38, 38, 40, 40, 42, 40, 40, 40, 42, 42, 44, 36, 36, 38, 38, 38, 40, 38, 38, 38, 40, 40, 42, 40, 40, 42, 42, 42, 42, 40, 42, 42, 42, 44, 46, 38, 40, 40, 40, 42, 44, 40, 42, 40, 42, 42, 44, 42, 42, 44, 44, 44, 46, 44, 46, 46, 46, 48, 48, 20, 24, 22, 24, 26, 28, 22, 26, 24, 26, 28, 30, 24, 26, 26, 28, 28, 30, 28, 30, 30, 32, 32, 34, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 30, 32, 26, 28, 28, 30, 30, 32, 30, 32, 32, 34, 34, 36, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 30, 32, 28, 30, 30, 30, 32, 34, 32, 34, 34, 34, 36, 38, 26, 28, 28, 30, 30, 32, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 34, 36, 38, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 36, 38, 38, 36, 38, 38, 38, 40, 42, 22, 26, 24, 26, 28, 30, 24, 28, 26, 28, 30, 32, 26, 28, 28, 30, 30, 32, 30, 32, 32, 34, 34, 36, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 32, 34, 28, 30, 30, 32, 32, 34, 32, 34, 34, 36, 36, 38, 26, 30, 28, 30, 32, 34, 28, 32, 30, 32, 32, 34, 30, 32, 32, 32, 34, 36, 34, 36, 36, 36, 38, 40, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 36, 38, 40, 32, 34, 34, 36, 36, 38, 34, 36, 36, 38, 38, 40, 36, 38, 38, 38, 40, 40, 38, 40, 40, 40, 42, 44, 24, 28, 26, 28, 30, 32, 26, 30, 28, 30, 32, 34, 28, 30, 30, 32, 32, 34, 32, 34, 34, 36, 36, 38, 26, 30, 28, 30, 32, 34, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 28, 32, 30, 32, 32, 34, 30, 34, 30, 32, 34, 36, 32, 34, 32, 34, 36, 36, 34, 36, 36, 38, 38, 40, 30, 32, 32, 34, 34, 36, 32, 34, 32, 34, 34, 36, 34, 36, 36, 36, 38, 38, 36, 38, 38, 38, 40, 42, 34, 36, 36, 38, 38, 40, 36, 38, 36, 38, 38, 40, 38, 40, 40, 40, 42, 42, 40, 42, 42, 42, 44, 46, 26, 30, 28, 30, 32, 34, 28, 32, 30, 32, 32, 34, 30, 32, 32, 32, 34, 36, 34, 36, 36, 36, 38, 40, 28, 32, 30, 32, 34, 36, 30, 34, 32, 34, 34, 36, 32, 34, 34, 34, 36, 38, 36, 38, 38, 38, 40, 42, 30, 34, 32, 34, 34, 36, 32, 36, 34, 36, 36, 38, 34, 34, 36, 36, 36, 38, 36, 38, 38, 38, 40, 42, 32, 34, 34, 34, 36, 38, 34, 36, 34, 36, 36, 38, 36, 36, 38, 38, 38, 38, 38, 40, 40, 40, 42, 42, 36, 38, 38, 38, 40, 40, 38, 40, 38, 40, 40, 42, 40, 40, 40, 42, 42, 42, 42, 42, 44, 44, 44, 46, 28, 30, 30, 32, 32, 34, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 36, 38, 40, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 38, 38, 40, 36, 38, 38, 38, 40, 42, 32, 34, 34, 36, 36, 38, 34, 36, 36, 38, 38, 40, 36, 38, 38, 38, 40, 40, 38, 38, 40, 40, 40, 42, 34, 36, 36, 38, 38, 40, 36, 38, 38, 40, 40, 42, 38, 40, 40, 42, 42, 42, 40, 42, 42, 42, 44, 46, 36, 38, 38, 38, 40, 42, 38, 40, 38, 40, 40, 42, 40, 40, 42, 42, 42, 44, 42, 44, 44, 44, 46, 48, 32, 34, 34, 36, 36, 38, 34, 36, 36, 38, 38, 40, 36, 38, 38, 38, 40, 40, 38, 40, 40, 40, 42, 44, 34, 36, 36, 38, 38, 40, 36, 38, 38, 40, 40, 42, 38, 40, 40, 40, 42, 42, 40, 42, 42, 42, 44, 46, 36, 38, 38, 40, 40, 42, 38, 40, 38, 40, 40, 42, 40, 40, 40, 42, 42, 44, 42, 42, 42, 44, 44, 46, 38, 40, 40, 40, 42, 42, 40, 42, 40, 42, 44, 44, 42, 42, 44, 44, 44, 44, 44, 44, 46, 44, 46, 48, 40, 42, 42, 42, 44, 46, 42, 44, 42, 44, 44, 46, 44, 44, 46, 46, 46, 48, 46, 48, 48, 48, 50, 52, 30, 30, 30, 30, 30, 34, 30, 32, 30, 32, 32, 34, 30, 32, 32, 32, 34, 36, 32, 34, 34, 36, 36, 40, 30, 32, 32, 32, 32, 36, 30, 32, 32, 34, 34, 36, 32, 34, 34, 34, 36, 38, 34, 36, 36, 38, 38, 40, 30, 32, 32, 34, 34, 36, 32, 34, 32, 34, 34, 36, 34, 34, 36, 36, 36, 38, 36, 38, 38, 38, 40, 42, 32, 34, 34, 34, 36, 38, 34, 36, 36, 36, 38, 40, 36, 36, 38, 38, 38, 40, 38, 40, 40, 40, 42, 42, 34, 36, 36, 38, 38, 40, 36, 38, 38, 40, 40, 42, 38, 40, 40, 40, 42, 42, 40, 42, 42, 42, 44, 46, 30, 32, 32, 32, 32, 36, 32, 34, 32, 34, 34, 36, 32, 34, 34, 34, 36, 38, 34, 36, 36, 38, 38, 42, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 36, 38, 40, 36, 38, 38, 40, 40, 42, 32, 34, 34, 36, 36, 38, 34, 36, 34, 36, 36, 38, 36, 36, 38, 38, 38, 40, 38, 40, 40, 40, 42, 44, 34, 36, 36, 36, 38, 40, 36, 38, 38, 38, 40, 42, 38, 38, 40, 40, 40, 42, 40, 42, 42, 42, 44, 44, 36, 38, 38, 40, 40, 42, 38, 40, 40, 42, 42, 44, 40, 42, 42, 42, 44, 44, 42, 44, 44, 44, 46, 48, 30, 32, 32, 34, 34, 36, 32, 34, 34, 36, 36, 38, 34, 36, 36, 36, 38, 40, 36, 38, 38, 40, 40, 42, 32, 34, 34, 36, 36, 38, 32, 34, 34, 36, 36, 38, 34, 36, 36, 38, 38, 40, 36, 38, 38, 40, 40, 42, 34, 36, 34, 36, 36, 38, 36, 38, 36, 38, 38, 40, 36, 38, 38, 40, 40, 42, 38, 40, 40, 42, 42, 44, 36, 38, 38, 38, 40, 42, 38, 40, 38, 40, 40, 42, 40, 40, 42, 42, 42, 44, 42, 44, 42, 44, 46, 46, 38, 40, 40, 42, 42, 44, 40, 42, 40, 42, 42, 44, 42, 44, 44, 44, 46, 46, 44, 46, 46, 46, 48, 50, 32, 34, 34, 36, 36, 38, 34, 36, 34, 36, 36, 38, 36, 36, 38, 38, 38, 40, 38, 40, 40, 40, 42, 44, 34, 36, 36, 38, 38, 40, 36, 38, 36, 38, 38, 40, 38, 38, 40, 40, 40, 42, 40, 42, 42, 42, 44, 46, 36, 38, 36, 38, 38, 40, 38, 40, 38, 40, 40, 42, 38, 40, 40, 40, 42, 42, 40, 42, 42, 42, 44, 46, 38, 38, 40, 40, 40, 42, 40, 40, 40, 42, 40, 42, 42, 42, 42, 44, 42, 44, 42, 44, 44, 44, 46, 46, 40, 42, 42, 42, 44, 44, 42, 44, 42, 44, 44, 46, 44, 44, 44, 46, 46, 46, 46, 46, 48, 48, 48, 50, 34, 36, 36, 36, 38, 40, 36, 38, 38, 38, 40, 42, 38, 38, 40, 40, 40, 42, 40, 42, 42, 42, 44, 44, 36, 38, 38, 38, 40, 42, 38, 40, 40, 40, 42, 44, 40, 40, 42, 42, 42, 44, 42, 44, 44, 44, 46, 46, 38, 40, 40, 40, 42, 44, 40, 42, 40, 42, 42, 44, 42, 42, 42, 44, 44, 46, 44, 44, 44, 46, 46, 48, 40, 40, 42, 42, 42, 44, 42, 42, 42, 44, 44, 46, 44, 44, 46, 46, 46, 46, 44, 46, 46, 46, 48, 50, 42, 44, 44, 44, 46, 46, 44, 46, 44, 46, 46, 48, 46, 46, 46, 48, 48, 48, 48, 48, 50, 48, 50, 52, 36, 38, 38, 40, 40, 42, 38, 40, 40, 42, 42, 44, 40, 42, 42, 42, 44, 44, 42, 44, 44, 44, 46, 48, 38, 40, 40, 42, 42, 44, 40, 42, 42, 44, 44, 46, 42, 44, 44, 44, 46, 46, 44, 46, 46, 46, 48, 50, 40, 42, 42, 44, 44, 46, 42, 44, 42, 44, 44, 46, 44, 44, 44, 46, 46, 48, 46, 46, 46, 48, 48, 50, 42, 44, 44, 44, 46, 46, 44, 46, 44, 46, 48, 48, 46, 46, 48, 48, 48, 48, 48, 48, 50, 48, 50, 52, 44, 46, 46, 46, 48, 50, 46, 48, 46, 48, 48, 50, 48, 48, 50, 50, 50, 52, 50, 52, 52, 52, 54, 56 }; 23 struct Node{ 24 int x, pos; 25 }; 26 Node a[10]; 27 bool compare(Node a, Node b){ 28 return a.x < b.x; 29 } 30 struct BOX{ 31 int num[10]; 32 BOX(){ 33 num[0] = 0; 34 } 35 }; 36 struct NODE{ 37 int cnt; 38 BOX box[10]; 39 }; 40 int b[10], n, m; 41 bool vis[8000005]; 42 int turn(NODE s){ 43 int f = 0; 44 for (int i = 1; i <= n; i++){ 45 for (int j = 1; j <= s.box[i].num[0]; j++){ 46 f += ten[s.box[i].num[j] - 1] * i; 47 } 48 } 49 return f; 50 } 51 int res; 52 int bfs(){ 53 NODE s; 54 s.cnt = 0; 55 for (int i = 1; i <= n; i++){ 56 s.box[i].num[0] = 1; 57 s.box[i].num[1] = b[i]; 58 } 59 memset(vis, 0, sizeof(vis)); 60 int f = turn(s); 61 if (f == res){ 62 return 0; 63 } 64 vis[f] = true; 65 queue<NODE> Q; 66 Q.push(s); 67 NODE t; 68 while (!Q.empty()){ 69 s = Q.front(); 70 Q.pop(); 71 for (int i = 1; i <= n; i++){ 72 int m = s.box[i].num[0]; 73 if (i != n && m != 0){ 74 if (s.box[i + 1].num[0] == 0 || (s.box[i].num[m] < s.box[i + 1].num[s.box[i + 1].num[0]])){ 75 t = s; 76 t.box[i + 1].num[0]++; 77 t.box[i + 1].num[t.box[i + 1].num[0]] = t.box[i].num[m]; 78 t.box[i].num[0]--; 79 int f = turn(t); 80 if (!vis[f]){ 81 if (f == res) return t.cnt + 1; 82 t.cnt++; 83 Q.push(t); 84 vis[f] = true; 85 } 86 } 87 } 88 if (i != 1 && m != 0){ 89 if (s.box[i - 1].num[0] == 0 || (s.box[i].num[m] < s.box[i - 1].num[s.box[i - 1].num[0]])){ 90 t = s; 91 t.box[i - 1].num[0]++; 92 t.box[i - 1].num[t.box[i - 1].num[0]] = t.box[i].num[m]; 93 t.box[i].num[0]--; 94 int f = turn(t); 95 if (!vis[f]){ 96 if (f == res) return t.cnt + 1; 97 t.cnt++; 98 Q.push(t); 99 vis[f] = true; 100 } 101 } 102 } 103 } 104 } 105 return -1; 106 } 107 108 int r[10]; 109 bool mp[10]; 110 int cnt = 0; 111 void solve(){ 112 for (int i = 1; i <= n; i++){ 113 a[i].x = r[i]; 114 a[i].pos = i; 115 } 116 sort(a + 1, a + n + 1, compare); 117 for (int i = 1; i <= n; i++){ 118 b[i] = a[i].pos; 119 } 120 int ans = bfs(); 121 printf("%d, ", ans); 122 cnt++; 123 } 124 125 void dfs(int x){ 126 if (x > n){ 127 solve(); 128 return; 129 } 130 for (int i = 1; i <= n; i++){ 131 if (mp[i]) continue; 132 r[x] = i; 133 mp[i] = true; 134 dfs(x + 1); 135 mp[i] = false; 136 } 137 } 138 int cantor(int s[], int n){ 139 int res = 0; 140 int cnt; 141 for (int i = 1; i <= n; i++){ 142 cnt = 0; 143 for (int j = i + 1; j <= n; j++){ 144 if (s[j] < s[i]){ 145 cnt++; 146 } 147 } 148 res += cnt * fact[n - i]; 149 } 150 return res; 151 } 152 153 int main() 154 { 155 #ifndef ONLINE_JUDGE 156 freopen("in.txt", "r", stdin); 157 //freopen("out.txt", "w", stdout); 158 #endif // OPEN_FILE 159 int T; 160 scanf("%d", &T); 161 while (T--){ 162 scanf("%d", &n); 163 res = 0; 164 for (int i = n; i >= 1; i--){ 165 res = res * 10 + i; 166 } 167 for (int i = 1; i <= n; i++){ 168 scanf("%d", &a[i].x); 169 a[i].pos = i; 170 } 171 sort(a + 1, a + n + 1, compare); 172 for (int i = 1; i <= n; i++){ 173 b[a[i].pos] = i; 174 } 175 if (n == 6){ 176 int pos = cantor(b, n); 177 printf("%d\n", u6[pos]); 178 continue; 179 } 180 if (n == 7){ 181 int pos = cantor(b, n); 182 //printf("%d\n", pos); 183 //return 0; 184 printf("%d\n", u7[pos]); 185 continue; 186 } 187 int ans = bfs(); 188 printf("%d\n", ans); 189 } 190 }