-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnew-lottery-game.py
51 lines (44 loc) · 1.58 KB
/
new-lottery-game.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
42
43
44
45
46
47
48
49
50
51
# Copyright (c) 2016 kamyu. All rights reserved.
#
# Google Code Jam 2014 Round 1B - Problem B. New Lottery Game
# https://code.google.com/codejam/contest/2994486/dashboard#s=p1
#
# Time: O(log(max(A, B)))
# Space: O(log(max(A, B)))
#
lookup = {}
def f(A, B, K):
if (A, B, K) in lookup:
return lookup[(A, B, K)]
# f(A, B, K) = 0 if 0 in (A, B, K)
if A == 0 or B == 0 or K == 0:
lookup[(A, B, K)] = 0
return 0
# f(1, 1, K) = 1
if A == B == 1:
lookup[(A, B, K)] = 1
return 1
# f(A, B, K) is the sum of the sizes of the following 4 sets:
# 1. {(a/2, b/2) | (a, b) in (S(A, B, K) && a even && b even)}
# = S(ceil(A/2), ceil(B/2), ceil(K/2))
# 2. {(a/2, (b-1)/2) | (a, b) in (S(A, B, K) && a even && b odd)}
# = S(ceil(A/2), floor(B/2), ceil(K/2))
# 3. {((a-1)/2, b/2) | (a, b) in (S(A, B, K) && a odd && b even)}
# = S(floor(A/2), ceil(B/2), ceil(K/2))
# 4. {((a-1)/2, (b-1)/2) | (a, b) in (S(A, B, K) && a odd && b odd)}
# = S(floor(A/2), floor(B/2), floor(K/2))
#
# Besides:
# - ceil(A/2) = (A+1) / 2
# - floor(A/2) = A / 2
#
lookup[(A, B, K)] = f((A+1)>>1, (B+1)>>1, (K+1)>>1) + \
f((A+1)>>1, B>>1, (K+1)>>1) + \
f(A>>1, (B+1)>>1, (K+1)>>1) + \
f(A>>1, B>>1, K>>1)
return lookup[(A, B, K)]
def new_lottery_game():
A, B, K = map(int, raw_input().strip().split())
return f(A, B, K)
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, new_lottery_game())