C . Serval and The Formula【Codeforces Round 1011 (Div. 2)】

C.Serval and The Formula

思路:

这场打的纯唐,导致掉大分了,主要就是卡这个题了。

一直在想怎么位运算,但是忽略了最基础的观察和构造方法,div2前三题的思维题一般都是:Attention is all you need.

注意到 x + y = ( x ⊕ y ) + 2 ( x & y ) x + y = (x \oplus y) + 2(x \& y) x+y=(x⊕y)+2(x&y) ,说明想要满足题目要求的形式,就得让 x + k x+k x+k、 y + k y+k y+k的二进制在同一位上不能同时为1

可以证明x==y时是无解的。

又注意到可以把 m a x ( x , y ) max({x,y}) max(x,y) 变成 1000000 这样的形式,那么答案就很简单了:
k = 2 n − m a x ( x , y ) k=2^n-max(x,y) k=2n−max(x,y),n足够大就行。

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define FU(i, a, b) for (int i = (a); i <= (b); ++i)
#define FD(i, a, b) for (int i = (a); i >= (b); --i)
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int zb(int n) { return log2f(n & -n); }
void solve() {
    int x, y;
    cin >> x >> y;
    if(x==y ){cout<<"-1\n";return;}
    if (y > x)
        swap(x, y);
    int k = (1<<(__lg(x)+1))-x;
    cout << k<<endl;
}

signed main() {
    cin.tie(0)->ios::sync_with_stdio(0);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
相关推荐
Dillon Dong8 小时前
【风电控制】TI TMS320F28379D 双CPU架构解析与任务分布设计
嵌入式硬件·算法·变流器·风电控制
NiceCloud喜云13 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
小羊在睡觉13 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
cjhbachelor13 小时前
c++继承
c++
3DVisionary13 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
AI玫瑰助手13 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
好评笔记13 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_4684668513 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
油炸自行车13 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋13 小时前
C++14特性
开发语言·c++·c++14特性