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 :


                            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){
            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;
            d[x][i+1][k-j] = dp(x, i+1, k-j);
    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++){
	ll ans = 0;
		for(i=1;i<=A&&i<=j;i++) ans += total - cnt[i^j];
	cout << ans << endl;
	return 0;

                        Solution in Java :

In  Java :

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(;
        int A = in.nextInt();
        int B = in.nextInt();
        int C = in.nextInt();
        int D = in.nextInt();
        int[] m = new int[] {A, B, C, D};
        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];

                        Solution in Python : 
In  Python3 :


import sys

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


tree=[([0]*3100).copy() for i in range(4100)]
cnt=[([0]*3100).copy() for i in range(4100)]
def updateBIT(treeNo,ind):

def getBITVal(treeNo,ind):
    return ret

for i in range(1,A+1):
    for j in range(i,B+1):

for i in range(len(cnt)):
    for j in range(1,len(cnt[i])):
for i in range(1,len(tot)):
for i in range(1,C+1):
    for j in range(i,D+1):

View More Similar Problems

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →