Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
761 B
1
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

define ll long long

ll countBits(ll n){
	n++;
	ll bitIdx = 0, ans = 0, one = 1;
	while((one << bitIdx) <= n){
		ll rem = (n % (one << (bitIdx + 1)));
		ans += (n / (one << (bitIdx + 1))) * (one << bitIdx);
		if(rem > (one << bitIdx))	ans += rem - (one << bitIdx);
		bitIdx++;
	}
	return ans;
}
int main(){
	init();
	ll t;	cin>>t;
	assert(t > 0 && t <= 20);
	while(t--){
		ll l, k;	cin>>l>>k;
		assert(l > 0 && l <= 1e15);
		l--;
		assert(countBits(2e15) - countBits(l) >= k);
		ll lo = l, hi = 2e15, ans = 2e15;
		while(lo <= hi){
			ll m = (lo + hi) / 2;
			if(countBits(m) >= countBits(l) + k){
				ans = m;
				hi = m - 1;
			}
			else	lo = m + 1;
		}
		cout<<ans<<endl;
	}
}