#include<bits/stdc++.h>
using namespace std;
int n, v[22], b3[22], b2[22];
long long dp[(1<<22)][22], sol;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for(int i = 0; i < n; ++i)
{
cin >> v[i];
int a = v[i];
b2[i] = __builtin_popcount(v[i]);
while(a)
{
if(a % 3 == 1)
++b3[i];
a /= 3;
}
}
for(int i = 0; i < n; ++i)
dp[(1<<i)][i] = 1;
for(int i = 1; i < (1<<n); ++i)
{
for(int j = 0; j < n; ++j)
{
if(!dp[i][j])
continue;
for(int k = 0; k < n; ++k)
{
if((i & (1<<k)))
continue;
if(b2[j] == b2[k] || b3[j] == b3[k])
dp[i + (1<<k)][k] += dp[i][j];
}
}
}
for(int i = 0; i < n; ++i)
sol += dp[(1<<n) - 1][i];
cout << sol;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
608 KB |
Output is correct |
8 |
Correct |
3 ms |
656 KB |
Output is correct |
9 |
Correct |
3 ms |
656 KB |
Output is correct |
10 |
Correct |
3 ms |
660 KB |
Output is correct |
11 |
Correct |
12 ms |
3288 KB |
Output is correct |
12 |
Correct |
10 ms |
3332 KB |
Output is correct |
13 |
Correct |
45 ms |
11804 KB |
Output is correct |
14 |
Correct |
230 ms |
45848 KB |
Output is correct |
15 |
Correct |
215 ms |
46020 KB |
Output is correct |
16 |
Correct |
218 ms |
46020 KB |
Output is correct |
17 |
Correct |
278 ms |
46020 KB |
Output is correct |
18 |
Correct |
219 ms |
46020 KB |
Output is correct |
19 |
Correct |
1232 ms |
180752 KB |
Output is correct |
20 |
Correct |
950 ms |
181396 KB |
Output is correct |