https://codeforces.com/gym/102783/problem/F
题意:给定两个整数A,B(在long long 范围内) 求A异或1到B任意整数的最大值。
解题思路:从1一直异或到B,显然会超时。既然是异或运算,我们首先将A B都转换为二进制。结果尽量大就是说,二进制的1尽量多,尽量大。就转换为贪心问题。注意B的最大值选择合适的贪心策略就可以了。
ac代码:
#include<bits/stdc++.h>
const int N = 100;
typedef long long ll;
using namespace std;
bool a[N];
bool b[N];
int main()
{
ll x,y;
int n1=0;
int n2=0;
cin>>x>>y;
while(x)
{
a[n1++]=x%2;
x/=2;
}
while(y)
{
b[n2++]=y%2;
y/=2;
}
bool flag=0;
for(int i=n2-1;i>=0;i--)
{
if(a[i]==1&&b[i]==1)
flag=1;
if(a[i]==0)
{
if(b[i]==1)
a[i]=1;
if(b[i]==0&&flag==1)
{
a[i]=1;
}
}
}
int k=max(n1,n2);
ll t=1;
ll sum=0;
for(int i=0;i<k;i++)
{
sum+=a[i]*t;
t*=2;
}
cout<<sum<<endl;
}