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

Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →