#include <bits/stdc++.h>
using namespace std;
int countOnes(int a, int b) {
int c = 0;
while (a > 0) {
if (a % b == 1) c++;
a /= b;
}
return c;
}
const int MAX_N = 20;
int a[MAX_N], c[MAX_N][MAX_N];
int dp[MAX_N][1 << MAX_N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
c[i][j] = countOnes(a[i], 2) == countOnes(a[j], 2) ||
countOnes(a[i], 3) == countOnes(a[j], 3);
}
}
for (int mask = 1; mask < 1 << n; mask++) {
if (__builtin_popcount(mask) < 2) {
int i = __builtin_ctz(mask);
dp[i][mask] = 1;
continue;
}
for (int x = mask; x > 0; x -= x&-x) {
int i = __builtin_ctz(x);
for (int y = x ^ (1 << i); y > 0; y -= y&-y) {
int j = __builtin_ctz(y);
if (c[i][j]) {
dp[i][mask] += dp[j][mask ^ (1 << i)];
dp[j][mask] += dp[i][mask ^ (1 << j)];
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans += dp[i][(1 << n) - 1];
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
6 |
Correct |
6 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
2 ms |
18780 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
3 ms |
18780 KB |
Output is correct |
11 |
Incorrect |
8 ms |
27224 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |