萌新求助 WA on test 17
查看原帖
萌新求助 WA on test 17
539529
王君诺弈!悟!楼主2024/9/5 21:27
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,k;
ll getans(ll x){
	if(x>n) return 0;
	if(x==n) return 1;
	ll ans=1;
	for(int i=1;;++i){
		unsigned ll u=(x<<i)+(1<<i)-1;
		//if(x==6) cout<<u<<endl;
		if(u<=n){
			ans+=(1<<i);
		}else{
			u=(x<<i);
			if(n<u) break;
			ans+=(n-u+1);
			break;
		}
	}
	return ans;
}
bool check(ll x){
	if(x==0) return 1;
	if(x==n) return 1>=k;
	ll ans=0;
	if(x%2==0){
		ans+=getans(x+1); 
	}
	ans+=getans(x);
	
	//if(x==3) cout<<ans<<endl;
	return ans>=k;
}
int main(){
	cin>>n>>k;
	ll res=0;
	ll l=0,r=n/2+1,mid;
	while(l+1<r){
		mid=(l+r)/2;
		if(check(2*mid+1)){
			l=mid;
		}else{
			r=mid;
		}
	}
	if(check(2*l+1)){
		res=max(res,2*l+1);
	}
	l=0,r=n/2+1;
	while(l+1<r){
		mid=(l+r)/2;
		if(check(2*mid)){
			l=mid;
		}else{
			r=mid;
		}
	}
	if(check(2*l)){
		res=max(res,2*l);
	}
	cout<<res;
	return 0;
}
2024/9/5 21:27
加载中...