#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#pragma GCC optimize("unroll-loops")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 230,mod = 1e9+7,inf = 1e9+10,maxm = (1 << 18)+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int n,T;
int dp[N][N][N][3][2];
pll vis[N];
void f(int i,int j,int k,int fl,int b){
if (dp[i][j][k][fl][b] != -1) return;
if (!i && !j && !k){
dp[i][j][k][fl][b] = 1;
return;
}
dp[i][j][k][fl][b] = 0;
if (!fl){
if (!i && !k){
f(i,j,k,1,0);
dp[i][j][k][0][0] = dp[i][j][k][1][0];
return;
}
if (i){
f(i-1,j,k,1,1);
dp[i][j][k][fl][b] = 1ll*i*dp[i-1][j][k][1][1]%mod;
}
if (k){
f(i,j+1,k-1,1,0);
dp[i][j][k][0][0] = mkay(dp[i][j][k][0][0],1ll*k*dp[i][j+1][k-1][1][0]%mod);
}
return;
}
if (fl == 1){
if (!i && !j){
f(i,j,k,2,b);
dp[i][j][k][1][b] = dp[i][j][k][2][b];
return;
}
if (i){
f(i-1,j,k+1,2,b);
dp[i][j][k][fl][b] = 1ll*i*dp[i-1][j][k+1][2][b]%mod;
}
if (j){
f(i,j-1,k,2,1);
dp[i][j][k][fl][b] = mkay(dp[i][j][k][fl][b],1ll*j*dp[i][j-1][k][2][1]%mod);
}
return;
}
if (!j && !k){
if (b == 1){
f(i,j,k,0,0);
dp[i][j][k][2][1] = dp[i][0][0][0][0];
}
else dp[i][j][k][2][0] = 0;
return;
}
if (!b){
if (!k){
dp[i][j][k][2][0] = 0;
return;
}
f(i,j,k-1,0,0);
dp[i][j][k][2][0] = 1ll*k*dp[i][j][k-1][0][0]%mod;
return;
}
if (k){
f(i,j,k-1,0,0);
dp[i][j][k][2][1] = 1ll*k*dp[i][j][k-1][0][0]%mod;
}
if (j){
f(i+1,j-1,k,0,0);
dp[i][j][k][2][1] = mkay(dp[i][j][k][2][1],1ll*dp[i+1][j-1][k][0][0]*j%mod);
}
}
int main(){
memset(dp,-1,sizeof dp);
cin >> n >> T;
int n2 = 2*n,n3 = 3*n;
while (T--){
rep(i,0,n3+1) vis[i] = {-1,-1};
int t0 = 0,t1 = 0,t2 = 0;
rep(j,0,3){
rep(i,0,n2){
int x;
cin >> x;
if (vis[x].X == -1) vis[x].X = j;
else{
vis[x].Y = j;
if (vis[x].X == 0 && vis[x].Y == 1) t0++;
else if (vis[x].X == 1 && vis[x].Y == 2) t1++;
else if (vis[x].X == 0 && vis[x].Y == 2) t2++;
}
}
}
f(t0,t1,t2,0,0);
cout << dp[t0][t1][t2][0][0] << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
285928 KB |
Output is correct |
2 |
Correct |
101 ms |
285984 KB |
Output is correct |
3 |
Correct |
101 ms |
286000 KB |
Output is correct |
4 |
Correct |
106 ms |
286036 KB |
Output is correct |
5 |
Correct |
119 ms |
286140 KB |
Output is correct |
6 |
Correct |
117 ms |
286096 KB |
Output is correct |
7 |
Correct |
141 ms |
286044 KB |
Output is correct |
8 |
Incorrect |
143 ms |
286036 KB |
Output isn't correct |
9 |
Incorrect |
161 ms |
286052 KB |
Output isn't correct |
10 |
Incorrect |
174 ms |
286024 KB |
Output isn't correct |