PIPIOJ 1436: PIPI's bit operation problem Ⅰ bit operation

http://pipioj.online/problem.php?id=1436
Insert picture description here
Idea: Combine L, RL, RL and R are converted into binary representations, and enumerate from the high bit. Obviously, there are four cases for each bit (in fact, there are only three cases, because R>=L), whenR i = 1 R_i=1Ri=1 andL i = 0 L_i=0Li=At 0 , we can take allR j R_jRjSet to 0 00L j L_jLjSet to 1 11 (j>i), this is the optimal solution.

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;

using ll=long long;

ll L,R;

int main()
{
    
    
    scanf("%lld%lld",&L,&R);
    ll ans=1ll<<60;
    while(ans)
    {
    
    
        if((R&ans)&&!(L&ans))
        {
    
    
            ans=(ans<<1)-1;
            break;
        }
        ans>>=1;
    }
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin blog.csdn.net/xiji333/article/details/114459104