# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
691827 | iskhakkutbilim | Beautiful row (IZhO12_beauty) | C++14 | 3058 ms | 300 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |