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 :
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
Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →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 →