#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
ll dp[205][205][205][3][2];
ll d(int ab, int ac, int bc, int step, bool done){
if(ab < 0 || ac < 0 || bc < 0) return 0;
if(ab + ac + bc == 0) return 1;
ll &r = dp[ab][ac][bc][step][done];
if(r != -1) return r;
r = 0;
if(step == 0){
if(ab + ac == 0){
r = d(ab, ac, bc, 1, 0);
}else{
r = (r + ac*d(ab, ac - 1, bc + 1, 1, 0)%mod)%mod;
r = (r + ab*d(ab - 1, ac, bc, 1, 1)%mod)%mod;
}
}else if(step == 1){
if(ab + bc == 0){
r = d(ab, ac, bc, 2, done);
}else{
r = (r + ab*d(ab - 1, ac + 1, bc, 2, done)%mod)%mod;
r = (r + bc*d(ab, ac, bc - 1, 2, 1)%mod)%mod;
}
}else if(step == 2){
if(ac == 0 && done == 0){
return r = 0;
}else if(ac + bc == 0){
r = d(ab, ac, bc, 0, 0);
}else{
if(done != 0)
r = (r + bc*d(ab + 1, ac, bc - 1, 0, 0)%mod)%mod;
r = (r + ac*d(ab, ac - 1, bc, 0, 0)%mod) % mod;
}
}
return r;
}
void solve(){
set<int> a, b, c;
for(int i = 0; i < 2*n; i++){
int k;
cin>>k;
auto u = a.find(k);
if(u != a.end()){
a.erase(u);
continue;
}
a.insert(k);
}
for(int i = 0; i < 2*n; i++){
int k;
cin>>k;
auto u = b.find(k);
if(u != b.end()){
b.erase(u);
continue;
}
b.insert(k);
}
for(int i = 0; i < 2*n; i++){
int k;
cin>>k;
auto u = c.find(k);
if(u != c.end()){
c.erase(u);
continue;
}
c.insert(k);
}
int ab = 0, ac = 0, bc = 0;
for(int u: a){
if(b.find(u) != b.end()){
ab++;
b.erase(u);
}else if(c.find(u) != c.end()){
ac++;
}
}
for(int u: b){
if(c.find(u) != c.end()) bc++;
}
memset(dp, -1, sizeof(dp));
cout<<d(ab, ac, bc, 0, 0)<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
int t;
cin>>n>>t;
while(t--)
solve();
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
355 ms |
404980 KB |
Output is correct |
2 |
Correct |
462 ms |
405052 KB |
Output is correct |
3 |
Correct |
451 ms |
405112 KB |
Output is correct |
4 |
Correct |
471 ms |
404984 KB |
Output is correct |
5 |
Correct |
906 ms |
404996 KB |
Output is correct |
6 |
Correct |
963 ms |
405312 KB |
Output is correct |
7 |
Correct |
1061 ms |
405184 KB |
Output is correct |
8 |
Correct |
1024 ms |
405136 KB |
Output is correct |
9 |
Correct |
1045 ms |
405112 KB |
Output is correct |
10 |
Correct |
1161 ms |
405232 KB |
Output is correct |