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;
}
相关推荐
SilentSamsara41 分钟前
生成器完全指南:`yield` 与惰性求值的工程价值
linux·开发语言·python·算法·机器学习·青少年编程
玛卡巴卡ldf1 小时前
【LeetCode 手撕算法】(二分查找)搜索插入位置、搜索二维矩阵、查找数组相同的所有位置、搜索旋转排序数组、旋转升序数组的最小值
数据结构·算法·leetcode
谷雨不太卷8 小时前
进程的状态码
java·前端·算法
jieyucx8 小时前
Go语言深度解剖:Map扩容机制全解析(增量扩容+等量扩容+渐进式迁移)
开发语言·后端·golang·map·扩容策略
顾温8 小时前
default——C#/C++
java·c++·c#
凉茶钱8 小时前
【c语言】动态内存管理:malloc,calloc,realloc,柔性数组
c语言·c++·vscode·柔性数组
脏脏a8 小时前
【C++模版】泛型编程:代码复用的终极利器
开发语言·c++·c++模版
island13148 小时前
【C++仿Muduo库#3】Server 服务器模块实现上
服务器·开发语言·c++
散峰而望8 小时前
【算法竞赛】C/C++ 的输入输出你真的玩会了吗?
c语言·开发语言·数据结构·c++·算法·github
小龙报8 小时前
【C语言】内存里的 “数字变形记”:整数三码、大小端与浮点数存储真相
c语言·开发语言·c++·创业创新·学习方法·visual studio