#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
int dp[301][301][301];
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
while(q--){
map<int, int> A;
map<int, int> B;
map<int, int> C;
for(int i = 0; i < n*2; i++){
int x;
cin >> x;
A[x]++;
}
for(int i = 0; i < n*2; i++){
int x;
cin >> x;
B[x]++;
}
for(int i = 0; i < n*2; i++){
int x;
cin >> x;
C[x]++;
}
for(int i = 0; i <= 300; i++){
for(int j = 0; j <= 300; j++){
for(int k = 0; k <= 300; k++){
dp[i][j][k] = 0;
}
}
}
int a = 0, b = 0, c = 0;
for(int i = 1; i <= 3*n; i++){
if(A[i] == 1 && B[i] == 1) a++;
if(A[i] == 1 && C[i] == 1) b++;
if(B[i] == 1 && C[i] == 1) c++;
}
dp[a][b][c] = 1;
auto go = [&](int i, int j, int k, int o, int p, int r){
int x = i, y = j, z = k;
int K = 1;
if(o == -1){
if(x != 0 || y != 0) return;
}
else if(o == 0){
if(x){
K *= x;
x--;
}
else return;
}
else{
if(y){
K *= y;
y--;
z++;
}
else return;
}
if(p == -1){
if(x != 0 || z != 0) return;
}
else if(p == 0){
if(x){
K *= x;
x--;
y++;
}
else return;
}
else{
if(z){
K *= z;
z--;
}
else return;
}
if(r == -1){
if(y != 0 || z != 0) return;
}
else if(r == 0){
if(y){
K *= y;
y--;
}
else return;
}
else{
if(z){
K *= z;
z--;
x++;
}
else return;
}
if(x >= 0 && y >= 0 && z >= 0 && !(x == i && y == j && z == k)){
dp[x][y][z] += K * dp[i][j][k];
dp[x][y][z] %= modulo;
}
};
for(int s = a + b + c; s >= 0; s--){
for(int i = 0; i <= 300; i++){
for(int j = 0; j <= 300 && i + j <= s; j++){
int k = s - i - j;
for(int o = -1; o <= 1; o++){
for(int p = -1; p <= 1; p++){
for(int r = -1; r <= 1; r++){
go(i, j, k, o, p, r);
}
}
}
}
}
}
cout << dp[0][0][0] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
213756 KB |
Output is correct |
2 |
Correct |
231 ms |
213812 KB |
Output is correct |
3 |
Correct |
229 ms |
213852 KB |
Output is correct |
4 |
Correct |
242 ms |
213752 KB |
Output is correct |
5 |
Correct |
713 ms |
213880 KB |
Output is correct |
6 |
Correct |
915 ms |
213884 KB |
Output is correct |
7 |
Correct |
1341 ms |
213880 KB |
Output is correct |
8 |
Correct |
1730 ms |
214040 KB |
Output is correct |
9 |
Execution timed out |
2098 ms |
213900 KB |
Time limit exceeded |
10 |
Execution timed out |
2079 ms |
213900 KB |
Time limit exceeded |