#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)
{
if(ab==0 && ac==2 && bc==0)
{
ab = 0;
}
int last_nr = ab + ac + bc;
int last_ab = ab;
int last_ac = ac;
if(a==2)
{
/// a da una comuna intre b si c
return;
}
if(b==3)
{
/// b da una comuna intre c si a
return;
}
if(c==1)
{
///
return;
}
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++)
{
for(int b=1; b<=3; b++)
{
for(int c=1; 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2552 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
15 ms |
8024 KB |
Output is correct |
5 |
Correct |
292 ms |
18664 KB |
Output is correct |
6 |
Correct |
522 ms |
22612 KB |
Output is correct |
7 |
Correct |
840 ms |
33440 KB |
Output is correct |
8 |
Correct |
1225 ms |
39052 KB |
Output is correct |
9 |
Correct |
1711 ms |
46488 KB |
Output is correct |
10 |
Execution timed out |
2033 ms |
48960 KB |
Time limit exceeded |