BIT_FILP Editorial

PROBLEM LINK:

Practice

Author: Prathamesh Sogale
Tester: Riddhish Lichade
Editorialist:Yogesh Deolalkar

DIFFICULTY:

CAKEWALK, SIMPLE

PREREQUISITES:

Greedy, Math

PROBLEM:

After J.J.Jameson revealed that Peter is Spider-Man and fake news about Mysterio,Peter and his friend’s got rejected from MIT. Peter felt very bad for his friends and decided to convince the professor of MIT. But when he reached MIT he found out that professor has already left for flight so he stared following her.

There was traffic at the bridge so he was able to find the professor, But suddenly bridge started shaking and Dr-Otto-Octavius(Doc-Ock) appeared out of no-where and started attacking Peter. As Doc-Ocks robotic hands are made up of nano-tech and during the fight he absorbed the nano-tech from Spidey’s suit.As Spiderman’s Suit is more advanced than Doc-Ocks robotic arms and as the part of his suit is absorbed by Doc, and is connected to the Spiderman’s suit, Spiderman can now manipulate the instruction in robotic arms by flipping the data bits from the Doc-Ocks arms.

You are given integer NN denoting instructions from Doc-Ocks suit,You need to perform QQ querries on it each querry contains 2 integers a,ba,b you need to flip the bits at position aa & bb of binary representation of NN from right most end.

You need to print value of NN after performing Q Queries

SOLUTIONS:

Setter's Solution

#include
using namespace std;
using ll = long long int;
bool bitset(ll n, ll pos){
if(n & (1<<pos)){
return true;
}
return false;
}
ll setBit(ll n,ll pos){
return (n | (1<<pos));
}

ll clearBit(ll n,ll pos){
return (n &(~(1<<pos)));
}

int main() {
// your code goes here
ll t;
cin>>t;
while(t–){
ll n, q;
cin>>n>>q;
while(q–){
ll x, y;
cin>>x>>y;
if(bitset(n,x-1)){
n = clearBit(n,x-1);
}else{
n = setBit(n,x-1);
}

        if(bitset(n,y-1)){
            n = clearBit(n,y-1);
        }else{
            n = setBit(n,y-1);
        }
    }
    cout<<n<<endl;
}
return 0;

}

Tester's Solution

cook your dish here

def output(n, temp):

return (n^(1 << (temp-1)))

T=int(input())
for i in range(T):
n, q = map(int, input().split())

for i in range(q):
    a, b = map(int, input().split())
    n = output(n, a)
    n = output(n, b)

print(n)