#include "bits/stdc++.h"
#define ll long long
using namespace std;
const int sz = 22;
ll dp[1 << sz][sz];
int tr[sz], tw[sz];
bool ok[sz][sz];
int a[sz];
int cO(int a, int b) {
int r = 0;
for (int c = a; c; c /= b)
r += 1 == c % b;
return r;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i ++)
cin >> a[i];
for (int i = 0; i < n; i ++) {
tw[i] = cO(a[i], 2);
tr[i] = cO(a[i], 3);
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
ok[i][j] = tr[i] == tr[j] || tw[i] == tw[j];
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 k = 0; k < n; k ++) {
if (j != k) if (i & (1 << j)) if (!(i & (1 << k))) if (ok[j][k])
dp[i | (1 << k)][k] += dp[i][j];
}
}
}
ll r = 0;
for (int i = 0; i < n; i ++)
r += dp[(1 << n) - 1][i];
cout << r << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
8 ms |
4700 KB |
Output is correct |
12 |
Correct |
9 ms |
3232 KB |
Output is correct |
13 |
Correct |
48 ms |
11600 KB |
Output is correct |
14 |
Correct |
212 ms |
45396 KB |
Output is correct |
15 |
Correct |
204 ms |
45440 KB |
Output is correct |
16 |
Correct |
207 ms |
45648 KB |
Output is correct |
17 |
Correct |
211 ms |
45396 KB |
Output is correct |
18 |
Correct |
217 ms |
45564 KB |
Output is correct |
19 |
Correct |
1087 ms |
180900 KB |
Output is correct |
20 |
Correct |
1043 ms |
180992 KB |
Output is correct |