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;
}
相关推荐
moonsea02039 分钟前
【无标题】
算法
春日见11 分钟前
E2E自驾规控30讲:导论
开发语言·驱动开发·git·matlab·计算机外设
wangchunting12 分钟前
Jvm-垃圾收集器
java·开发语言·jvm
沐知全栈开发23 分钟前
PHP Math: 精通PHP中的数学函数与应用
开发语言
佑白雪乐30 分钟前
<ACM进度212题>[2026-3-1,2026-3-26]
算法·leetcode
穿条秋裤到处跑33 分钟前
每日一道leetcode(2026.03.26):等和矩阵分割 II
算法·leetcode·矩阵
吴声子夜歌35 分钟前
JavaScript——call()、apply()和bind()
开发语言·前端·javascript
平凡灵感码头38 分钟前
C语言 printf 数据打印格式速查表
c语言·开发语言·算法
哔哔龙43 分钟前
Android OpenCV 实战:图片轮廓提取与重叠轮廓合并处理
android·算法
hz_zhangrl1 小时前
CCF-GESP 等级考试 2026年3月认证C++三级真题解析
c++·算法·程序设计·gesp·gesp2026年3月·gesp c++三级