#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
int a[21], binary[21], ternary[21], used[21];
map<int, int> order;
int n, answer;
int ones(int x){
int s = 0;
while(x > 0){
s+= (x%3==1);
x/= 3;
}
return s;
}
void f(int idx, int last){
if(idx == n){
answer++;
return;
}
for(int i = 0;i < n; i++){
if(used[i] == 0){
if(last == -1){
used[i] = 1;
f(idx+1, a[i]);
used[i] = 0;
}else{
if(binary[i] == binary[order[last]]){
used[i] = 1;
f(idx+1, a[i]);
used[i] = 0;
}else if(ternary[i] == ternary[order[last]]){
used[i] = 1;
f(idx+1, a[i]);
used[i] = 0;
}
}
}
}
}
main(){
cin >> n;
for(int i = 0;i < n; i++){
cin >> a[i];
order[a[i]] = i;
binary[i] = __builtin_popcount(a[i]);
ternary[i] = ones(a[i]);
}
f(0, -1);
cout << answer;
return 0;
}
Compilation message
beauty.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
46 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
283 ms |
280 KB |
Output is correct |
9 |
Correct |
2 ms |
212 KB |
Output is correct |
10 |
Correct |
136 ms |
212 KB |
Output is correct |
11 |
Execution timed out |
3058 ms |
212 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |