C. Interesting Sequence

By xiaruize

思路

手模一下样例一

10=(1010)210 = (1010)_2 , 发现需要将 212^1 位的 11 去掉

此时,就需要与一个 212^1 位为 00 的数, 同时其它位不能使 1010 改变, 且这个数 10\geq 10

那么, 这个数的最小值显然为 (1100)2=12(1100)_2=12

总结上面的过程, 得到以下这些结论

  1. n&x=xn \And x=x , 否则是 -1

  2. 考虑 nxn - x 的值的二进制表示 (即 nn 中需要改为 00 的二进制位的值), 发现所有的 11 必须是 nn 中最低且连续的, 否则 -1

  3. nxn-x 中最高位的 112k2^k 位上, 那么 2k+12^{k+1} 位在 nn 中一定为 00 , 否则 -1

  4. 否则答案为 2k+1n+x2^{k+1}-n+x

samples

  1. n=11,m=4n=11,m=4
  2. n=20,m=16n=20,m=16
  3. n=11,m=9n=11,m=9

如果不能理解结论,建议手模一遍上述样例

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n, x;
int t;
// bool en;

void solve()
{
cin >> n >> x;
if (x > n)
{
cout << "-1" << endl;
return;
}
if (x == n)
{
cout << n << endl;
return;
}
int p = n - x;
int d = p;
for (int i = 0; i <= 61; i++)
{
if (p < 0)
{
cout << "-1" << endl;
return;
}
if (p == 0 && (n & (1ll << i)))
{
cout << "-1" << endl;
return;
}
if (p == 0)
{
cout << (1ll << i) - d + n << endl;
return;
}
if (n & (1ll << i))
{
p -= (1ll << i);
}
}
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> t;
while (t--)
solve();
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}