#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
ll n, a[22], base2[22], base3[22];
ll Base3(ll x) {
ll ans = 0;
while (x > 0) {
ans += (x % 3 == 1);
x /= 3;
}
return ans;
}
ll dp[1 << 20][20];
ll solve(ll mask, ll last) {
if (mask == (1 << n) - 1) return dp[mask][last] = 1;
if (dp[mask][last]) return dp[mask][last];
ll ans = 0;
for (int i = 0; i < n; i++) {
if (!(mask & (1 << i)) && (mask == 0 || base2[i] == base2[last] || base3[i] == base3[last])) {
ans += solve(mask | (1 << i), i);
}
}
return dp[mask][last] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
base2[i] = __builtin_popcount(a[i]);
base3[i] = Base3(a[i]);
}
cout << solve(0, 0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Execution timed out |
3079 ms |
2868 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |