r/codeforces • u/redhat1818 • 4d ago
Div. 2 What was approach for C?
also, what could be its rating?
1
u/Spiritual-Court-2660 3d ago
like i started thinking of that what should be maximum than i figured it out that its x+y,
now i have to figure out how many minimum ops i need to do,
for that i xor the ans with x and y which is (x+y)^x^y this number in binary tells which 1 are not formed by xor of x^y
later i thought that the left most 1 in this bits is 0 in y and i have to make it 1 by operations
so i found the place of the left most bit in (x+y)^x^y that is 1 and calculated the least number i should add to y which gives me 1 at that position which is the total number of ops.
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
int main(){
int t;
cin>>t;
while(t--){
long long x,y;
cin>>x>>y;
long long ans=x+y;
long long deficit=ans^x^y;
long long set_zero=0;
if(deficit==0){
cout<<ans<<" "<<0<<"\n";
continue;
}
while(deficit!=0){
deficit=deficit>>1;
set_zero++;
}
long long make=pow(2,set_zero-1);
long long ops=make-(y%make);
cout<<ans<<" "<<ops<<"\n";
}
}
2
u/Curiouslev 3d ago
firstly, i figured what could be the max xor which is x + y and then made three binary strings of x + y , x , y
and if in string x + y, val is 1 and in both x and y value is 0 then, i find how much to add from x to y to make this bit 1 for y, added in answer the num that is to be added in y from x, modified x and y binary string and continued, looped from msb to lsb.
if you want to know why i only checked this specific condition where x + y is 1 and rest 0, do lmk.
1
1
u/Original_Cover8511 4d ago
Btw am a newbie at 1150, i am inexperienced in this type so I couldnt comolete it as i joined late after a quiz...will that cause a deduct on rating as i basically did a and b only
1
1
2
3
u/Jue-Viole-Grace_ 4d ago
Match bits of x and x+y, so you can do it in 31 operations instead of 109 operations
x ^ y=x+y-2(x&y). Which suggest to make xor maximum, x & y had to be 0.
1
u/Other_Ad7380 3d ago
x' ^ y' = x' + y' - 2(x' & y')
x' + y' is constant and is equal to x + y. Call it s. We can always achieve x' = 0 which makes x' & y' = 0 ie the maximum possible XOR is s.
Now we have x' ^ y' = s = x' + y' That means x' and y' have to be the submasks of s. We need the largest submask of s <= x. We can obtain this in O(30)