# OR Tuples | codechef solution

## Problem

Chef has $3$ numbers $�$$�$ and $�$. Chef wants to find the number of triples $\left(�,�,�\right)$ such that:

• $\left(�\mid �\right)=�,$ $\left(�\mid �\right)=�$ and $\left(�\mid �\right)=�$ (Here, $\mid$ denotes the bitwise OR operation)
• $0\le �,�,�<{2}^{20}$

Can you help Chef?

### Input Format

• The first line of input will contain a single integer $�$, denoting the number of test cases.
• Each test case consists of a single line of input containing $3$ space-separated integers denoting $�,�$ and $�$ respectively.

### Output Format

For each test case, output a single integer denoting the number of triplets $\left(�,�,�\right)$ that satisfy the given conditions.

### Constraints

• $1\le �\le 1{0}^{5}$
• $0\le �,�,�<{2}^{20}$

### Sample 1:

Input
Output
3
10 12 14
0 5 5
0 0 7

4
1
0


### Explanation:

Test case $1$: The following $4$ triplets $\left(�,�,�\right)$ satisfy $�\mid �=10,�\mid �=12,$ and $�\mid �=14$$\left(2,8,12\right),\left(10,0,12\right),\left(10,8,4\right),$ and $\left(10,8,12\right)$.

Test case $2$: The following triplet $\left(�,�,�\right)$ satisfies $�\mid �=0,�\mid �=5,$ and $�\mid �=5$$\left(0,0,5\right)$.

Test case $3$: There are no triplets satisfying all the conditions.

Code(C++):-

#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{ int t;
cin>>t;
while(t--)
{ int p,q,r;
cin>>p>>q>>r;
vector<int>pbit(20,0);
vector<int>qbit(20,0);
vector<int>rbit(20,0);
for(int i=0;i<20;i++)
{ if(p&(1<<i))
pbit[i]=1;
}
for(int i=0;i<20;i++)
{ if(q&(1<<i))
qbit[i]=1;
}
for(int i=0;i<20;i++)
{ if(r&(1<<i))
rbit[i]=1;
}
int ans=1;
bool flag=true;
for(int i=0;i<20;i++)
{ if(pbit[i]+qbit[i]+rbit[i]==3)
ans*=4;
else if(pbit[i]+qbit[i]+rbit[i]==1)
{ flag=false;
break;
}
}
if(flag==false)
cout<<"0"<<"\n";
else
cout<<ans<<"\n";
}
return 0;
}

Code(JAVA):-

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int test=sc.nextInt();
while(test-->0){
int p=sc.nextInt();
int q=sc.nextInt();
int r=sc.nextInt();
long count=1;
for(int i=0;i<20;i++){
int b1=(p>>i)&1;
int b2=(q>>i)&1;
int b3=(r>>i)&1;
if(b1+b2+b3==3){
count*=4;
}else if(b1+b2+b3==1){
count=0;
break;
}
}
out.println(count);
}
out.close();
}
}

### Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-