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. JZOJ 2022.02.10【提高组】模拟总结

    \(\text{简要题解}\) \(\text{GDOI2012}\) 的题 不得不说当年的题做起来真的很不爽 整体看起来就是数据结构+博弈论+宽搜+背包dp优化 考场上 \(T1\) 十分钟解决过了 ...

  2. 【SDOI2015】星际战争

    #include<cstdio> #include<queue> using namespace std; const int M = 10000; const double ...

  3. Vue中v-model与:value的区别

    v-model不可以加其他值 <input type="text" v-model="curAmount"> :value可以加 单位 <in ...

  4. Spring AOP 报错:Error creating bean with name 'student' defined in file

    问题概述 Spring AOP 报错,一直显示:Error creating bean with name 'student' defined in file 的报错.从五个方向排查:第一,aspec ...

  5. Epicor 助力F1车队Scuderia AlphaTauri 提升车队运营效率和性能

    Scuderia AlphaTauri 很高兴地宣布,Epicor 是促进业务增长的行业特定企业软件的全球领导者,已被任命为车队的官方 ERP 合作伙伴.这项多年期协议建立在已经成功的长期技术合作关系 ...

  6. Decompiling XAPK Files

    http://calebfenton.github.io/2016/02/28/decompiling-xapk-files/

  7. 基于GPU 显卡在k8s 集群上实现hpa 功能

    前言 Kubernetes 支持HPA模块进行容器伸缩,默认支持CPU和内存等指标.原生的HPA基于Heapster,不支持GPU指标的伸缩,但是支持通过CustomMetrics的方式进行HPA指标 ...

  8. Django,Flask中的request

    request的结构获取 class Upload(Resource): def post(self): print(curPath) print(request.files['file'].__di ...

  9. VUE学习-自定义指令

    自定义指令 有的情况下,你仍然需要对普通 DOM 元素进行底层操作,这时候就会用到自定义指令. <div id="directive-demo"> <input ...

  10. vue element 日期范围选择器限制:只能选今天之前的时间 || 只能选今天之后的时间 || 选取今天往后三天内

    举例:只能选今天或者今天之后的时间(如下图) <el-date-picker clearable v-model="form.limitTime" type="da ...