C. Interesting Sequence

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya and his friend, robot Petya++, like to solve exciting math problems.One day Petya++ came up with the numbers nn and xx and wrote the following equality on the
board:
 
n & (n+1) & … & m=x,n & (n+1) & … & m=x
 
where && denotes the bitwise AND operation. Then he suggested his friend Petya find such a minimal mm (m≥nm≥n) that the equality on the board holds. Unfortunately, Petya couldn't solve this problem in his head and decided to ask for computer help. He quickly wrote a program and found the answer.Can you solve this difficult problem?
Input

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).

Output

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.

 
Example
input
5
10 8
10 10
10 42
20 16
1000000000000000000 0
output

12
10
-1
24
1152921504606846976

Note

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) 取交集。

最后,如果可行域不为空,输出最小元素。时间复杂度是 Θ(log⁡max(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的更多相关文章

  1. 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 ...

  2. Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation

    还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门  Problem - D - Codeforces 题意 n个数字,n ...

  3. Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读

    http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力

    Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. react的react-devtools 工具

    步骤1: 访问react-devtools工具网址: https://gitcode.net/mirrors/facebook/react-devtools?utm_source=csdn_githu ...

  2. qt 运行环境配置

    注意事项: 1 在设备上进行如下配置 root@am335x-pico:/opt# export QTDIR=/opt/qt-4.6.2-arm root@am335x-pico:/opt# expo ...

  3. Python学习:画K帮

    import datetime import pandas_datareader.data as web df_stockload = web.DataReader("600797.SS&q ...

  4. Android组件化开发-----页面路由(ARouter)

    平时开发中,我们经常用到页面跳转功能.之前我一直使用Intent过跳转 Intent intent = new Intent(A.this, B.class); intent.putExtra(&qu ...

  5. pull request 猜想

    先从某个地方 fork 一个项目, 我上传一个 git commit, 然后自动显示是否要 pull request, 点 是, 然后就发送到 charger 那里去了.1, git fork,2, ...

  6. RSTP-快速生成树协议

    1 STP的不足之处STP协议虽然能够解决环路问题,但是由于网络拓扑收敛慢,影响了用户通信质量. 2 RSTP概述RSTP在许多方面对STP进行了优化,它的收敛速度更快,而且能够兼容STP. 通过接口 ...

  7. vue-awesome-swiper使用中的一些问题

    项目中使用了vue-awesome-swiper,发现autoplay不能用.网上找了半天,说是版本问题.最后在main.js中添加以下代码解决. import VueAwesomeSwiper fr ...

  8. R7-1 求10个点到原点的距离和

    R7-1 求10个点到原点的距离和 分数 15 全屏浏览题目 切换布局 作者 张高燕 单位 浙大城市学院 求10个点到原点的距离和.输入10个点的坐标,计算并输出这些点到原点的距离和.定义函数dist ...

  9. CSS3选择器nth-child(n)

    CSS3选择器nth-child(n)实现隔几行选择元素 nth-child(n),n 可以是数字.关键词或公式.选择器匹配属于其父元素的第N个子元素,不论元素的类型. 序号写法:li:nth-chi ...

  10. nginx auth_basic uwsgi 目录简易认证。

    mkdir /etc/nginx/conf.d/auth_pwd touch /etc/nginx/conf.d/auth_pwd/xx.pwd htpasswd -c -d /etc/nginx/c ...