Beautiful Quadruples


Problem Statement :


We call an quadruple of positive integers, , beautiful if the following condition is true:

Note:  is the bitwise XOR operator.

Given , , , and , count the number of beautiful quadruples of the form  where the following constraints hold:

When you count the number of beautiful quadruples, you should consider two quadruples as same if the following are true:

They contain same integers.
Number of times each integers occur in the quadruple is same.
For example  and  should be considered as same.

nput Format

A single line with four space-separated integers describing the respective values of A, B, C, and D.



Solution :



title-img


                            Solution in C :

In  C  :





#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int A; 
    int B; 
    int C; 
    int D; 
long long int d[4097][3002][5];
long long int dp(int x,int i,int k){
    if(k==0){
        if(x!=0){
            return 1;
        } 
        return 0;
    }
    int p=0;
    if(i<=A) p++;
    if(i<=B) p++;
    if(i<=C) p++;
    if(i<=D) p++;
    if(p==0) return 0;
    int j=0;
    if(p<k) return 0;
    long long int l=0;
    for(j=0;j<=k;j++){
        if(d[x][i+1][k-j]==-1){
            d[x][i+1][k-j] = dp(x, i+1, k-j);
        }
        l+=d[x][i+1][k-j];
        x=x^i;
    }
    return l;
}

int main(){
    memset(d, -1, sizeof(d));
    scanf("%d %d %d %d",&A,&B,&C,&D);
    
    printf("%lld\n",dp(0, 1, 4));
    return 0;
}
                        


                        Solution in C++ :

In  C++  :







#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>

using namespace std;

#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define snuke(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

int a[4];
int A,B,C,D;

typedef long long ll;

ll total;
ll cnt[10000];

int main(void){
	int i,j;
	
	REP(i,4) cin >> a[i];
	sort(a, a+4);
	
	A = a[0];
	B = a[1];
	C = a[2];
	D = a[3];
	
	for(i=1;i<=C;i++) for(j=i;j<=D;j++){
		total++;
		cnt[i^j]++;
	}
	
	ll ans = 0;
	
	for(j=1;j<=B;j++){
		for(i=1;i<=A&&i<=j;i++) ans += total - cnt[i^j];
		for(i=j;i<=D;i++){
			total--;
			cnt[i^j]--;
		}
	}
	
	cout << ans << endl;
	
	return 0;
}
                    


                        Solution in Java :

In  Java :






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int A = in.nextInt();
        int B = in.nextInt();
        int C = in.nextInt();
        int D = in.nextInt();
        
        int[] m = new int[] {A, B, C, D};
        Arrays.sort(m);
        
        long[][] count = new long[3001][4096];
        
        for (int i=1; i<=m[0]; i++) {
            for (int j=i; j<=m[1]; j++) {
                count[j][i ^ j] ++;
            }
        }
        for (int i=0; i<4096; i++) {
            for (int j=1; j<=3000; j++) {
                count[j][i] += count[j-1][i];
            }
        }
        
        
        long[] sum = new long[3001];
        for (int j=1; j<=3000; j++) {
            for (int i=0; i<4096; i++) {
                sum[j] += count[j][i];
            }
        }
        
        long res=0;
        for (int i=1; i<=m[2]; i++) {
            for (int j=i; j<=m[3]; j++) {
                int x = i ^ j;
                res += sum[i] - count[i][i^j];
            }
        }
        System.out.println(res);
    }
}
                    


                        Solution in Python : 
                            
In  Python3 :







#!/bin/python3

import sys


A,B,C,D = input().strip().split(' ')
temp = [int(A),int(B),int(C),int(D)]
temp.sort()
A,B,C,D=temp

tot=[0]*4100

tree=[([0]*3100).copy() for i in range(4100)]
cnt=[([0]*3100).copy() for i in range(4100)]
def updateBIT(treeNo,ind):
    while(ind<3100):
        tree[treeNo][ind]+=1
        ind+=ind&-ind

def getBITVal(treeNo,ind):
    ret=0
    while(ind>0):
        ret+=tree[treeNo][ind]
        ind-=ind&-ind
    return ret

zero=0
tot=[0]*3100
for i in range(1,A+1):
    for j in range(i,B+1):
        #updateBIT(i^j,j)
        cnt[i^j][j]+=1
        tot[j]+=1

for i in range(len(cnt)):
    for j in range(1,len(cnt[i])):
        cnt[i][j]+=cnt[i][j-1]
        
for i in range(1,len(tot)):
    tot[i]+=tot[i-1]
    
ans=0        
for i in range(1,C+1):
    ans+=tot[i]*(D+1-i)
    for j in range(i,D+1):
        zero+=cnt[i^j][i]
        #ans+=getBITVal2(i)
        #print(i,j)
#print(ans)
ans-=zero
#print(zero)
print(ans)
                    


View More Similar Problems

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →