跳到正文

个人记录

3

ljl241105约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年9月11日
收录
2026年9月11日
更新
2026年9月11日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,n,k,ans;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
signed main(){
	T=read();
	while(T--){
		n=read(),k=read(),ans=0;
		while(k>0&&n>1){
			int L=0;
			while((1LL<<(L+1))<=n)L++;
			int p2=1LL<<L;
			int M=(p2<<1)-1;
			int low=max(1LL,M-n);
			int high=min(n,p2-1);
			int P=0;
			if(low<=high)P=high-low+1;
			if(k<=P){
				ans+=2*k*M;
				k=0;
			}else{
				ans+=2*P*M;
				k-=P;
				n=M-n-1;
				if(n<=0)break;
			}
		}
		cout<<ans<<'\n';
	}
	return 0;
}
原文