#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 4e6 + 7;
int n, N;
int totalCards;
int a_arr[MAXN];
int cards[MAXN][2];
int play_order[MAXN];
int chosen[MAXN];
ll cnt;
int minimax(int idx){
if(idx == N){
int maxCard1 = 0, maxPos1 = 0;
int maxCard2 = 0, maxPos2 = 0;
for(int k = 0; k < N; k++){
int pos = play_order[k];
int c1 = cards[pos][chosen[pos]];
int c2 = cards[pos][1 - chosen[pos]];
if(c1 > maxCard1){
maxCard1 = c1;
maxPos1 = pos;
}
if(c2 > maxCard2){
maxCard2 = c2;
maxPos2 = pos;
}
}
int score = 0;
if(maxPos1 % 2 == 0) score++;
if(maxPos2 % 2 == 0) score++;
return score;
}
int pos = play_order[idx];
bool isAlgo = (pos % 2 == 0);
int best = isAlgo ? -1 : 3;
for(int c = 0; c < 2; c++){
chosen[pos] = c;
int val = minimax(idx + 1);
if(isAlgo) best = max(best, val);
else best = min(best, val);
}
chosen[pos] = -1;
return best;
}
bool check(){
for(int s = 1; s <= N; s++){
for(int k = 0; k < N; k++){
play_order[k] = ((s - 1 + k) % N) + 1;
}
for(int i = 0; i <= N; i++){
chosen[i] = -1;
}
int result = minimax(0);
if(result != a_arr[s - 1]) return 0;
}
return 1;
}
void gen(int player, int mask){
if(player > N){
if(check()){
cnt = (cnt + 1) % MOD;
}
return;
}
for(int c1 = 0; c1 < totalCards; c1++){
if(mask & (1 << c1)) continue;
for(int c2 = c1 + 1; c2 < totalCards; c2++){
if(mask & (1 << c2)) continue;
cards[player][0] = c1 + 1;
cards[player][1] = c2 + 1;
gen(player + 1, mask | (1 << c1) | (1 << c2));
}
}
}
void solve(){
cin >> n;
N = 2 * n;
totalCards = 4 * n;
cnt = 0;
for(int i = 0; i < N; i++) cin >> a_arr[i];
gen(1, 0);
cout << cnt % MOD << "\n";
}
bool multi = 1;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
if(multi) cin >> t;
while(t--){
solve();
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <bits/stdc++.h> using namespace std; #define fi first #define se second typedef long long ll; const int MOD = 1e9 + 7; const int MAXN = 4e6 + 7; int n, N; int totalCards; int a_arr[MAXN]; int cards[MAXN][2]; int play_order[MAXN]; int chosen[MAXN]; ll cnt; int minimax(int idx){ if(idx == N){ int maxCard1 = 0, maxPos1 = 0; int maxCard2 = 0, maxPos2 = 0; for(int k = 0; k < N; k++){ int pos = play_order[k]; int c1 = cards[pos][chosen[pos]]; int c2 = cards[pos][1 - chosen[pos]]; if(c1 > maxCard1){ maxCard1 = c1; maxPos1 = pos; } if(c2 > maxCard2){ maxCard2 = c2; maxPos2 = pos; } } int score = 0; if(maxPos1 % 2 == 0) score++; if(maxPos2 % 2 == 0) score++; return score; } int pos = play_order[idx]; bool isAlgo = (pos % 2 == 0); int best = isAlgo ? -1 : 3; for(int c = 0; c < 2; c++){ chosen[pos] = c; int val = minimax(idx + 1); if(isAlgo) best = max(best, val); else best = min(best, val); } chosen[pos] = -1; return best; } bool check(){ for(int s = 1; s <= N; s++){ for(int k = 0; k < N; k++){ play_order[k] = ((s - 1 + k) % N) + 1; } for(int i = 0; i <= N; i++){ chosen[i] = -1; } int result = minimax(0); if(result != a_arr[s - 1]) return 0; } return 1; } void gen(int player, int mask){ if(player > N){ if(check()){ cnt = (cnt + 1) % MOD; } return; } for(int c1 = 0; c1 < totalCards; c1++){ if(mask & (1 << c1)) continue; for(int c2 = c1 + 1; c2 < totalCards; c2++){ if(mask & (1 << c2)) continue; cards[player][0] = c1 + 1; cards[player][1] = c2 + 1; gen(player + 1, mask | (1 << c1) | (1 << c2)); } } } void solve(){ cin >> n; N = 2 * n; totalCards = 4 * n; cnt = 0; for(int i = 0; i < N; i++) cin >> a_arr[i]; gen(1, 0); cout << cnt % MOD << "\n"; } bool multi = 1; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; if(multi) cin >> t; while(t--){ solve(); } return 0; } |
English