-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathslides.py
61 lines (55 loc) · 1.33 KB
/
slides.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
52
53
54
55
56
57
58
59
60
61
# Copyright (c) 2016 kamyu. All rights reserved.
#
# Google Code Jam 2016 Round 1C - Problem A. Senate Evacuation
# https://code.google.com/codejam/contest/4314486/dashboard#s=p1
#
# Time: O(B^2)
# Space: O(1)
#
def slides():
B, M = map(int, raw_input().strip().split())
# The number of ways without loop is at most 2^(B-2).
# We can create the graph in this form:
# 01111
# 00111
# 00011
# 00001
# 00000
if M > (2 ** (B - 2)):
return []
# Init row 1 ~ B
# 00000
# 00111
# 00011
# 00001
# 00000
res = [[0 for _ in xrange(B)] for _ in xrange(B)]
for i in xrange(1, B):
for j in xrange(i + 1, B):
res[i][j] = 1
# We can create the graph in exact M ways in this form:
# 0????
# 00111
# 00011
# 00001
# 00000
if M == 2 ** (B - 2):
# row 1 is 01111
for j in xrange(1, B):
res[0][j] = 1
else:
# row 1 is 0???0, ??? is M in binary format.
j = -2
while M:
res[0][j] = M % 2
M /= 2
j -= 1
return res
for case in xrange(input()):
res = slides()
if not res:
print 'Case #%d: %s' % (case+1, "IMPOSSIBLE")
else:
print 'Case #%d: %s' % (case+1, "POSSIBLE")
for row in res:
print "".join(map(str, row))