#include <bits/stdc++.h>
using namespace std;
const int mxn = 20;
long long dp[1 << mxn][mxn];
int a[mxn], bin[mxn], ter[mxn];
signed main() {
std::ios::sync_with_stdio(0);
std::cout.tie(0);
std::cin.tie(0);
#ifdef saarang
freopen("/home/saarang/Documents/cp/input.txt", "r", stdin);
freopen("/home/saarang/Documents/cp/output.txt", "w", stdout);
#endif
int n; cin >> n;
for(int i = 0; i < n; i++) {
cin >> a[i];
int c = 0;
for(int j = 0; j < 31; j++)
c += (a[i] >> j & 1);
bin[i] = c;
int f = 0, x = a[i];
while(x) {
f += (x % 3 == 1);
x /= 3;
}
ter[i] = f;
}
for(int i = 0; i < n; i++)
dp[1 << i][i] = 1;
for(int mask = 1; mask < (1 << n); mask++) {
for(int i = 0; i < n; i++) if(!(mask >> i & 1)) {
for(int j = 0; j < n; j++) if(mask >> j & 1)
if(bin[i] == bin[j] || ter[i] == ter[j])
dp[mask ^ (1 << i)][i] += dp[mask][j];
}
}
long long ans = 0;
for(int i = 0; i < n; i++)
ans += dp[(1 << n) - 1][i];
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
8 ms |
2924 KB |
Output is correct |
12 |
Correct |
7 ms |
2924 KB |
Output is correct |
13 |
Correct |
31 ms |
10604 KB |
Output is correct |
14 |
Correct |
153 ms |
41324 KB |
Output is correct |
15 |
Correct |
134 ms |
41452 KB |
Output is correct |
16 |
Correct |
162 ms |
41324 KB |
Output is correct |
17 |
Correct |
175 ms |
41452 KB |
Output is correct |
18 |
Correct |
170 ms |
41452 KB |
Output is correct |
19 |
Correct |
738 ms |
164460 KB |
Output is correct |
20 |
Correct |
630 ms |
164588 KB |
Output is correct |