#include <bits/stdc++.h>
using namespace std;
long long n, m, a[25], dp[1<<21][25], ans, cnt[25], cnt1[25];
int main (){
cin >> n;
for (int i = 0;i < n;i++) {
cin >> a[i];
cnt[i] = __builtin_popcount(a[i]);
m = a[i];
while (m){
if (m % 3 == 1) cnt1[i]++;
m /= 3;
}
}
for (int i = 0;i < n;i++) dp[1 << i][i] = 1;
for (int i = 0;i < (1 << n);i++)
for (int j = 0;j < n;j++)
for (int o = 0;o < n;o++)
if (!(i & 1<<o) && (cnt[j] == cnt[o] || cnt1[j] == cnt1[o]))
dp[i|(1<<o)][o] += dp[i][j];
for (int i = 0;i < n;i++) ans += dp[(1<<n) - 1][i];
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
524 KB |
Output is correct |
5 |
Correct |
2 ms |
668 KB |
Output is correct |
6 |
Correct |
13 ms |
808 KB |
Output is correct |
7 |
Correct |
3 ms |
808 KB |
Output is correct |
8 |
Correct |
2 ms |
808 KB |
Output is correct |
9 |
Correct |
3 ms |
808 KB |
Output is correct |
10 |
Correct |
3 ms |
808 KB |
Output is correct |
11 |
Correct |
16 ms |
3812 KB |
Output is correct |
12 |
Correct |
15 ms |
3812 KB |
Output is correct |
13 |
Correct |
66 ms |
13312 KB |
Output is correct |
14 |
Correct |
351 ms |
51900 KB |
Output is correct |
15 |
Correct |
265 ms |
51936 KB |
Output is correct |
16 |
Correct |
365 ms |
51936 KB |
Output is correct |
17 |
Correct |
375 ms |
51936 KB |
Output is correct |
18 |
Correct |
380 ms |
51976 KB |
Output is correct |
19 |
Correct |
1792 ms |
205924 KB |
Output is correct |
20 |
Correct |
1129 ms |
205924 KB |
Output is correct |