-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathallergy-testing.py
41 lines (37 loc) · 988 Bytes
/
allergy-testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright (c) 2015 kamyu. All rights reserved.
#
# Google Code Jam 2014 World Finals - Problem E. Allergy Testing
# https://code.google.com/codejam/contest/7214486/dashboard#s=p4
#
# Time: O((logN)^3)
# Space: O(1)
#
# Combinatorial Solution
#
def nCk(n, k):
if k > n:
return 0
res = 1
for i in xrange(1, min(n - k, k) + 1):
res = res * (n - i + 1) // i
return res
def max_foods(D, A, B):
cnt = 0
for L in xrange(min(51, D // B + 1)):
K_min = (D - L * B - B) // A + 1
K_max = (D - L * B) // A
cnt += nCk(K_max + L + 1, L + 1) - nCk(K_min + L, L + 1)
return cnt
def min_days(N, A, B):
left = 0
right = int(1e15)
while left < right:
D = (left + right) // 2
if max_foods(D, A, B) >= N:
right = D
else:
left = D + 1
return left
for case in xrange(input()):
print "Case #%d: %d" % (case+1, \
min_days(*map(int, raw_input().strip().split())))