-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path002b_the_least_round_way.py
74 lines (62 loc) · 1.9 KB
/
002b_the_least_round_way.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
#! /usr/bin/python
import sys
import copy
def count_factor(num, factor):
c = 0
while num > 0 and num % factor == 0:
c += 1
num /= factor
return c
def optimal_route(trace_table, N):
route = []
i, j = N-1, N-1
while not (i == 0 and j == 0):
r = trace_table[i][j]
route.append(r)
if r == 'R':
j -= 1
else:
i -= 1
route.reverse()
return ''.join(route)
def solve(data, N, count_func):
trace_table = [ [0 for j in range(N)] for i in range(N)]
table = [ count_func(i) for i in data[0] ]
for i in range(1, len(table)):
table[i] += table[i-1]
trace_table[0][i] = 'R'
for i in range(1, N):
table[0] += count_func(data[i][0])
trace_table[i][0] = 'D'
for j in range(1, len(data[i])):
f = count_func(data[i][j])
if table[j-1] > table[j]:
table[j] += f
trace_table[i][j] = 'D'
else:
table[j] = table[j-1] + f
trace_table[i][j] = 'R'
return table[-1], optimal_route(trace_table, N)
def solve_zero(data, N):
for i in range(N):
for j in range(N):
if data[i][j] == 0:
return (1, ''.join(['R' for x in range(j)]) + ''.join(['D' for x in range(N-1)]) + ''.join(['R' for x in range(N - j - 1)]))
return None
if __name__ == '__main__':
N = int(sys.stdin.readline())
data = [ map(int, line.split()) for line in sys.stdin.readlines()]
a, route_a = solve(data, N, lambda num: count_factor(num, 2))
b, route_b = solve(data, N, lambda num: count_factor(num, 5))
ans = (-1, -1)
if a > b:
ans = (b, route_b)
else:
ans = (a, route_a)
# Special case, matrix may contain 0
if a > 0 and b > 0:
r = solve_zero(data, N)
if r is not None:
ans = r
print ans[0]
print ans[1]