The Little Elephant from the Zoo of Lviv is going to the Birthday Party of  the Big Hippo tomorrow. Now he wants to prepare a gift for the Big Hippo.

He has N balloons, numbered from 1 to N. The i-th balloon has the color Ci and it costs Pi dollars. The gift for the Big Hippo will be any subset (chosen randomly, possibly empty) of the balloons such that the number of different colors in that subset is at least M.

Help Little Elephant to find the expected cost of the gift.

-----Input-----
The first line of the input contains a single integer T - the number of test cases. T test cases follow. The first line of each test case contains a pair of integers N and M. The next N lines contain N pairs of integers Ci and Pi, one pair per line.

-----Output-----
In T lines print T real numbers - the answers for the corresponding test cases. Your answer will considered correct if it has at most 10^-6 absolute or relative error.

-----Constraints-----
- 1 ≤ T ≤ 40
- 1 ≤ N,  Ci≤ 40
- 1 ≤ Pi ≤ 1000000
- 0 ≤ M ≤ K, where K is the number of different colors in the test case.

-----Example-----
Input:
2
2 2
1 4
2 7
2 1
1 4
2 7

Output:
11.000000000
7.333333333

Source: codechef | Difficulty: HARD
Tags: Dynamic programming

Combinatorics

Mathematics

Probability


Solutions:
T = int(input())
for testIndex in range(T):
	(N, M) = map(int, input().split())
	products = [(0, 0)]
	for productIndex in range(N):
		(C, P) = map(int, input().split())
		products += [(C, P)]
	products.sort(key=lambda x: x[0])
	dpN = [[0 for j in range(N + 1)] for i in range(N + 1)]
	dpS = [[0 for j in range(N + 1)] for i in range(N + 1)]
	dpN[0][0] = 1
	dpS[0][0] = 0
	for last in range(N):
		for colors in range(N):
			if dpN[last][colors] > 0:
				for next in range(last + 1, N + 1):
					colorsAfter = colors + 1
					if products[last][0] == products[next][0]:
						colorsAfter -= 1
					dpS[next][colorsAfter] += dpS[last][colors] + dpN[last][colors] * products[next][1]
					dpN[next][colorsAfter] += dpN[last][colors]
	allN = 0
	allS = 0
	for last in range(0, N + 1):
		for colors in range(M, N + 1):
			allS += dpS[last][colors]
			allN += dpN[last][colors]
	result = allS / allN
	print('%.7f' % result)


def epc(d):
	r = [0, 1, 0]
	for i in range(len(d)):
		p = r[0]
		for j in range(1, len(r)):
			t = r[j]
			r[j] = p + t * d[i]
			p = t
		r += [0]
	r = r[1:-1]
	r.reverse()
	return r

def main():
	T = int(input())
	while T > 0:
		line = input().split(' ')
		N = int(line[0])
		M = int(line[1])
		n = {}
		d = {}
		for i in range(N):
			line = input().split(' ')
			C = int(line[0])
			P = int(line[1])
			if C in n:
				n[C] += P
				d[C] = (d[C] + 1) * 2 - 1
			else:
				n[C] = P
				d[C] = 1
		K = len(n)
		for c in n:
			n[c] = n[c] * (d[c] - ((d[c] + 1) / 2 - 1)) / d[c]
		b = [[], []]
		d0 = []
		dp = 1
		for c in d:
			b[0] += [0]
			b[1] += [d[c]]
			d0 += [d[c]]
			dp *= d[c]
		e0 = epc(d0)
		for m in range(2, K + 1):
			b += [[]]
		for i in range(len(d0)):
			d1 = []
			for j in range(i):
				d1 += [d0[j]]
			for j in range(i + 1, len(d0)):
				d1 += [d0[j]]
			e1 = epc(d1)
			for m in range(2, K):
				b[m] += [e0[m] - e1[m]]
			b[-1] += [dp]
		n0 = []
		for c in n:
			n0 += [n[c]]
		rn = 0
		rd = 0
		for i in range(M, K + 1):
			rd += e0[i]
			for j in range(K):
				rn += n0[j] * b[i][j]
		print('%.8f' % (rn / rd))
		T -= 1
main()


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	colors = [0] * 41
	cost = [0] * 41
	color = 0
	for i in range(n):
		(cc, pp) = list(map(int, input().split()))
		colors[cc] += 1
		cost[cc] += pp
	for i in colors:
		if i > 0:
			color += 1
	dp2 = [[0] * 41 for i in range(color + 1)]
	dp2[0] = [1] * 41
	for i in range(1, color + 1):
		for j in range(1, 41):
			dp2[i][j] = dp2[i][j - 1] + dp2[i - 1][j - 1] * (2 ** colors[j] - 1)
	dp1 = [[0] * 41 for i in range(color + 1)]
	for i in range(1, color + 1):
		for j in range(1, 41):
			dp1[i][j] = dp1[i][j - 1] + dp1[i - 1][j - 1] * (2 ** colors[j] - 1) + dp2[i - 1][j - 1] * cost[j] * 2 ** (colors[j] - 1)
	num = den = 0
	for i in range(m, color + 1):
		num += dp1[i][40]
		den += dp2[i][40]
	print(num / den)