#include <bits/stdc++.h>
using namespace std;
using ll = long long;
pair<int, int> ones[21];
int n;
int ternary_ones(int x) {
int s = 0;
while(x > 0) {
if (x % 3 == 1)
s++;
x /= 3;
}
return s;
}
ll cnt = 0;
void go(int id, bool used[], pair<int, int> last) {
if (id == n) {
cnt++;
return;
}
for(int i = 0; i < n; i++) {
if (used[i])
continue;
if (ones[i].first == last.first || ones[i].second == last.second) {
used[i] = true;
go(id + 1, used, ones[i]);
used[i] = false;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
ones[i].first = __builtin_popcount(x);
ones[i].second = ternary_ones(x);
//cout << x << " " << ones[i].first << " " << ones[i].second << "\n";
}
// for(int i = 0; i < n; i++) {
// cout << ones[i].first << " " << ones[i].second;
// }
bool used[n];
fill(used, used + n, false);
for(int i = 0; i < n; i++) {
used[i] = true;
go(1, used, ones[i]);
used[i] = false;
}
cout << cnt;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
199 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
92 ms |
364 KB |
Output is correct |
11 |
Execution timed out |
3079 ms |
364 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |