#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9+7;
int n,P,C;
int hands[20][2];
int target_a[20];
int ord[20];
int team(int p) { return p%2; } // p=0: pos1=Potykacz=0, p=1: pos2=Algorytmik=1
int mm(int move, int t1[20]) {
if (move == P) {
int t1max=-1,t1w=-1;
for(int p=0;p<P;p++) if(t1[p]>t1max){t1max=t1[p];t1w=p;}
int t2[20];
for(int p=0;p<P;p++) t2[p]=(hands[p][0]==t1[p])?hands[p][1]:hands[p][0];
int t2max=-1,t2w=-1;
for(int p=0;p<P;p++) if(t2[p]>t2max){t2max=t2[p];t2w=p;}
return (team(t1w)==1)+(team(t2w)==1);
}
int cp=ord[move],mt=team(cp);
int best=-1,nt[20];
for(int ci=0;ci<2;ci++){
copy(t1,t1+P,nt);
nt[cp]=hands[cp][ci];
int s=mm(move+1,nt);
if(best==-1) best=s;
else if(mt==1) best=max(best,s);
else best=min(best,s);
}
return best;
}
bool check() {
for(int start=0;start<P;start++){
for(int k=0;k<P;k++) ord[k]=(start+k)%P;
int t1[20]={};
if(mm(0,t1)!=target_a[start]) return false;
}
return true;
}
long long cnt;
vector<int> rem;
void gen(int p) {
if(p==P){if(check())cnt++;return;}
int sz=rem.size();
for(int i=0;i<sz;i++)for(int j=i+1;j<sz;j++){
hands[p][0]=rem[i];hands[p][1]=rem[j];
int ri=rem[i],rj=rem[j];
rem.erase(rem.begin()+j);rem.erase(rem.begin()+i);
gen(p+1);
rem.insert(rem.begin()+i,ri);rem.insert(rem.begin()+j,rj);
}
}
int main(){
int t;cin>>t;
while(t--){
cin>>n;P=2*n;C=4*n;
for(int i=0;i<P;i++) cin>>target_a[i];
rem.clear();for(int i=1;i<=C;i++) rem.push_back(i);
cnt=0;gen(0);
cout<<cnt%MOD<<"\n";
}
}
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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1e9+7; int n,P,C; int hands[20][2]; int target_a[20]; int ord[20]; int team(int p) { return p%2; } // p=0: pos1=Potykacz=0, p=1: pos2=Algorytmik=1 int mm(int move, int t1[20]) { if (move == P) { int t1max=-1,t1w=-1; for(int p=0;p<P;p++) if(t1[p]>t1max){t1max=t1[p];t1w=p;} int t2[20]; for(int p=0;p<P;p++) t2[p]=(hands[p][0]==t1[p])?hands[p][1]:hands[p][0]; int t2max=-1,t2w=-1; for(int p=0;p<P;p++) if(t2[p]>t2max){t2max=t2[p];t2w=p;} return (team(t1w)==1)+(team(t2w)==1); } int cp=ord[move],mt=team(cp); int best=-1,nt[20]; for(int ci=0;ci<2;ci++){ copy(t1,t1+P,nt); nt[cp]=hands[cp][ci]; int s=mm(move+1,nt); if(best==-1) best=s; else if(mt==1) best=max(best,s); else best=min(best,s); } return best; } bool check() { for(int start=0;start<P;start++){ for(int k=0;k<P;k++) ord[k]=(start+k)%P; int t1[20]={}; if(mm(0,t1)!=target_a[start]) return false; } return true; } long long cnt; vector<int> rem; void gen(int p) { if(p==P){if(check())cnt++;return;} int sz=rem.size(); for(int i=0;i<sz;i++)for(int j=i+1;j<sz;j++){ hands[p][0]=rem[i];hands[p][1]=rem[j]; int ri=rem[i],rj=rem[j]; rem.erase(rem.begin()+j);rem.erase(rem.begin()+i); gen(p+1); rem.insert(rem.begin()+i,ri);rem.insert(rem.begin()+j,rj); } } int main(){ int t;cin>>t; while(t--){ cin>>n;P=2*n;C=4*n; for(int i=0;i<P;i++) cin>>target_a[i]; rem.clear();for(int i=1;i<=C;i++) rem.push_back(i); cnt=0;gen(0); cout<<cnt%MOD<<"\n"; } } |
English