Monisha likes to paint. She has painted $N$ paintings (numbered $1$ through $N$) and wants to choose some subset of these paintings for an exhibition. For each valid $i$, the $i$-th painting has beauty $b_i$ and the probability that it will be displayed at the exhibition is $p_i$. Each painting is chosen or excluded from the exhibition independently randomly.
The beauty of the resulting exhibition is the bitwise XOR of the beauties of all displayed paintings. If no paintings are displayed, the XOR is $0$.
Monisha wants to find out how beautiful her exhibition is going to be. Help her compute the expected value of the beauty of the exhibition.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$.
- The second line contains $N$ space-separated integers $b_1, b_2, \ldots, b_N$.
- The third line contains $N$ space-separated real numbers $p_1, p_2, \ldots, p_N$. Each of these numbers is given with at most five digits after the decimal point.

-----Output-----
For each test case, print a single line containing one real number — the expected beauty of the exhibition. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$.

-----Constraints-----
- $1 \le N \le 10^5$
- $0 \le b_i \le 10^9$ for each valid $i$
- $0 \le p_i \le 1$ for each valid $i$
- the sum of $N$ over all test cases does not exceed $4 \cdot 10^5$

-----Example Input-----
2
3
5 6 2
1.0 0.0 0.5
4
2 2 2 2
0.5 0.5 0.5 0.5

-----Example Output-----
6.000000000000000
1.000000000000000

-----Explanation-----
Example case 1: The first painting must be displayed at the exhibition, the second one must be excluded. If the third painting is displayed (with probability $0.5$), the beauty is $5 \oplus 2 = 7$; if it is excluded (with probability $0.5$), the beauty is $5$. The expected beauty is $0.5 \cdot 5 + 0.5 \cdot 7 = 6$.
Example case 2: If there is an odd number of paintings at the exhibition, the beauty is $2$; this has a probability of $0.5$. If there is an even number of paintings, the beauty is $0$ (with probability $0.5$), so the expected beauty is $0.5 \cdot 0 + 0.5 \cdot 2 = 1$.

Source: codechef | Difficulty: VERY_HARD
Tags: Bit manipulation

Mathematics

Probability


Solutions:
for i in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	p = list(map(float, input().split()))
	ans = 0
	o = [0] * 30
	for i in range(n):
		s = bin(b[i])[2:]
		for j in range(1, len(s) + 1):
			if s[-j] == '1':
				o[j - 1] = (1 - o[j - 1]) * p[i] + (1 - p[i]) * o[j - 1]
	ans = 0
	x = 1
	for item in o:
		ans += x * item
		x = x * 2
	print(ans)


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
from math import *
from decimal import *
from copy import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 10 ** 6 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b):
	return abs(a[0] - b[0]) + abs(b[1] - a[1])

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN():
	return list(numIN())

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	st = en = be = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
			st = be
			en = i
		if meh < 0:
			meh = 0
			be = i + 1
	return (msf, st, en)

def flush():
	return sys.stdout.flush()
for _ in range(int(input())):
	n = int(input())
	a = arrIN()
	p = list(map(float, input().split()))
	dp = [0] * 32
	for i in range(32):
		for j in range(n):
			if a[j] & 1 << i:
				dp[i] = (1 - p[j]) * dp[i] + (1 - dp[i]) * p[j]
	ans = 0
	for i in range(32):
		ans += dp[i] * (1 << i)
	print(ans)


T = int(input())
for i in range(0, T):
	N = int(input())
	b = [int(x) for x in input().split()]
	p = [float(x) for x in input().split()]
	arr = [0] * 32
	for j in range(0, len(b)):
		st = bin(b[j])
		st = st[2:]
		for k in range(1, len(st) + 1):
			if st[-k] == '1':
				arr[k - 1] = (1 - arr[k - 1]) * p[j] + (1 - p[j]) * arr[k - 1]
	exp = 0
	xr = 1
	for j in range(0, len(arr)):
		exp = exp + xr * arr[j]
		xr = xr * 2
	print(exp)


for _ in range(int(input())):
	n = int(input())
	(beauty, prob) = (list(map(int, input().split())), list(map(float, input().split())))
	(exp0, exp1) = ([1.0 for _ in range(31)], [0.0 for _ in range(31)])
	for i in range(n):
		(curr_beauty, curr_prob, j) = (beauty[i], prob[i], 0)
		while 30 >= j and 0 != curr_beauty:
			if curr_beauty & 1:
				exp0[j] = exp1[j] * curr_prob + exp0[j] * (1 - curr_prob)
				exp1[j] = 1 - exp0[j]
			(curr_beauty, j) = (curr_beauty // 2, j + 1)
	(ans, j, mul) = (0.0, 0, 1)
	while 30 >= j:
		(ans, mul, j) = (ans + mul * exp1[j], mul * 2, j + 1)
	print('%.9f' % ans)


def findout(P):
	ans = 1
	ans2 = 1
	for x in P:
		ans *= 2 * x - 1
	if len(P) % 2 == 0:
		return (1 - ans) / 2
	else:
		return (1 + ans) / 2

def find(b, p):
	B = max(b)
	okay = len(bin(B)[2:])
	new_b = []
	for x in b:
		new_b += [('0' * (okay - len(bin(x)[2:])) + bin(x)[2:])[::-1]]
	ans = 0
	for i in range(okay):
		temp_s = ''
		ones = []
		for j in range(len(new_b)):
			if new_b[j][i] == '1':
				ones += [p[j]]
		want = findout(ones)
		ans += 2 ** i * want
	return ans
T = int(input())
for _ in range(T):
	N = int(input())
	b = list(map(int, input().strip().split(' ')))
	p = list(map(float, input().strip().split(' ')))
	print(find(b, p))


l = [1]
for k in range(31):
	l.append(l[-1] * 2)

def convert(s):
	while len(s) != 32:
		s = '0' + s
	ans = ''
	for i in s:
		ans = i + ans
	return ans

def main():
	for _ in range(int(input())):
		n = int(input())
		val = list(map(int, input().split()))
		p = list(map(float, input().split()))
		res = 0
		for i in range(32):
			x = 0
			for j in range(n):
				if val[j] & 1 << i:
					x = p[j] * (1 - x) + (1 - p[j]) * x
			res += l[i] * x
		print('%.6f' % res)
main()


for _ in range(int(input())):
	n = int(input())
	B = [int(x) for x in input().split()]
	P = [float(x) for x in input().split()]
	tl = [0] * 30
	for (b, p) in zip(B, P):
		bs = bin(b)[2:][::-1]
		for i in range(len(bs)):
			if bs[i] == '1':
				tl[29 - i] = (1 - tl[29 - i]) * p + (1 - p) * tl[29 - i]
	ans = 0
	for i in range(29, -1, -1):
		ans += 2 ** (29 - i) * tl[i]
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	B = list(map(int, input().strip().split(' ')))
	P = list(map(float, input().strip().split(' ')))
	l = [0] * 32
	ans = 0
	cur_bit = 1
	for i in range(0, 31):
		cur_prob = 0.0
		for j in range(0, len(B)):
			if cur_bit & B[j]:
				cur_prob = cur_prob * (1 - P[j]) + (1 - cur_prob) * P[j]
		cur_bit = cur_bit << 1
		ans = ans + cur_prob * 2 ** i
	print(ans)
	t -= 1


k = 30
for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(float, input().split()))
	ans = 0
	for i in range(k):
		p = 0
		for j in range(n):
			if l1[j] & 1 << i:
				p = p * (1 - l2[j]) + (1 - p) * l2[j]
		ans += p * (1 << i)
	print('%.15f' % ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(float, input().split()))
	ans = 0
	for i in range(30):
		temp = 0
		for j in range(n):
			if a[j] & 1 << i:
				temp = temp * (1 - b[j]) + (1 - temp) * b[j]
		ans += temp * (1 << i)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	xl = [int(x) for x in input().split()]
	pl = [float(x) for x in input().split()]
	tl = [0] * 30
	for (x, p) in zip(xl, pl):
		bs = bin(x)[2:][::-1]
		for i in range(len(bs)):
			if bs[i] == '1':
				tl[i] = (1 - tl[i]) * p + (1 - p) * tl[i]
	ans = 0
	for i in range(30):
		ans += 2 ** i * tl[i]
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	b = [int(x) for x in input().split()]
	p = [float(x) for x in input().split()]
	s = [0] * 10
	yet = 2
	mx = 0
	for i in range(n):
		st = bin(b[i])
		rng = len(st) - 2
		if rng + 2 > yet:
			for ml in range(rng + 2 - yet):
				s.append(0)
		if rng > mx:
			mx = rng
		for k in range(2, rng + 2):
			if st[k] == '1':
				s[rng - k + 1] = s[rng - k + 1] * (1 - p[i]) + (1 - s[rng - k + 1]) * p[i]
	mult = 1
	ans = 0
	for i in range(0, mx):
		ans += mult * s[i]
		mult = mult * 2
	print('%.16f' % ans)
	t -= 1


t = int(input())
while t:
	x = 0
	n = int(input())
	b = list(map(int, input().strip().split()))
	p = list(map(float, input().strip().split()))
	for i in range(30):
		pb = 0.0
		for j in range(n):
			if b[j] & 1 << i:
				pb = pb * (1 - p[j]) + (1 - pb) * p[j]
		x += (1 << i) * pb
	print(x)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	b = [int(i) for i in input().split()]
	p = [float(i) for i in input().split()]
	pb = [0] * 30
	for i in range(n):
		h = b[i]
		j = 0
		while h:
			if h % 2 == 1:
				x = pb[j]
				y = p[i]
				pb[j] = x * (1 - y) + y * (1 - x)
			h = h // 2
			j += 1
	ans = 0
	k = 1
	for i in pb:
		ans += k * i
		k *= 2
	print('%.10f' % ans)


for _ in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	p = list(map(float, input().split()))
	x = 0
	for i in range(30):
		pb = 0.0
		for j in range(n):
			if b[j] & 1 << i:
				pb = pb * (1 - p[j]) + (1 - pb) * p[j]
		x += (1 << i) * pb
	print(x)