HihoCoder - 1509
Given a sequence a[1..n], you need to calculate how many integers S satisfy the following conditions:
(1). 0 ≤ S < 2^60
(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] < 2^60
Output
Output one integer : the answer
题意
对于一个给定的数组,a[i] xor S <= a[i+1] xor S,输出满足条件的S的个数。
思路
对于两个数,假设前面的二进制相同,遇到的第一位不同,1/0,那么S在这一位上的二进制数是固定的,同理我们可以固定其他的以为位置,S一共60位,则S的可能取值的数目就是 2^(60-固定数目)。
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll a[100];
int b1[100], b2[100];
int v[100];
ll ans;
void dl(ll x, ll y) {
int i, j;
for (int i = 1; i <= 60; i++) {
b1[i] = x % 2;
x = x / 2;
}
for (int j = 1; j <= 60; j++) {
b2[j] = y % 2;
y = y / 2;
}
ans = 0;
for (int i = 60; i >= 1; i--) {
if (b1[i] == b2[i])
continue;
ans = i;
break;
}
v[ans] = 1;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n - 1; i++)
dl(a[i], a[i + 1]);
ans = 0;
for (int i = 60; i >= 1; i--)
{
if (v[i])continue;
ans++;
}
ans = (ll)1 << ans;
cout << ans << endl;
}