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. pip和pipenv简记

    一. pip篇 1. 配置文件换源 windows:C:\Users\Administrator\pip\pip.ini mac:~/pip/pip.conf [global] index-url = ...

  2. JZOJ 2022.07.18【提高组A】模拟

    \(\text{T1}\) 很容易想到用 \(f_1 f_2 ... f_k\) 来表示第 \(n\) 项 发现重点关注指数即可,即我们要递推 \(f_1 ... f_k\) 对应的指数 递推涉及 \ ...

  3. 「CEOI2019」魔法树

    \(\text{Solution}\) 设 \(f_{x,i}\) 表示以 \(x\) 为根的子树 \(i\) 天后的最大收益 那么 \(f_{x,i} = \max(f_{x,i-1},w_x [d ...

  4. Git基础操作及协作流程

    一整套流程帮你实践整个 Git 操作基础流程. 来源:https://docs.microsoft.com/zh-cn/learn/paths/intro-to-vc-git/ Git 介绍 配置 G ...

  5. C#泛型接口请求封装类

    using HttpUtil; using Newtonsoft.Json; using System; using System.Collections.Generic; using System. ...

  6. Mars3D入门示例

    1. 引言 Mars3D是基于Cesium的Web端的三维GIS库,对Cesium做了进一步封装和扩展 Mars3D官网:Mars3D三维可视化平台 | 火星科技 Mars3D开发手册:开发教程 - ...

  7. 添加 Echarts bi库

    1.安装库和依赖 npm i echarts --save npm i ngx-echarts --save npm i resize-observer-polyfill --save-dev 2.相 ...

  8. vue学习笔记:组件

    组件是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,说白了就是一组可以重复使用的模板.组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面都可以抽 ...

  9. Mac Parallels Desktop篇 安装Windows 10

    Parallels Desktop安装Windows 10系统: 1.首先下载并安装Parallels Desktop ,下载 Windows 10 系统镜像 2.打开Parallels Deskto ...

  10. mysql 备份定时任务

    #!/bin/bash rq=`date +%Y-%m-%d-%H` #日期 #数据库信息 host=127.0.0.1 user=root password=xxx dbname=script #放 ...