#include <bits/stdc++.h>
using namespace std;
const int nmax = 100;
const int Mod = 1e9 + 7;
int n;
int dp[3 * nmax + 5][3 * nmax + 5][3 * nmax + 5];
pair<int,int> p[3 * nmax + 5];
void update(int ab, int ac, int bc, int a, int b, int c)
{
int last_nr = ab + ac + bc;
int last_ab = ab;
int last_ac = ac;
int coef = 1;
if(ab + ac == 0)
{
if(a!=1)
{
return;
}
}
else
{
if(a==1)
{
coef = 1LL * coef * ab % Mod;
--ab;
}
else if(a==3)
{
coef = 1LL * coef * ac % Mod;
--ac;
++bc;
}
}
if(ab < 0 || ac < 0 || bc < 0)
{
return;
}
if(bc + ab == 0)
{
if(b!=1)
{
return;
}
}
else
{
if(b==1)
{
coef = 1LL * coef * ab % Mod;
--ab;
++ac;
}
else if(b==2)
{
coef = 1LL * coef * bc % Mod;
--bc;
}
}
if(ab < 0 || ac < 0 || bc < 0)
{
return;
}
if(ac + bc == 0)
{
if(c!=2)
{
return;
}
}
else
{
if(c==2)
{
coef = 1LL * coef * bc % Mod;
--bc;
++ab;
}
else if(c==3)
{
coef = 1LL * coef * ac % Mod;
--ac;
}
}
if(ab < 0 || ac < 0 || bc < 0)
{
return;
}
if(ab + ac + bc == last_nr)
{
return;
}
dp[ab + ac + bc][ab][ac] += 1LL * dp[last_nr][last_ab][last_ac] * coef % Mod;
dp[ab + ac + bc][ab][ac] %= Mod;
}
void solve_test()
{
for(int i=1; i<=2*n; i++)
{
int x;
cin>>x;
if(!p[x].first)
{
p[x].first = 1;
}
else
{
p[x].second = 1;
}
}
for(int i=1; i<=2*n; i++)
{
int x;
cin>>x;
if(!p[x].first)
{
p[x].first = 2;
}
else
{
p[x].second = 2;
}
}
for(int i=1; i<=2*n; i++)
{
int x;
cin>>x;
if(!p[x].first)
{
p[x].first = 3;
}
else
{
p[x].second = 3;
}
}
int nr_tot = 3 * n;
int nrab = 0, nrac = 0;
for(int i=1; i<=3*n; i++)
{
if(p[i].first==p[i].second)
{
--nr_tot;
p[i].first = p[i].second = 0;
continue;
}
if(p[i].first==1 && p[i].second==2)
{
++nrab;
}
else if(p[i].first==1 && p[i].second==3)
{
++nrac;
}
p[i].first = p[i].second = 0;
}
dp[nr_tot][nrab][nrac] = 1;
for(int nr=nr_tot; nr>=1; nr--)
{
for(int ab=0; ab<=nr; ab++)
{
for(int ac=0; ab+ac<=nr; ac++)
{
int bc = nr - ab - ac;
for(int a=1; a<=3; a++)
{
if(a==2)
{
continue;
}
for(int b=1; b<=2; b++)
{
for(int c=2; c<=3; c++)
{
update(ab,ac,bc,a,b,c);
}
}
}
}
}
}
cout<<dp[0][0][0]<<'\n';
for(int nr=nr_tot;nr>=0;nr--)
{
for(int ab=0;ab<=nr;ab++)
{
for(int ac=0;ab+ac<=nr;ac++)
{
dp[nr][ab][ac] = 0;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
int t;
cin>>n>>t;
for(int i=1; i<=t; i++)
{
solve_test();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
4944 KB |
Output is correct |
4 |
Correct |
9 ms |
8028 KB |
Output is correct |
5 |
Correct |
189 ms |
19176 KB |
Output is correct |
6 |
Correct |
339 ms |
22648 KB |
Output is correct |
7 |
Correct |
549 ms |
33420 KB |
Output is correct |
8 |
Correct |
783 ms |
39140 KB |
Output is correct |
9 |
Correct |
1121 ms |
46104 KB |
Output is correct |
10 |
Correct |
1539 ms |
55636 KB |
Output is correct |