-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline_up_the_captives.py~
129 lines (101 loc) · 2.99 KB
/
line_up_the_captives.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def answer(x, y, n):
import math
import scipy
from scipy import special
import gc
def grow_tree(tr, level_num, max_level):
tree_level_list = tr[level_num - 1]
if level_num < max_level:
for index_l, l in enumerate(tree_level_list):
number_left = l[2]
if number_left != 0 and len(l[0]) !=1:
for index_count, count in enumerate(l[0]):
if index_count == 0:
gc.disable()
tr[level_num].append( [ [0],
scipy.special.binom(number_left - 1, count) * math.factorial(number_left - 1 - count),
0, index_l] )
gc.enable()
else:
if count > max_level - level_num:
g.disable()
tr[level_num].append( [ range(max_level - level_num - 1, count),
scipy.special.binom(number_left - 1, count) * math.factorial(number_left - 1 - count),
count, index_l] )
gc.enable()
elif level_num == max_level:
for index_l, l in enumerate(tree_level_list):
number_left = l[2]
if number_left != 0:
gc.disable()
tr[level_num].append( [ [0], math.factorial(number_left - 1),
0, index_l ] )
gc.enable()
def count_tree(tr):
tr = [l for l in tr if l]
tree_len = len(tr)
for i in range(0, tree_len - 1)[::-1]:
for node_index, node in enumerate(tr[i]):
child_node_count_list = [l[1] for l in tr[i + 1] if l[3] == node_index]
if child_node_count_list:
child_sum = sum( child_node_count_list )
node[1] *= child_sum
return tr[0][0][1]
def one_side(num, z):
tree = []
for i in range(0, z + 1):
tree.append([])
if z == 1:
tree[0] = [ [ [0], 1, num, 0 ] ]
else:
tree[0] = [ [ range(z - 1, num), 1, num, 0 ] ]
for i in range(1, z + 1):
print i
grow_tree(tree, i, z)
return count_tree(tree)
arrangements = []
if x + y > n + 1:
return str(0)
if x == 1 and y != 1:
ans = one_side(n - 1, y - 1)
ans = str(int(ans))
return ans
elif y == 1 and x != 1:
ans = one_side(n - 1, x - 1)
ans = str(int(ans))
return ans
elif x == 1 and y == 1 and n != 1:
return str(0)
else:
for j in range(x - 1, n - y + 1):
# print j
left_arrs = one_side(j, x - 1)
right_arrs = one_side(n - 1 - j, y -1)
arrangements.append(left_arrs * right_arrs * scipy.special.binom(n - 1, j))
ans = str(int(sum(arrangements)))
return ans
print answer(9, 10, 30)
"""
def answer2(x, y, n):
import itertools
perms = list(itertools.permutations(range(0, n)))
counter = 0
for p in perms:
visible_x = 1
visible_y = 1
for i in range(1, n):
if p[i] > max(p[:i]):
visible_x += 1
for j in range(0, n - 1)[::-1]:
if p[j] > max(p[j + 1:]):
visible_y += 1
if visible_x == x and visible_y == y:
counter += 1
counter = str(counter)
return counter
for n in range(3, 8):
for x in range(1, 8):
for y in range(1, 8):
if answer(x, y, n) != answer2(x, y, n):
print "Does not match on (x, y, n): ",x,y,n,'\t',"ans1, ans2(correct): ",answer(x,y,n),answer2(x,y,n)
"""