-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathK Candy Store.py
More file actions
49 lines (40 loc) · 1.37 KB
/
K Candy Store.py
File metadata and controls
49 lines (40 loc) · 1.37 KB
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
42
43
44
45
46
47
48
49
"""
Problem Statement
Jim enters a candy shop which has N different types of candies, each candy is of the same price. Jim has enough money to
buy K candies. In how many different ways can he purchase K candies if there are infinite candies of each kind?
"""
MOD = 10 ** 9
__author__ = 'Danyang'
class Solution(object):
def __init__(self):
"""
\binom nk=\binom{n-1}{k-1}+\binom{n-1}k, \text{ for } 0<k<n,
"""
self.C = [[1 for _ in xrange(2000)] for _ in xrange(2000)]
for n in xrange(1, 2000):
for k in xrange(1, n): # 0<k<n
self.C[n][k] = self.C[n - 1][k - 1] + self.C[n - 1][k]
def solve(self, cipher):
"""
Abstract: N types, K holes, repeatable
nCk when items are duplicated
reference:
dp algorithm: https://github.com/derekhh/HackerRank/blob/master/k-candy-store.cpp
:param cipher: the cipher
"""
N, K = cipher
return self.C[N + K - 1][K] % MOD
if __name__ == "__main__":
import sys
f = open("1.in", "r")
# f = sys.stdin
testcases = int(f.readline().strip())
solution = Solution()
for t in xrange(testcases):
# construct cipher
N = int(f.readline().strip())
K = int(f.readline().strip())
cipher = N, K
# solve
s = "%s\n" % (solution.solve(cipher))
print s,