#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int n, dp[(1 << 20)][20], v[20], ok[20][20];
short bits[(1 << 20)][20];
int base2 (int x) {
int cnt = 0;
while (x) {
if (x % 2 == 1)
cnt++;
x /= 2;
}
return cnt;
}
int base3 (int x) {
int cnt = 0;
while (x) {
if (x % 3 == 1)
cnt++;
x /= 3;
}
return cnt;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> v[i];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (base2(v[i]) == base2(v[j]) || base3(v[i]) == base3(v[j]))
ok[i][j] = 1;
for (int i = 1; i < (1 << n); i++) {
for (int j = 0; j < n; j++)
if (i & (1 << j))
bits[i][++bits[i][0]] = j;
}
for (int i = 1; i < (1 << n); i++) {
if (bits[i][0] == 1) {
dp[i][bits[i][1]] = 1;
continue;
}
for (int j = 1; j <= bits[i][0]; j++)
for (int k = 1; k <= bits[i][0]; k++)
if (j != k) {
int b1 = bits[i][j];
int b2 = bits[i][k];
if (ok[b1][b2])
dp[i][b1] = (dp[i ^ (1 << b1)][b2] + dp[i][b1]);
}
}
int ans = 0;
for (int i = 0; i < n; i++)
ans += dp[(1 << n) - 1][i];
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Incorrect |
4 ms |
2252 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |