-
Notifications
You must be signed in to change notification settings - Fork 0
/
And Plus Or.cpp
78 lines (54 loc) · 1.7 KB
/
And Plus Or.cpp
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
Task: Given an integer x, find two non-negative integers a and b such that (a∧b)+(a∨b)=x, where ∧ is the bitwise AND operation and ∨
is the bitwise OR operation.
Input
The first line of the input contains a single integer T
denoting the number of test cases. The description of T
test cases follows.
The first and only line of each test case contains a single integer x
.
Output
If there is no valid pair (a,b)
, print a single line containing the integer −1. Otherwise, print a single line containing two space-separated integers a and b
.
If there are multiple solutions, you may print any one of them.
Constraints
1≤T≤105
1≤x≤1018
Subtasks
Subtask #1 (30 points):
1≤T≤200
1≤x≤200
Subtask #2 (70 points): original constraints
Example Input
2
1
8
Example Output
0 1
5 3
Solution:
#include <iostream>
using namespace std;
int main() {
long long int t;
cin>>t;
while(t--){
long long int x,a=0,b=0;
cin>>x;
if(x%2!=0){
a = (x/2) + 1;
b = x/2;
}
else{
a = x/2;
b = x/2;
}
if(((a&b) + (a|b) )== x){
cout<<a<<" "<<b<<endl;
}
else{
cout<<-1<<endl;
}
}
return 0;
}