Header Ads Widget

OR Tuples | codechef solution

Problem

Chef has 3 numbers  and . Chef wants to find the number of triples (,,) such that:

  • ()=, ()= and ()= (Here,  denotes the bitwise OR operation)
  • 0,,<220

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 (,,) that satisfy the given conditions.

Constraints

  • 1105
  • 0,,<220

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 (,,) satisfy =10,=12, and =14(2,8,12),(10,0,12),(10,8,4), and (10,8,12).

Test case 2: The following triplet (,,) satisfies =0,=5, and =5(0,0,5).

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:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-




Post a Comment

0 Comments