#include <bits/stdc++.h> using namespace std; #define loop(i, a, b) for (ll i = (a); i <…
- User
#include <bits/stdc++.h> using namespace std;
#define loop(i, a, b) for (ll i = (a); i < (b); i++) #define rloop(i, a, b) for (ll i = (b); i > (a); i--) #define sz(x) ((ll)x.size()) #define trav(a, x) for (auto& a : x) #define all(x) x.begin(), x.end() #define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower) #define upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper) #define pb push_back #define mp make_pair #define vi vector
typedef long long ll; typedef long double ld;
const ll MOD = 1e9 + 7; const ll INF = 1e18;
void solve() {
ll b,c,d; cin >> b >> c >> d; ll a = 0; for (ll i = 0; i < 64; i++) { ll x = (b >> i) & 1; ll y = (c >> i) & 1; ll z = (d >> i) & 1; if (z) { if (!x) { a |= (1 << i); } } else { if (y) { continue; } } } if ((a|b) - (a&c) == d) { cout << a << '\n'; } else { cout << -1 << '\n'; }}
int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t; cin >> t; while (t--) { solve(); } return 0;} C. Bitwise Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output
You are given three non-negative integers b, c, and d. Please find a non-negative integer a∈[0,261] such that (a|b)−(a&c)=d, where | and & denote the bitwise OR operation and the bitwise AND operation, respectively. If such an a exists, print its value. If there is no solution, print a single integer −1. If there are multiple solutions, print any of them.
Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105). The description of the test cases follows. The only line of each test case contains three positive integers b, c, and d (0≤b,c,d≤1018).
Output For each test case, output the value of a, or −1 if there is no solution. Please note that a must be non-negative and cannot exceed 261.
Example Input 3 2 2 2 4 2 6 10 2 14
Output 0 -1 12
Note In the first test case, (0|2)−(0&2)=2−0=2. So, a=0 is a correct answer. In the second test case, no value of a satisfies the equation. In the third test case, (12|10)−(12&2)=14−0=14. So, a=12 is a correct answer.
- Assistant
The problem requires us to find a non-negative integer ( a ) such that:
[ (a ,|, b) - (a ,&, c) = d ]
Here's an effective way to approach this problem:
-
Bitwise Analysis:
We need to examine each bit position ( i ) (from 0 to 61, since ( a \leq 2^{61} )) and determine whether to set bit ( i ) in ( a ).
-
Logic per Bit:
-