概率DP——CF 768 D - Jon and Orbs (python)

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} kidpi,j1
  • 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)dpi1,j1

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=kidpi,j1+kk(i+1)dpi1,j1

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,j2000piϵ

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值