https://codeforces.com/contest/1775/problem/C

题意

题意是说,给你n和x,你要求出最小的满足要求的m,使得 \(n\)&\((n+1)\)&\((n+2)\)&\(...\)&\(m=x\)

若没有满足的输出-1

思路

容易知道x的范围\(0\leq x \leq n\)

若\(x\)为0,答案是 \((1 << n的最高位的bit + 1)\)

若\(x\)为n,答案就是n

否则,先将\(x\)转化成二进制,\(x\)中至少有一个1,对于一个bit来说 n 0 x 1是不可能发生的,这一位x是1 只可能n取1,这个时候恰好满足“\(x\)最右边的1及之前的部分和\(n\)中对应的部分相同”,若这个条件不满足,输出-1

再来考虑这个位置之后的序列,x中的数肯定都是0了,假设n这一段数中,最高位上的数是1,此时x对应位上的数是0,可以知道此时肯定发生了进位,但是n和x的前缀又是完全相同的,故这种情况也不合法,输出-1

那么在这一段数中,\(n\)中就算有1,也不是紧邻着\(x\)中最右边的\(1\)的,我们只需要找到\(n\)中最低位的\(1\),设这个位置为\(i\),答案就是 \(((n >> i + 1) | 1) << i + 1\)

code

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
int T;
ll n, x; int main() {
cin >> T;
while(T--) {
cin >> n >> x;
if(x > n) {
puts("-1");
continue;
}
if(x == n) {
printf("%lld\n", n);
continue;
}
if(x == 0) {
for(int i = 60; i >= 0; i--) {
if(n >> i & 1) {
printf("%lld\n", (1LL << (i + 1)));
break;
}
}
continue;
}
int pos = -1;
for(int i = 0; i <= 60; i++) {
if(x >> i & 1) {
pos = i;
break;
}
}
// [st...pos] , [pos + 1, ... , en]
bool f = 1;
for(int i = 60; i >= pos; i--) {
if((x >> i & 1) != (n >> i & 1)) {
f = 0;
break;
}
}
if(pos && (n >> (pos - 1) & 1)) f = 0;
if(!f) {
puts("-1");
continue;
}
ll ans = 0;
for(int i = pos - 1; i >= 0; i--) {
if(n >> i & 1) {
ans = ((n >> i + 1) | 1) << i + 1;
break;
}
}
printf("%lld\n", ans);
}
return 0;
}

Codeforces Round #843 (Div. 2) C【思维】的更多相关文章

  1. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  2. Codeforces Round #143 (Div. 2) (ABCD 思维场)

    题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...

  3. Codeforces Round #395 (Div. 2)(A.思维,B,水)

    A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...

  4. Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)

    A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  5. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  6. Codeforces Round #539 (Div. 2) D 思维

    https://codeforces.com/contest/1113/problem/D 题意 将一个回文串切成一段一段,重新拼接,组成一个新的回文串,问最少切几刀 题解 首先无论奇偶串,最多只会切 ...

  7. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  8. Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化

    D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  9. Codeforces Round #619 (Div. 2)E思维+二维RMQ

    题:https://codeforces.com/contest/1301/problem/E 题意:给个n*m的图形,q个询问,每次询问问询问区间最大的合法logo的面积是多少 分析:由于logo是 ...

  10. Codeforces Round #700 (Div. 2) --- B(思维)

    传送门 有必要提醒自己一下, 先把题读利索了(手动捂脸) 题目 B. The Great Hero   The great hero guards the country where Homer li ...

随机推荐

  1. CF1781D 解题乱弹

    abc1057510554 老师说,搞这种数论题,就可以在 CF 上 number theory 板刷一个 1300-1900 就可以了. 然后发现连 1800 的题都做不出来,我可以退役力 QAQ ...

  2. php .inc 文件

    inc 文件顾名思义是include file的意思.即PHP的包含文件,这里用后缀来表示文件的作用, inc文件一般加载一些设置 举个例子   <? php//这里是数据库连接的配置信息db. ...

  3. Bypass disable_functions 食用方法

    Bypass disable_functions 食用方法 目录 Bypass disable_functions 食用方法 1 上传Payload 2 直接使用sh反弹shell 3 上传 Payl ...

  4. 代码随想录算法训练营day14 | leetcode 层序遍历 226.翻转二叉树 101.对称二叉树 2

    层序遍历 /** * 二叉树的层序遍历 */ class QueueTraverse { /** * 存放一层一层的数据 */ public List<List<Integer>&g ...

  5. typescript - 学习档案

    由于内容繁多,使用掘金来记录此笔记,方便索引跟随!未完待续~~~ 地址如下: https://juejin.cn/post/6899350420541014030/#heading-20

  6. Visual Studio 2022 不支持 .NET Framework 老版本 项目解决办法

    Visual Studio 2022 不支持 .NET Framework老版本 (4.5) 项目解决办法 新电脑安装的是Visual Studio 2022,打开老项目的时候发现没有.net fra ...

  7. 1.1 创建一个WCF应用程序服务

    第一步:引入System.ServiceModel.dll 第二步 定义一个WCF接口: //定义接口 [ServiceContract] interface IGetInfo { [Operatio ...

  8. vue3 打开页面input框自动获得焦点

    1.需要聚焦的el-input输入框设置ref值: ref="getfcous" <el-input v-model="workorder" ref=&q ...

  9. frp使用教程

    内网穿透工具---frp使用教程 https://blog.csdn.net/u011215939/article/details/103383373

  10. Appium-TouchAction类与MultiAction类(控件元素的滑动、拖动,九宫格解锁,手势操作等)

    文章转自:https://www.cnblogs.com/lfr0123/p/13679568.html swipe一般用于对页面进行上下左右滑动操作,但自动化过程中还会遇到其他情况,如对控件元素进行 ...