1918C - XOR-distance - CodeForces Solution


bitmasks dp greedy implementation

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;

typedef long long ll;
typedef long double ld;
typedef pair<ll , ll> PII ;
typedef vector<bool> vb ;
typedef vector<ll> vi;
typedef vector<PII> vpi;
typedef vector<vector<ll>> vvi;
typedef vector<char> vc ;

const double EBS = 1e-9;
const double pi = 3.14159265358979 ;


#define Log(x) (31^__builtin_clz(x)) // Easily calculate log2 on GNU G++ compilers
#define logll(x) (63^__builtin_clzll(x))
#define popcount(i) __builtin_popcount(i)


#define mp make_pair

#define testCase ll t; cin >> t; while (t--)
#define fast  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(f, s, i) for (ll i = f; i < s; i++)
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define calunique(v)  disance(v.begin(),unique(v.begin(),v.end()));
#define el cout << "\n"
#define mb make_pair
#define pb push_back
#define no cout << "NO\n"
#define yes cout << "YES\n"
#define all(v) v.begin(), v.end()
#define INF (ll) 1e9
#define INFLL (ll)1e18
#define debug cout << "___________________________________" << endl
#define left  ((p<<1) + 1)
#define right ((p<<1) + 2)
#define int ll


void solve(){
	int a , b , x ; cin >> a >> b >> x ; 
	if(a < b) swap(a , b);
	bool ok = 1 ; 
	for(int i = 59 ; i >= 0 ; i--){
		int bit = (1LL << i) ; 
		
		if((a & bit) && !(b & bit)){
			if(ok){
				ok = 0 ;
				continue ;
			}
			if(x < bit) continue ;
			a ^= bit ;
			b ^= bit ; 
			x -= bit ;
		}
	}
	cout << abs(a - b) << endl ;
}

int32_t main(){
	fast ;

	testCase{
		solve();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II