A15602 | Interesting Sequence
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Petya and his friend, robot Petya++, like to solve exciting math problems.
One day Petya++ came up with the numbers $n$ and $x$ and wrote the following equality on the board: $$$$n\ \&\ (n+1)\ \&\ \dots\ \&\ m = x, $$ where $\\&$ denotes the <a href="https://en.wikipedia.org/wiki/Bitwise_operation#AND">bitwise AND operation</a>. Then he suggested his friend Petya find such a minimal $m$ ( $m \\ge 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?

One day Petya++ came up with the numbers $n$ and $x$ and wrote the following equality on the board: $$$$n\ \&\ (n+1)\ \&\ \dots\ \&\ m = x, $$ where $\\&$ denotes the <a href="https://en.wikipedia.org/wiki/Bitwise_operation#AND">bitwise AND operation</a>. Then he suggested his friend Petya find such a minimal $m$ ( $m \\ge 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?

输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 2000$ ). The description of the test cases follows.
The only line of each test case contains two integers $n$ , $x$ ( $0\le n, x \le 10^{18}$ ).
The only line of each test case contains two integers $n$ , $x$ ( $0\le n, x \le 10^{18}$ ).
输出格式
For every test case, output the smallest possible value of $m$ 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 \cdot 10^{18}$ .
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 \cdot 10^{18}$ .
输入输出样例
输入 #1
5 10 8 10 10 10 42 20 16 1000000000000000000 0
输出 #1
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted