#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int n, v[20], ok[20][20];
short bits[(1 << 20)][20];
long long dp[(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]);
}
}
long long 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 |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
5 ms |
3532 KB |
Output is correct |
12 |
Correct |
5 ms |
3532 KB |
Output is correct |
13 |
Correct |
22 ms |
13124 KB |
Output is correct |
14 |
Correct |
111 ms |
51500 KB |
Output is correct |
15 |
Correct |
101 ms |
51660 KB |
Output is correct |
16 |
Correct |
116 ms |
51524 KB |
Output is correct |
17 |
Correct |
122 ms |
51524 KB |
Output is correct |
18 |
Correct |
134 ms |
51540 KB |
Output is correct |
19 |
Correct |
503 ms |
205424 KB |
Output is correct |
20 |
Correct |
458 ms |
205400 KB |
Output is correct |