Problem Statement https://www.hackerrank.com/challenges/xoring-ninja Given a list containing N integers, calculate the XOR_SUM of all the non-empty subsets of the list and print the value of sum % (109 + 7). XOR operation on a list (or a subset of the list) is defined as the XOR of all the elements present in it. E.g. XOR of list containing elements {A,B,C} = ((A^B)^C), where ^ represents XOR. E.g. XOR_SUM of list A having three elements {X1, X2, X3} can be given as follows. All non-empty subsets will be {X1, X2, X3, (X1,X2), (X2,X3), (X1,X3), (X1,X2,X3)} XOR_SUM(A) = X1 + X2 + X3 + X1^X2 + X2^X3 + X1^X3 + ((X1^X2)^X3) Input Format
An integer T, denoting the number of testcases. 2T lines follow. Each testcase contains two lines, first line will contains an integer N followed by second line containing N integers separated by a single space. Output Format
T lines, ith line containing the output of the ith testcase. Constraints
1 <= T <= 5 1 <= N <= 105 0 <= A[i] <= 109
let large_number = 1000000007 ;;
let compute n k = if k = 0 then 0 else let result = ref 1 in for i = 1 to (n - 1) do result := (!result lsl 1) mod large_number done; !result ;; let process arr length = let max_elem = ref (-1) in for i = 0 to (length - 1 ) do if (arr.(i) > !max_elem) then max_elem := arr.(i) done; let level = ref 0 and result = ref 0 in while !max_elem > 0 do let count = ref 0 in for i = 0 to (length - 1) do count := !count + (1 land arr.(i)); arr.(i) <- (arr.(i) lsr 1) done; (* Printf.printf "max_elem: %d, level: %d, result: %d, count: %d\n" !max_elem !level !result !count; *) let res = compute length !count in result := (!result + (res lsl !level) ) mod large_number; level := !level + 1 ; max_elem := !max_elem lsr 1 ; done; !result ;; (* main *) let num_case = read_int () ;; for i = 1 to num_case do let n = read_int () in let line = read_line () in let lst_string = Str.split (Str.regexp " ") line in let lst_int = List.map int_of_string lst_string in let arr = Array.of_list lst_int in let result = process arr n in Printf.printf "%d\n" result done ;;
No comments:
Post a Comment