#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1000000007;
int add(int a, int b){ a += b; if(a >= MOD) a -= MOD; return a; }
int mul(int a, int b){ return (1LL*a*b)%MOD; }
vector <int> pos[605];
const int G = 205;
int hsh(int a, int b, int c, int turn, int dis){
return G*G*G*a + G*G*b + G*c + 10*turn + dis;
}
int dp[205][205][205][3][2];
int resi(int a, int b, int c, int turn, int dis){
/// a = 1 i 2
/// b = 2 i 3
/// c = 3 i 1
if(a == 0 && b == 0 && c == 0) return 1;
int k = dp[a][b][c][turn][dis];
if(k) return k-1;
if(turn == 0){
if(a > 0) k = add(k, mul(a, resi(a-1, b, c, 1, 1)));
if(c > 0) k = add(k, mul(c, resi(a, b+1, c-1, 1, dis)));
if(a == 0 && c == 0) k = add(k, resi(a, b, c, 1, dis));
}
else if(turn == 1){
if(b > 0) k = add(k, mul(b, resi(a, b-1, c, 2, 1)));
if(a > 0) k = add(k, mul(a, resi(a-1, b, c+1, 2, dis)));
if(a == 0 && b == 0) k = add(k, resi(a, b, c, 2, dis));
}
else{
if(c > 0) k = add(k, mul(c, resi(a, b, c-1, 0, 0)));
if(dis && b > 0) k = add(k, mul(b, resi(a+1, b-1, c, 0, 0)));
if(c == 0 && b == 0 && dis) k = add(k, resi(a, b, c, 0, 0));
}
dp[a][b][c][turn][dis] = k+1;
return k;
}
void solve(int n){
for(int i=1; i<=3*n; i++) pos[i].clear();
for(int i=1; i<=2*n; i++){
int x;
cin >> x;
pos[x].push_back(1);
}
for(int i=1; i<=2*n; i++){
int x;
cin >> x;
pos[x].push_back(2);
}
for(int i=1; i<=2*n; i++){
int x;
cin >> x;
pos[x].push_back(3);
}
int ima1 = 0, ima2 = 0, ima3 = 0;
for(int i=1; i<=3*n; i++){
if(pos[i].size() >= 2 && pos[i][0] != pos[i][1]){
if(pos[i][0] == 1){
if(pos[i][1] == 2) ima1++;
else ima3++;
}
else ima2++;
}
}
cout << resi(ima1, ima2, ima3, 0, 0) << "\n";
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n, t;
cin >> n >> t;
while(t--) solve(n);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
1356 KB |
Output is correct |
4 |
Correct |
5 ms |
4300 KB |
Output is correct |
5 |
Correct |
29 ms |
25312 KB |
Output is correct |
6 |
Correct |
47 ms |
36124 KB |
Output is correct |
7 |
Correct |
69 ms |
48920 KB |
Output is correct |
8 |
Correct |
97 ms |
63616 KB |
Output is correct |
9 |
Correct |
162 ms |
80324 KB |
Output is correct |
10 |
Correct |
170 ms |
96888 KB |
Output is correct |