Jon and Orbs (Codeforces 768D)
Tags: Math, probabilities

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 pi. 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 pi (1 ≤ pi ≤ 1000) — i-th query.

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


Sample Inputs:
1 1
1

2 2
1
2

Sample Outputs:
1

2
2

Solution (Python):
""""""
from sys import stdin
import bisect
input = stdin.readline
(k, q) = map(int, input().split())
eps = 10 ** (-7)
p_max = 0.5
f = [[0.0 for i in range(k + 1)], [0.0 for i in range(k + 1)]]
f[0][0] = 1.0
f[1][1] = 1.0
for i in range(2, 10 ** 5):
    arr = [0.0 for i in range(k + 1)]
    for j in range(1, k + 1):
        arr[j] = f[-1][j] * j / k + f[-1][j - 1] * (k - j + 1) / k
    f.append(arr)
    if arr[k] >= p_max:
        break
L = [f[i][-1] for i in range(len(f))]
for _ in range(q):
    p = int(input())
    print(bisect.bisect_right(L, p / 2000))

Solution (Python):
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()

Solution (Python):
(k, q) = 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())])