Codeforces Round #843 (Div. 2) Problem C
C. Interesting Sequence
time limit per test
1 second
256 megabytes
input
standard input
standard output

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤2000). The description of the test cases follows.The only line of each test case contains two integers n, x (0≤n,x≤10^18).
For every test case, output the smallest possible value of mm such that equality holds.If the equality does not hold for any m, print −1 instead.We can show that if the required m exists, it does not exceed 5⋅10^18.
12
10
-1
24
1152921504606846976
In the first example, 10 & 11=10, but 10 & 11 & 12=8, so the answer is 12.
In the second example, 10=10, so the answer is 10.
In the third example, we can see that the required m does not exist, so we have to print −1.
思路:
我们可以
按位考虑。如果
- n 在这一位上是 0 , x 在这一位上是 0
- 选取任何的 m 都可行。
- n 在这一位上是 0 , x 在这一位上是 1
- 不可能实现。
- n 在这一位上是 1 , x 在这一位上是 0
- 必须等到某一个在这一位为 0 的数出现,才能满足要求。
- 设这个数最小为 k ,则可行域与 [k,+∞] 取交集。
- n 在这一位上是 1 , x 在这一位上是 1
- m 必须在某一个在这一位为 0 的数出现之前,才能满足要求。
- 设这个数最小为 k ,则可行域与 [n,k) 取交集。
最后,如果可行域不为空,输出最小元素。时间复杂度是 Θ(logmax(n,x))
代码:
1 #include<bits/stdc++.h>
2 #define N 70
3 using namespace std;
4 typedef long long ll;
5
6 void solve()
7 {
8 ll n,x;
9 scanf("%lld%lld",&n,&x);
10 bitset<64> bn(n),bx(x);
11 ll l=n,r=5e18;
12 for(int i=63;i>=0;i--)
13 {
14 if(bn[i]==0 && bx[i]==1)
15 {
16 puts("-1");
17 return;
18 }
19 if(bn[i]==0 && bx[i]==0) continue;
20 if(bn[i]==1 && bx[i]==0)
21 {
22 l=max(l,((n/(1ll<<i))+1)*(1ll<<i));
23 //二进制 1010 * 10 = 10100
24 //一个数乘 100...00 相当于左移相应的位数
25 //一个数整除 100...00 相当于把这个1右边的所有位数变成0
26 }
27 else{
28 r=min(r,((n/(1ll<<i))+1)*(1ll<<i)-1);
29 }
30 }
31
32 if(l<=r) printf("%lld\n",l);
33 else puts("-1");
34
35 return ;
36 }
37
38 int main()
39 {
40 int _;
41 cin>>_;
42 while(_--) solve();
43 return 0;
44 }
Noted by DanRan02
2023.1.11
Codeforces Round #843 (Div. 2) Problem C的更多相关文章
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation
还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门 Problem - D - Codeforces 题意 n个数字,n ...
- Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读
http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学
— This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...
- Codeforces Round #439 (Div. 2) Problem B (Codeforces 869B)
Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...
- Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...
- Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...
- Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinat ...
- Codeforces Round #427 (Div. 2) Problem A Key races (Codeforces 835 A)
Two boys decided to compete in text typing on the site "Key races". During the competition ...
随机推荐
- API对象--Service(chrono《kubernetes入门实战课》笔记整理)
[概念解说] 当pod被实例化出来,如果运行 一段时间会销毁,虽然deployment和ds会注意管理和维护pod的数目,但是pod销毁后再重建,ip会发生变化,这对于服务来说,是很麻烦的.所以需要有 ...
- D3简介
(一)D3简介 一.D3是什么 一张图片价值相当于一千个字 D3的全称是 Data-Driven Documents,直译为:数据驱动的文档 D3是一个javaScript的函数库,是用来做数据可视化 ...
- 看到项目中的DateTimeFormat和JsonFormat就头大
刚来这家公司的时候, 发现很多同事还在用这种方式写代码 当时以为是偶然, 刚才在群里发现还有好多人在交流应当加哪些注解, 声明时区问题. 当写一个东西感到麻烦的时候, 那么大概率是有低成本的更优解的 ...
- centos NTP时间同步
1.先设置时区 timedatectl set-timezone Asia/Shanghai 2安装ntp服务 yum install chrony 3.修改ntp配置文件的ntp服务器 vi /et ...
- AutoCAD专用卸载工具,完美彻底卸载清除干净AutoCAD各种残留注册表和文件。
AutoCAD专用卸载工具,完全彻底卸载删除干净AutoCAD各种残留注册表和文件的方法和步骤.如何卸载AutoCAD呢?有很多同学想把AutoCAD卸载后重新安装,但是发现AutoCAD安装到一半就 ...
- clickhouse杂记
1,clickhouse show tables SHOW [TEMPORARY] TABLES [FROM ] [LIKE ''] [LIMIT ] [INTO OUTFILE ] [FORMAT ...
- springBoot中对mongodb添加2dsphere位置索引
项目需求:最近有个需求,就是要根据坐标位置找出附近的车辆(车辆有对应的坐标).然后翻了翻百度,cv流一顿操作之后,大概整理出来了一段代码如下 //根据当前位置坐标,找出附近*米内的所有车辆BasicD ...
- python实现自动打卡
自己也百度了一下,然后写的,分为了三个部分,见三段代码 代码:主程序代码 import timefrom selenium import webdriverfrom private_info impo ...
- FIFO 串口接收处理机制
与安富莱电子的串口处理机制做对比交互 参考链接: https://www.eet-china.com/mp/a161019.html
- 用find和xargs处理文件名中带空格的文件
我们经常用find和xargs来进行批处理文件. 常见的用法如:find ./ -name ".jpg" | xargs cp -t ./JPEGImages ,实现将当前目前下所 ...