1.Description
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, where ε < 10 - 7.
To better prepare himself, he wants to know the answer for q different values of p i p_{i} pi. Since he is busy designing the battle strategy with Sam, he asks you for your help.
2.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 p_{i} pi (1 ≤ p i p_{i} pi ≤ 1000) — i-th query.
3.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
4.Ideas
This is a typical probabilistic dynamic programming problem.Thinking about j-th day, i distinct orbs are produced.The probability of that is donated as d p i , j dp_{i,j} dpi,j.
In order to spawn i distinct orbs on j-th day, there are only two cases on (j-1)-th day:
- Case-1: i orbs have been produced and new orb hasn’t spawned then the probability of that is i k ⋅ d p i , j − 1 \frac{i}{k} \cdot dp_{i,j-1} ki⋅dpi,j−1
- Case-2: i-1 orbs have been produced and new orb is spawned then the probability of that is k − ( i + 1 ) k ⋅ d p i − 1 , j − 1 \frac{k-(i+1)}{k} \cdot dp_{i-1,j-1} kk−(i+1)⋅dpi−1,j−1
Then the state transition equation is:
d
p
i
,
j
=
i
k
⋅
d
p
i
,
j
−
1
+
k
−
(
i
+
1
)
k
⋅
d
p
i
−
1
,
j
−
1
dp_{i,j} = \frac{i}{k} \cdot dp_{i,j-1} + \frac{k-(i+1)}{k} \cdot dp_{i-1,j-1}
dpi,j=ki⋅dpi,j−1+kk−(i+1)⋅dpi−1,j−1
We need to find the minimum j s.t. d p k , j ≥ p i − ϵ 2000 dp_{k,j} \geq \frac{p_{i}-\epsilon}{2000} dpk,j≥2000pi−ϵ
where
i for number of distinct orbs Jon has till now.
j for number of day Jon waited.
k for number of different kinds of orbs.
5.Code
k, q = map(lambda x : int(x), input().split())
p_list = [eval(input()) for i in range(q)]
p_list_sort = [y for y in p_list]
p_list_sort.sort()
dp = [[0 for i in range(k+1)]]
dp[0][0] = 1
eps = 1e-7
day_list = [0 for i in range(q)]
i, loc = 0, 0
while True: # j for number of day Jon waited
i += 1
dp.append([0 for m in range(k+1)])
for j in range(1, k+1): # i for number of distinct orbs Jon has till now
"""dp[i][j] for probability of Jon having i distinct orbs in j days"""
dp[i][j] = j / k * dp[i-1][j] + (k-(j-1)) / k * dp[i-1][j-1]
for p in range(loc, q):
if p_list_sort[p] - eps < dp[i][-1] * 2000:
day_list[p_list.index(p_list_sort[p])] = i
loc += 1
else:
break
if loc == q:
break
#for i in dp:
# print(i)
for i in day_list:
print(i)