专栏文章
题解:CF2085C Serval and The Formula
CF2085C题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @miptgv93
- 此快照首次捕获于
- 2025/12/03 17:40 3 个月前
- 此快照最后确认于
- 2025/12/03 17:40 3 个月前
简要说明题意:
已知 ,求 使得 ,如果不存在输出 。
题目分析:
众所周知 ,那么只需要找到 满足 ,就能让条件成立。也就是说, 和 的任意二进制位不能同时为1。所以输出 的条件就是 。
考虑到 可以大于 ,那么直接从低位开始扫,两数的某位如果同时为 ,就把大的那个加进位。这个过程可以迭代,可以解决问题。
代码如下:
CPP#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int T;
long long x,y,t,temp,cnt,k;
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&x,&y),k=0ll,t=(x&y);
if(x==y){
printf("-1\n");
continue;
}
while(t){
temp=(t&(-t)),temp=(temp<<1)-1;
cnt=min(((x&temp)^temp)+1,((y&temp)^temp)+1);//计算要加到进位差多少,可能有更简单的表达方式?
k+=cnt,x+=cnt,y+=cnt;
t=(x&y);
}
printf("%lld\n",k);
}
return 0;
}
/*
1
3 7
*/
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...