Xoring Ninja


Problem Statement :


An XOR operation on a list is defined here as the xor () of all its elements (e.g.: ).

The  of set  is defined here as the sum of the s of all non-empty subsets of  known as . The set  can be expressed as:


For example: Given set 

The set of possible non-empty subsets is: 

The  of these non-empty subsets is then calculated as follows:
 = 

Given a list of  space-separated integers, determine and print .

For example, . There are three possible subsets, . The XOR of , of  and of . The XorSum is the sum of these:  and .

Note: The cardinality of powerset is , so the set of non-empty subsets of set  of size  contains  subsets.

Function Description

Complete the xoringNinja function in the editor below. It should return an integer that represents the XorSum of the input array, modulo .

xoringNinja has the following parameter(s):

arr: an integer array


Input Format

The first line contains an integer , the number of test cases.

Each test case consists of two lines:
- The first line contains an integer , the size of the set .
- The second line contains  space-separated integers .


Output Format

For each test case, print its  on a new line. The  line should contain the output for the  test case.



Solution :



title-img


                            Solution in C :

In  C  :






#include<stdio.h>
#define LL long long int
#define MOD 1000000007

LL power(LL a, LL b)
{
	if (b == 0)
		return 1;
	else
	{
		LL temp=(power(a,b/2))%MOD;
		if(b%2==0)   
			return (temp*temp)%MOD;
		else
			return (((temp*a)%MOD)*temp)%MOD;
	}
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		int x=0;
		int i,a;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a);
			x|=a;
		}
		LL ans=power(2,n-1);
		ans=(ans*x)%MOD;
		printf("%lld\n",ans);
	}
	return 0;
}
                        


                        Solution in C++ :

In  C++  :








#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <ctime>
#include <cassert>
#include <utility>

using namespace std;

#define MAXN 100005
#define MOD 1000000007

int T, N;
int A[MAXN];
long long dp[MAXN][2];

int main() {
//	freopen("date.in", "r", stdin);
	
	cin >> T;
	while(T--) {
        cin >> N;
        for(int i = 0; i < N; i++)
            cin >> A[i];
        
        long long ans = 0;
        for(int bit = 0; bit < 32; bit++) {
            memset(dp, 0, sizeof(dp)); 
            dp[0][0] = 1;
            dp[0][1] = 0;
            for(int i = 0; i < N; i++) {
                int crt = 0;
                if(A[i] & (1 << bit))
                    crt = 1;
                for(int j = 0; j < 2; j++) {
                    int nj = j ^ crt;
                    dp[i + 1][nj] += dp[i][j];
                    dp[i + 1][j] += dp[i][j];
                    dp[i + 1][nj] %= MOD;
                    dp[i + 1][j] %= MOD;
                }
            }
            long long cnt = dp[N][1];
            ans += cnt * (1LL << bit);
            ans %= MOD;
        }
        
        cout << ans << '\n';
	}
	
	return 0;
}
                    


                        Solution in Java :

In   Java :






import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;


public class Solution {

	static Solution main;
	
	static long sp;
	
	public static void main(String[] args) {
		sp = 1000000007;
		long [] fact = new long[110001];
		long [] pow2 = new long[110001];
		pow2[0] = 1;
		fact[0] = 1;
		for(int i = 1 ; i < 110001 ; i++) {
			fact[i] = fact[i-1] * i;
			fact[i] %= sp;
			pow2[i] = 2*pow2[i-1];
			if(pow2[i]>=sp) {
				pow2[i]-=sp;
			}
		}
		main = new Solution();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedOutputStream bos = new BufferedOutputStream(System.out);
		String eol = System.getProperty("line.separator");
		byte[] eolb = eol.getBytes();
		try {
			String str = br.readLine();
			int t = Integer.parseInt(str);
			for(int i = 0 ; i < t ; i++) {
				str = br.readLine();
				int n = Integer.parseInt(str);
				int [] ar = new int[40];
				Arrays.fill(ar, 0);
				str = br.readLine();
				int j=0;
				int s=0;
				int length = str.length();
				while(j<length) {
					while(j<length) {
						if(str.charAt(j) == ' ') {
							break;
						}else {
							j++;
						}
					}
					int x = Integer.parseInt(str.substring(s,j)) ;	
					int iter = 0;
					while(x>0) {
						if((x%2)==1) {
							ar[iter]++;
						}
						x/=2;
						iter++;
					}
					j++;
					s=j;			
				}
				long ans = 0;
				for(int a = 0 ;a < 40 ; a++) {
					int x = ar[a];
					for(int b = 1 ; b <= x; b+=2) {
						// x choose b * 2^(n-x) * 2^a
						long tempAns = fact[x];
						tempAns *= inverse(fact[b]);
						tempAns %= sp;
						tempAns *= inverse(fact[x-b]);
						tempAns %= sp;
						tempAns *= pow2[n-x+a];
						tempAns %= sp;
						ans += tempAns;
						if(ans>sp) {
							ans -= sp;
						}
					}
				}
				bos.write(new Long(ans).toString().getBytes());
				bos.write(eolb);
			}
			bos.flush();
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public static long inverse(long a) {
		return getPow(a,sp-2);
	}
	
	public static long getPow(long a , long b) {
		long ans = 1;
		long pow = a;
		while(b>0) {
			if((b%2)==1) {
				ans *= pow;
				ans %= sp;
			}
			pow *= pow;
			pow %= sp;
			b/=2;
		}
		return ans;
	}

}
                    


                        Solution in Python : 
                            
In  Python3 :







import sys

def poweroftwo(n):
	val = 1
	yield val
	for i in range(n):
		val *= 2
		yield val
    

r = sys.stdin.readline
#table = list(poweroftwo(100000))
text = r()
t = int(text)
for i in range(t):
	text = r()
	n = int(text)
	text = r()
	tmp = 0
	for num in map(int,text.split()):
		tmp |= num
	#tmp *= table[n]
	for i in range(n-1):
		tmp *= 2
		if i % 20 == 0:
			tmp %= 1000000007
	print(tmp%1000000007)
                    


View More Similar Problems

Dynamic Array

Create a list, seqList, of n empty sequences, where each sequence is indexed from 0 to n-1. The elements within each of the n sequences also use 0-indexing. Create an integer, lastAnswer, and initialize it to 0. There are 2 types of queries that can be performed on the list of sequences: 1. Query: 1 x y a. Find the sequence, seq, at index ((x xor lastAnswer)%n) in seqList.

View Solution →

Left Rotation

A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. Given an integer, d, rotate the array that many steps left and return the result. Example: d=2 arr=[1,2,3,4,5] After 2 rotations, arr'=[3,4,5,1,2]. Function Description: Complete the rotateLeft function in the editor below. rotateLeft has the following parameters: 1. int d

View Solution →

Sparse Arrays

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun

View Solution →

Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →