Reference: codeforces Round # 608 (. Div's 2) E - the Common Number The (binary tree structure thought)
Specific practices may see reference blog.
After the key is divided parity, is the orderly
In a large range of data when you are trying to reduce the complexity of the answers dichotomy is a way to reduce the complexity, the complexity is reduced to O level (log (n)), so that even if the data is also 1e18 able to finish.
Code:
// Created by CAD on 2020/1/2.
#include <bits/stdc++.h>
#define fi first
#define se second
#define pll pair<long long,long long>
#define ll long long
using namespace std;
ll n,k;
bool judge(ll x)
{
queue<pll> q;
ll cnt=0;
if(x&1) q.push({x,x});
else q.push({x,x+1});
while(q.size())
{
auto i=q.front(); q.pop();
cnt+=min(n,i.se)-i.fi+1;
if((i.fi<<1)<=n) q.push({i.fi<<1,i.se<<1|1});
}
return cnt>=k;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
ll ans=-1;
//遍历偶数
ll l=1,r=n/2;
while(l<=r)
{
ll mid=(l+r)>>1;
if(judge(mid<<1)) ans=max(ans,mid<<1),l=mid+1;
else r=mid-1;
}
//遍历奇数
l=0,r=n/2;
while(l<=r)
{
ll mid=(l+r)>>1;
if(judge(mid<<1|1)) ans=max(ans,mid<<1|1),l=mid+1;
else r=mid-1;
}
cout<<ans<<endl;
return 0;
}