XOR Sorting 题解

题目描述:
Given a sequence a[1..n], you need to calculate how many integers S satisfy the following conditions:

(1). 0 ≤ S < 260

(2). For every i in [1,n-1] , (a[i] xor S) ≤ (a[i+1] xor S)

Input
On the first line there is only one integer n

On the second line there are n integers a[1..n]

1 ≤ n ≤ 50

0 ≤ a[i] < 260

Output
Output one integer : the answer


Sample Input
3
1 2 3
Sample Output
288230376151711744


题解:将ai与a(i+1)的十进制转换为二进制,然后比较从某个位置开始的不同,如果某个位置不同,则标记此位置。设此位置为x,则x以前的所有位数都不想关。
特此感谢任大大。


AC代码:

include

include

include

using namespace std;
long long a[60];
int aa[60];
int bb[60];
int visit[60]={0};
typedef long long ll;
int main()
{
int cnt = 0;
int n;
cin >> n;
for(int i=0;i {
cin >> a[i];
}
for(int i=0;i {
ll ax = a[i];
ll bx = a[i+1];
for(int j=0;j<60;j++)
{
aa[j] = ax%2;
ax/=2;
}
for(int j=0;j<60;j++)
{
bb[j] = bx%2;
bx /= 2;
}
for(int j=59;j>=0;j--)
{
if(aa[j]!=bb[j])
{
visit[j] = 1;
break;
}
}
}
for(int i=59;i>=0;i--)
{
if(visit[i]==1)
cnt++;
}
long long ans = (1ll<<(60-cnt));
printf("%lld",ans);
return 0;
}

你可能感兴趣的:(XOR Sorting 题解)