题解:CF2108B SUMdamental Decomposition
wangboyue_740325 · · 题解
思路
为了方便理解,接下来使用表格的形式表示二进制下的
||1|2|3| |:-:|:-:|:-:|:-:| |(4)||1|1| |(2)||1|| |(1)|1||1|
表示
明显,当 2 27 为例:
||1|2| |:-:|:-:|:-:| |(16)||1| |(8)||1| |(4)||| |(2)|1|| |(1)|1||
可以任意左右移动二进制
当 3 6 为例:
||1|2|3| |:-:|:-:|:-:|:-:| |(8)|||| |(4)|||| |(2)||1|| |(1)|1|1|1|
即使我们尽可能拆散
当 2 1 为例:
||1|2| |:-:|:-:|:-:| |(2)|1|1| |(1)|1||
此时在最后一对数字中,二进制位
当
代码
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int help(){
int n,x;
cin>>n>>x;
int b=__builtin_popcountll(x);
if(n<=b){
return x;
}
else if(x==1){
if(n%2==1){
return n;
}
else{
return n+3;
}
}
else if(x==0){
if(n==1){
return -1;
}
else if(n%2==0){
return n;
}
else if(n%2==1){
return n+3;
}
}
int ne=n-b;
if(ne%2==1){
ne++;
}
return x+ne;
}
signed main(){
int t;
cin>>t;
while(t--){
cout<<help()<<endl;
}
return 0;
}