#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
476 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
9 ms |
3412 KB |
Output is correct |
12 |
Correct |
8 ms |
3540 KB |
Output is correct |
13 |
Correct |
37 ms |
13076 KB |
Output is correct |
14 |
Correct |
206 ms |
51496 KB |
Output is correct |
15 |
Correct |
140 ms |
51584 KB |
Output is correct |
16 |
Correct |
220 ms |
51720 KB |
Output is correct |
17 |
Correct |
251 ms |
51588 KB |
Output is correct |
18 |
Correct |
236 ms |
51592 KB |
Output is correct |
19 |
Correct |
1052 ms |
205604 KB |
Output is correct |
20 |
Correct |
645 ms |
205484 KB |
Output is correct |