#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;
}
map <int, int> dp;
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[hsh(a, b, c, turn, dis)];
if(k) return k-1;
if(turn == 1){
if(a > 0) k = add(k, mul(a, resi(a-1, b, c, 2, 1)));
if(c > 0) k = add(k, mul(c, resi(a, b+1, c-1, 2, dis)));
if(a == 0 && c == 0) k = add(k, resi(a, b, c, 2, dis));
}
else if(turn == 2){
if(b > 0) k = add(k, mul(b, resi(a, b-1, c, 3, 1)));
if(a > 0) k = add(k, mul(a, resi(a-1, b, c+1, 3, dis)));
if(a == 0 && b == 0) k = add(k, resi(a, b, c, 3, dis));
}
else{
if(c > 0) k = add(k, mul(c, resi(a, b, c-1, 1, 0)));
if(dis && b > 0) k = add(k, mul(b, resi(a+1, b-1, c, 1, 0)));
if(c == 0 && b == 0 && dis) k = add(k, resi(a, b, c, 1, 0));
}
dp[hsh(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, 1, 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 |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
9 ms |
1356 KB |
Output is correct |
5 |
Correct |
183 ms |
15568 KB |
Output is correct |
6 |
Correct |
354 ms |
26660 KB |
Output is correct |
7 |
Correct |
606 ms |
41796 KB |
Output is correct |
8 |
Correct |
927 ms |
62144 KB |
Output is correct |
9 |
Correct |
1460 ms |
88012 KB |
Output is correct |
10 |
Correct |
1959 ms |
117016 KB |
Output is correct |