# XOR Sorting HihoCoder - 1509

Posted by stilgar on Wed, 22 Apr 2020 16:38:13 +0200

Think:
1 knowledge point: [base conversion + exclusive or operation]
2 question meaning: input a sequence a[1..n], and judge how many S satisfy the following two conditions
(1):0 <= S < pow(2, 60)
(2) : for I (1 < = I < n), (a [i] XOR s) < = (a [i + 1] XOR s)
3. Solutions:
(1) Firstly, a[i] and a[i+1] are converted to binary, and then the determinable bits are determined by the property that the same is zero and the same is one through exclusive or operation and the condition that (a[i] XOR S) < = (a[i+1] XOR S). Then there are two values (0, 1) for the indeterminate bits, and then the number of S satisfying the condition is obtained

Here is the Accepted code

```#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

int rec, s1, s2;

bool solve(LL x, LL y);
LL power(LL x, int k);

int main(){
int n, i, cnt, flag;
LL a, t;
scanf("%d", &n);
flag = 1;
memset(rec, -1, sizeof(rec));
scanf("%lld", &t);
for(i = 1; i < n; i++){
scanf("%lld", &a);
if(!solve(a, t)) flag = false;
t = a;
}
if(!flag){
printf("0\n");
}
else {
cnt = 0;
for(i = 0; i < 60; i++){
if(rec[i] == -1) cnt++;
}
printf("%lld\n", power(2, cnt));
}
return 0;
}
LL power(LL x, int k){
LL ans = 1;
while(k){
if(k & 1) ans *= x;
x *= x;
k >>= 1;
}
return ans;
}
bool solve(LL x, LL y){
int i;
for(i = 0; i < 60; i++){
if(x & ((LL)1 << i)) s1[i] = 1;
else s1[i] = 0;
if(y & ((LL)1 << i)) s2[i] = 1;
else s2[i] = 0;
}
for(i = 59; i >= 0; i--){
if(s1[i] == 1 && s2[i] == 0){
if(rec[i] == -1 || rec[i] == 1) rec[i] = 1;
else return false;
break;
}
else if(s1[i] == 0 && s2[i] == 1){
if(rec[i] == -1 || rec[i] == 0) rec[i] = 0;
else return false;
break;
}
}
return true;
}
```