You have two integers L and R, and you are required to find the max xor value of a and b where L <= a <= R and L <= b <= R

Input

 

Two integers in a line. L, R <= 1e9

 

Output

One integer, the answer

Example

Input:
1 10

Output:
15

题意:

给定L,R,X1^X2^X3...最大异或,(L<=X1,X2,X3...<=R)。

 没什么思路,上次CF就遇到这道题,我是用贪心写的,忽略pow的精度问题,可以AC。

http://codeforces.com/contest/912/problem/B

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
long long a,b,n,k,ans,c,d;
map<long long,int>mp;
vector<long long>S;
int main()
{
    while(~scanf("%I64d%I64d",&n,&k)){
         ans=0;
         for(long long  i=log2(n);i>=0;i--){
                long long tmp=pow(2,i);
                if(k>0){
                    k--;    
                    mp[tmp]=1;
                    S.push_back(tmp);
                    ans+=tmp;
                }
                else{
                    int L=S.size();
                    for(int j=0;j<L;j++){
                        if(mp[S[j]]==1&&S[j]+tmp<=n&&mp[S[j]+tmp]==0) {
                            mp[S[j]]=0;
                            mp[S[j]+tmp]=1;
                            ans+=tmp;
                            S.push_back(S[j]+tmp);
                            break;
                        }
                    }
                }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
View Code

相关文章:

  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2021-11-23
  • 2021-08-20
  • 2021-05-20
  • 2021-06-14
猜你喜欢
  • 2021-12-05
  • 2021-10-01
  • 2021-09-01
  • 2021-10-03
  • 2021-07-10
  • 2022-01-19
  • 2021-09-22
相关资源
相似解决方案