Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

OR Tuples CodeChef Solution

Problem -OR Tuples CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

OR Tuples CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void solve(int test) {
  ll p, q, r;
  cin >> p >> q >> r;
  ll ans = 1;
  while (p || q || r) {
    ll x = 0;
    if (p & 1) x++;
    if (q & 1) x++;
    if (r & 1) x++;
    if (x == 1) {
      ans = 0;
      break;
    }
    if (x == 3) ans = ans * 4;
    p = p >> 1;
    q = q >> 1;
    r = r >> 1;
  }
  cout << ans << endl;
}

signed main() {

  int T = 1;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  cin >> T;
  for (int i = 1; i <= T; i++) {
    //cout <<"Case #" <<i<<": ";
    solve(i);
  }

  return 0;
}

OR Tuples CodeChef Solution in C++14

#include <iostream>
#include<cmath>
using ll= long long;
using namespace std;

int main() {
	// your code goes here
int t;
cin>>t;
while(t--)
{
    ll A,B,C,x,y,z,count=0;
    cin>>A>>B>>C;
    
    while(A>0 || B>0 || C>0)
    {
        x=A%2;
        y=B%2;
        z=C%2;
        
        if(x+y+z==1)
        break;
        else if(x+y+z==3)
        count++;
        
        A=A>>1;
        B=B>>1;
        C=C>>1;
        
    }
    if(A!=0 || B!=0 || C!=0)
    {
        cout<<0<<endl;
    }
    else{
        ll ans=pow(4,count);
        cout<<ans<<endl;
    }
}
	return 0;
	
}

OR Tuples CodeChef Solution in PYTH 3

# cook your dish here
for _ in range(int(input())):
    P, Q, R = list(map(int, input().split()))
    p = str(bin(P))[2:]
    q = str(bin(Q))[2:]
    r = str(bin(R))[2:]
    pl = len(p)
    ql = len(q)
    rl = len(r)
    ml = max(pl, ql, rl)
    p = '0'*(ml - pl) + p
    r = '0' * (ml - rl) + r
    q = '0' * (ml - ql) + q
    ans = 1
    flag = True
    for i in range(ml):
        if int(p[i]) + int(q[i]) + int(r[i]) == 1:
            flag = False
            break
        elif int(p[i]) + int(q[i]) + int(r[i]) == 3:
            ans *= 4
    if flag:
        print(ans)
    else:
        print(0)

OR Tuples CodeChef Solution in C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define lli long long int
#define li long int
#define inf 10000

int max(int a, int b)
{
    int m = a;
    if(b>a)
        m = b;
    return m;
}

int min(int a, int b)
{
    int m = a;
    if(b<a)
        m = b;
    return m;
}

// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(lli arr[], lli l, lli m, lli r)
{
    lli i, j, k;
    lli n1 = m - l + 1;
    lli n2 = r - m;
 
    /* create temp arrays */
    lli L[n1], R[n2];
 
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
 
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    /* Copy the remaining elements of L[], if there
    are any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    /* Copy the remaining elements of R[], if there
    are any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(lli arr[], lli l, lli r)
{
    if (l < r) {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        lli m = l + (r - l) / 2;
 
        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
 
        merge(arr, l, m, r);
    }
}

void Dec2Bin(lli num, int bin[])
{
    lli n = num;
    int i = 21;
    while(n)
    {
        bin[i] = n%2;
        i--;
        n /= 2;
    }
    return;
}

void program()
{
    /*
    see lal khata/mobile heart pic
    */
    lli p,q,r;
    scanf("%lld%lld%lld", &p, &q, &r);
    int Pbin[22],Qbin[22],Rbin[22];
    int i;
    for ( i = 0; i < 22; i++)
    {
        Pbin[i] = 0;
        Qbin[i] = 0;
        Rbin[i] = 0;
    }
    Dec2Bin(p, Pbin);
    Dec2Bin(q, Qbin);
    Dec2Bin(r, Rbin);
    int SumBin[22];
    for ( i = 0; i < 22; i++)
    {
        SumBin[i] = Pbin[i] + Qbin[i] + Rbin[i];
    }
    lli ans = 1;
    for ( i = 0; i < 22; i++)
    {
        switch (SumBin[i])
        {
            case 0: ans *= 1; break; 
            case 1: ans *= 0; break;
            case 2: ans *= 1; break;
            case 3: ans *= 4; break;
        }
    }
    printf("%lld\n", ans);
}

int main(void) 
{
    long long int T;
    scanf("%lld", &T);
    //printf("%lld\n", T);
    // T = 1;
    for (long long int i = 0; i < T; i++)
    {
        program();
    }
	return 0;
}

OR Tuples CodeChef Solution in JAVA

/* package codechef; // don't place package name! */

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
	{
		// your code goes here
		try
		{
		    long T,p,q,r,a,ans=1;
		    Scanner sc=new Scanner (System.in);
		    T=sc.nextInt ();
		    while (T-->0)
		    {
		        ans=1;
		        p=sc.nextInt ();
		        q=sc.nextInt ();
		        r=sc.nextInt ();
		        for (int i=0;i<32;i++)
		        {
		        a=((p>>i)&1)+((q>>i) &1)+((r>>i) &1);
		        if (a == 3)
		        {
		            ans=ans *4;
		        }
		        else if (a==1)
		        {
		            ans=0;
		            break;
		        }
		        }
		        System.out.println (ans);
		    }
		    
		}
		catch (Exception e)
		{
		    System.out.println (e);
		}
	}
}

OR Tuples CodeChef Solution in PYPY 3

for _ in range(int(input())):
    p, q, r = map(int, input().split())
    
    ans = 1
    for i in range(20):
        s = (p&1) + (q&1) + (r&1)
        if s == 1:
            print(0)
            break
        
        elif s == 3:
            ans *= 4
        
        p, q, r = p//2, q//2, r//2
    
    else:
        print(ans)

OR Tuples CodeChef Solution in C#

using System.Dynamic;
using System.Collections.Concurrent;
using System;
using System.Linq;
class Program{
    public static void Main(string[] args){
        int t=1;
        t = Convert.ToInt32(Console.ReadLine());
        while(t-- > 0) {
            solve();
        }
    }
    private static void solve() {
        var input = Console.ReadLine()?.Trim().Split(" ").Select(x => Convert.ToInt32(x)).ToArray();
        Int64 p,q,r;
	    p = input?[0] ?? 0;
	    q = input?[1] ?? 0;
	    r = input?[2] ?? 0;
	    Int64[] T= new Int64[4]{1,0,1,4};
        Int64 ans=1;
        for(int i=0;i<30;++i)
        {
            Int64 temp = ((p>>i)&1)+((q>>i)&1)+((r>>i)&1);
            ans*=T[temp];
        }
        System.Console.WriteLine(ans);
    }
}  

OR Tuples CodeChef Solution in NODEJS

process.stdin.resume();
process.stdin.setEncoding('utf8');

// your code goes here
let input = '' ;
process.stdin.on('data', function(chunk) {
    input += chunk
});

process.stdin.on('end', function(chunk) {
    const [n,...arr]= input.split('\n');
    countTuples(n, arr) ;
}) ;


function countTuples(t, arr) {
    let index=0;
    while(t--) {
        let [p,q,r] = arr[index].split(' ');
        p= parseInt(p);
        q= parseInt(q);
        r= parseInt(r);
        let ans=1//, a = b = c = new Array(8).fill(0);
        // console.log("val ",a,b,c);
        for (let i=0; i<=20; i++){
            let mask = 1<<i;
            let count=0;
            if (mask&p) {
                ++count;
            }
            if(mask&q) {
                ++count;
            }
            if(mask&r) {
                ++count;
            }
            if(count===0 || count === 2) {
                
                ans *= 1;
            } else if(count == 1) {
                ans *= 0;
                break;
            }
            else if (count===3){
                ans *= 4;
            }
        }
        console.log(ans);
        index++;
    }
}

OR Tuples CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	var buf bytes.Buffer

	tc := readNum(reader)

	for tc > 0 {
		tc--
		P, Q, R := readThreeNums(reader)
		res := solve(P, Q, R)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

const H = 20

func solve(P, Q, R int) int64 {

	var res int64 = 1

	for i := 0; i < H; i++ {
		x := (P >> i) & 1
		y := (Q >> i) & 1
		z := (R >> i) & 1
		cnt := x + y + z
		if cnt == 0 {
			// only one options, all be zero
			continue
		}
		if cnt == 1 {
			return 0
		}
		if cnt == 2 {
			// a, b, or c be zero
			continue
		}
		res *= 4
	}

	return res
}

func solve1(P, Q, R int) int64 {
	// max(P, Q, R) < 1 << 20
	// A | B = P, B | C = Q, C | A = R
	// A, B should be subset of P
	// B, C should be subset of Q
	// A, C should be subset of R
	nums := []int{P, Q, R}
	sort.Ints(nums)
	P, Q, R = nums[0], nums[1], nums[2]

	// B := P ^ A,
	// C := R ^ A
	var res int64

	for a := P; ; a = (a - 1) & P {
		if a&R == a {
			// b 至少要包含所有P中为1,且a中不为1的部分,
			b := P ^ a
			// c 至少要包含所有R中为1,且a中不为1的部分,
			c := R ^ a
			x := b | c
			if x&Q == x {
				// x 是 Q的一个子集
				var tmp int64 = 1
				for i := H - 1; i >= 0; i-- {
					if (Q>>i)&1 == 1 {
						// 如果Q[i] = 1
						if (x>>i)&1 == 0 {
							// 这一位需要为1,要么在b中为1, 要么在c中为1
							pw := 1
							if (P>>i)&1 == 1 {
								// b中可以为1
								pw *= 2
							}
							if (R>>i)&1 == 1 {
								// c 中可以为1
								pw *= 2
							}
							// pow(2, cnt) - 1
							tmp *= int64(pw - 1)
						}
						//else 这一位已经为1,(b中为1,or c = 1)
					}
					// else Q[i] = 0, then x[i] = 0, and force b[i] = 0, c[i] = 0
				}
				res += tmp
			}
		}
		if a == 0 {
			break
		}
	}

	return res
}
OR Tuples CodeChef Solution Review:

In our experience, we suggest you solve this OR Tuples CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the OR Tuples CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this OR Tuples CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *