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;
}
相关推荐
rainFFrain1 天前
qt显示类控件---QProgressBar
开发语言·qt
鑫—萍1 天前
C/C++精品算法——双指针(1)
c语言·c++·算法
rainFFrain1 天前
qt输入类控件---QComboBox/QSpinBox
开发语言·qt
铭哥的编程日记1 天前
深入浅出蓝桥杯:算法基础概念与实战应用(三)搜索
算法·蓝桥杯·深度优先
2501_941111891 天前
低延迟系统C++优化
开发语言·c++·算法
未来之窗软件服务1 天前
自建开发工具IDE(二)文件托拽读取——东方仙盟炼气期
开发语言·前端·javascript·仙盟创梦ide·东方仙盟
Hello_WOAIAI1 天前
4.2 python多线程编程:threading 模块深度解析
开发语言·python
2501_941111991 天前
C++中的装饰器模式变体
开发语言·c++·算法
树下水月1 天前
python 连接hive2 数据库
开发语言·数据库·python