#include <bits/stdc++.h>
#define ll long long
#define MAXN 21
using namespace std;
ll a[MAXN],dp[1<<MAXN][MAXN];
bool ok[MAXN][MAXN];
ll Count(ll x)
{
ll cur=387420489ll,cnt=0;
while (x)
{
if (x/cur==1)
cnt++;
x%=cur;
cur/=3;
}
return cnt;
}
bool Check(ll x,ll y)
{
if (__builtin_popcount(x)==__builtin_popcount(y))
return true;
if (Count(x)==Count(y))
return true;
return false;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n;
cin >> n;
for (ll i=0;i<n;i++)
{
cin >> a[i];
dp[1<<i][i]=1;
}
for (ll i=0;i<n;i++)
{
for (ll j=0;j<n;j++)
ok[i][j]=Check(a[i],a[j]);
}
for (ll i=1;i<(1<<n);i++)
{
for (ll j=0;j<n;j++)
{
if (!((1<<j)&i))
continue;
for (ll k=0;k<n;k++)
{
if ((1<<k)&i || !ok[j][k])
continue;
dp[i|(1<<k)][k]+=dp[i][j];
}
}
}
ll ans=0;
for (ll i=0;i<n;i++)
ans+=dp[(1<<n)-1][i];
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
4700 KB |
Output is correct |
12 |
Correct |
5 ms |
4700 KB |
Output is correct |
13 |
Correct |
22 ms |
12892 KB |
Output is correct |
14 |
Correct |
112 ms |
43696 KB |
Output is correct |
15 |
Correct |
103 ms |
43680 KB |
Output is correct |
16 |
Correct |
113 ms |
43692 KB |
Output is correct |
17 |
Correct |
130 ms |
43940 KB |
Output is correct |
18 |
Correct |
116 ms |
43604 KB |
Output is correct |
19 |
Correct |
578 ms |
172788 KB |
Output is correct |
20 |
Correct |
538 ms |
172768 KB |
Output is correct |