PARTY21 Editorial | TERM20 | UIET PU Campus Chapter

PROBLEM LINK:

Smallest Cost

Setter: Nitin Pundir
Tester: Nitin Pundir
Editorialist: Nitish Kumar Tiwari

DIFFICULTY

simple

PREREQUISITES

Bitwise operator and their properties.

PROBLEM

Find X and Y for which (B&X)&(G&X), (B&Y)|(G&Y) is maximum and X+Y is minimum.

EXPLANATION

We have to just minimize the expression (B&X)&(G&X), (B&Y)|(G&Y) using distributive law.
(B&X)&(G&X) = (B&G)&X
(B&Y)|(G&Y) = (B|G)&Y
Now maximum value of (A&B) where A is fixed and B is variable equals A and minimum value of B for which (A&B) = A is A
Similarly minimum value of X for which (B&G)&X is maximum is (B&G)
With the same analogy minimum value of Y for which (B|G)&Y is maximum is (B|G).

So X+Y = (B&G)+(B|G)

TIME COMPLEXITY

The time complexity is O(1) .

SOLUTIONS

Setter's Solution

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int test_case;
cin>>test_case;
while(test_case–)
{
long long int a,b;
cin>>a>>b;
cout<<(a|b)+(a&b)<<endl;
}
return 0;
}

1 Like