Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are k different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, he wants to know the minimum number of days he should wait before sending a ranger to collect the orbs such that the probability of him getting at least one of each kind of orb is at least $\frac{p_{i} - \epsilon}{2000}$, where ε < 10^{ - 7}.

To better prepare himself, he wants to know the answer for q different values of p_{i}. Since he is busy designing the battle strategy with Sam, he asks you for your help.


-----Input-----

First line consists of two space separated integers k, q (1 ≤ k, q ≤ 1000) — number of different kinds of orbs and number of queries respectively.

Each of the next q lines contain a single integer p_{i} (1 ≤ p_{i} ≤ 1000) — i-th query.


-----Output-----

Output q lines. On i-th of them output single integer — answer for i-th query.


-----Examples-----
Input
1 1
1

Output
1

Input
2 2
1
2

Output
2
2

Source: codeforces | Difficulty: HARD
Tags: Dynamic programming

Mathematics

Probability


Solutions:
(k, q) = list(map(int, input().split()))
t = [0] * (k + 1)
t[1] = 1
d = [0]
n = i = 1
while i < 1001:
	if 2000 * t[k] > i - 1e-07:
		d.append(n)
		i += 1
	else:
		t = [0] + [(j * t[j] + (k - j + 1) * t[j - 1]) / k for j in range(1, k + 1)]
		n += 1
for i in range(q):
	print(d[int(input())])


(K, Q) = map(int, input().split())
dp = [[0.0 for i in range(K + 1)] for j in range(10000)]
dp[0][0] = 1.0
for i in range(1, 10000, 1):
	for j in range(1, K + 1, 1):
		dp[i][j] = dp[i - 1][j] * j / K + dp[i - 1][j - 1] * (K - (j - 1)) / K
for qi in range(Q):
	P = int(input())
	for i in range(10000):
		if P <= dp[i][K] * 2000:
			print(i)
			break


(k, q) = map(int, input().split())
dp = [[0.0 for i in range(k + 1)] for j in range(10000)]
dp[0][0] = 1.0
for i in range(1, 10000):
	for j in range(1, k + 1):
		dp[i][j] = dp[i - 1][j] * j / k + dp[i - 1][j - 1] * (k - j + 1) / k
for t in range(q):
	p = int(input())
	for i in range(10000):
		if p <= dp[i][k] * 2000:
			print(i)
			break


def main():
	eps = 10 ** (-7)
	(k, q) = readIntArr()

	def rowFactory():
		row = [0 for _ in range(k + 1)]
		return row
	dp = [rowFactory()]
	dp[0][0] = 1
	while dp[-1][-1] <= 0.5:
		newRow = rowFactory()
		for j in range(1, k + 1):
			newRow[j] += dp[-1][j] * j / k + dp[-1][j - 1] * (k - (j - 1)) / k
		dp.append(newRow)
	n = len(dp)
	for _ in range(q):
		p = int(input())
		b = n + 1
		nTurns = 0
		while b > 0:
			while nTurns + b < n and dp[nTurns + b][k] < p / 2000:
				nTurns += b
			b //= 2
		nTurns += 1
		print(nTurns)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]
inf = float('inf')
MOD = 10 ** 9 + 7
main()


(k, q) = map(int, input().split())
t = [0] * (k + 1)
t[1] = 1
c = [0]
n = i = 1
while i < 1001:
	if 2000 * t[k] > i - 10 ** (-7):
		c.append(n)
		i += 1
	else:
		t = [0] + [(j * t[j] + (k - j + 1) * t[j - 1]) / k for j in range(1, k + 1)]
		n += 1
for i in range(q):
	print(c[int(input())])