#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 = 105;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
9 ms |
1356 KB |
Output is correct |
5 |
Correct |
174 ms |
15652 KB |
Output is correct |
6 |
Incorrect |
338 ms |
26552 KB |
Output isn't correct |
7 |
Incorrect |
589 ms |
41932 KB |
Output isn't correct |
8 |
Incorrect |
930 ms |
62120 KB |
Output isn't correct |
9 |
Incorrect |
1413 ms |
88132 KB |
Output isn't correct |
10 |
Incorrect |
1960 ms |
116804 KB |
Output isn't correct |