In [185]:
"""
Need to calculate the number of 2s and 5s in the grid because that leads to number of trailing zeros.
Using DP so that same path is not calculated multiple number of times
min num of 2/5 will be current factors plus min of previous row or previous column
min_zeros is the main function
min_trail_zeros is the main logic
"""


Out[185]:
'\nNeed to calculate the number of 2s and 5s in the grid because that leads to number of trailing zeros.\nUsing DP so that same path is not calculated multiple number of times\nmin num of 2/5 will be current factors plus min of previous row or previous column\nmin_zeros is the main function\nmin_trail_zeros is the main logic\n'

In [5]:
import numpy as np

In [83]:
m, n = map(int, input().strip().split())


20 20

In [84]:
grid = []
for i in range(m):
    row = list(map(int, input().strip().split()))
    assert(len(row) == n)
    grid.append(row)
grid = np.array(grid)


100 54 66 10 65 19 48 87 97 74 69 78 74 22 13 75 41 51 16 13
6 37 20 96 90 21 38 2 10 87 85 48 67 5 47 13 22 42 81 44
1 5 36 20 69 57 18 48 11 73 16 54 32 56 63 90 42 66 54 20
30 96 10 37 92 1 2 35 81 47 19 14 95 47 68 78 46 34 18 75
15 97 9 95 98 44 30 77 33 48 90 50 39 100 97 5 97 27 47 75
30 84 24 83 42 62 86 25 96 5 12 25 100 47 35 15 39 30 39 9
8 76 24 16 5 97 89 11 40 41 57 84 95 84 66 72 4 70 12 82
34 92 50 85 72 11 38 4 68 85 70 98 51 82 83 99 67 22 97 31
18 92 47 78 74 88 46 69 80 15 26 63 43 58 6 88 34 62 7 64
9 55 89 12 22 26 100 85 44 23 40 67 15 28 44 68 35 63 28 5
24 91 63 33 30 63 50 6 6 27 55 82 19 81 1 98 12 74 93 15
52 47 31 73 73 40 97 94 65 7 41 32 21 43 22 48 99 93 17 39
79 45 5 64 54 88 29 48 45 69 75 86 2 38 73 64 34 31 7 18
91 4 97 13 84 12 20 3 17 29 72 60 35 70 25 88 30 42 58 91
21 24 98 75 97 71 72 9 29 94 35 62 17 39 45 3 6 88 36 44
46 63 78 71 5 19 56 79 17 15 57 57 96 74 83 83 51 94 27 15
20 78 20 63 82 63 22 13 97 62 28 10 87 83 34 39 90 90 77 38
63 25 42 32 89 69 87 69 58 43 93 60 70 23 67 91 91 89 46 58
30 81 51 47 88 74 88 6 7 35 29 18 100 13 99 78 29 17 85 24
34 24 35 79 50 2 100 46 36 87 1 38 64 13 58 33 62 94 44 35

In [98]:
grid


Out[98]:
array([[100,  54,  66,  10,  65,  19,  48,  87,  97,  74,  69,  78,  74,
         22,  13,  75,  41,  51,  16,  13],
       [  6,  37,  20,  96,  90,  21,  38,   2,  10,  87,  85,  48,  67,
          5,  47,  13,  22,  42,  81,  44],
       [  1,   5,  36,  20,  69,  57,  18,  48,  11,  73,  16,  54,  32,
         56,  63,  90,  42,  66,  54,  20],
       [ 30,  96,  10,  37,  92,   1,   2,  35,  81,  47,  19,  14,  95,
         47,  68,  78,  46,  34,  18,  75],
       [ 15,  97,   9,  95,  98,  44,  30,  77,  33,  48,  90,  50,  39,
        100,  97,   5,  97,  27,  47,  75],
       [ 30,  84,  24,  83,  42,  62,  86,  25,  96,   5,  12,  25, 100,
         47,  35,  15,  39,  30,  39,   9],
       [  8,  76,  24,  16,   5,  97,  89,  11,  40,  41,  57,  84,  95,
         84,  66,  72,   4,  70,  12,  82],
       [ 34,  92,  50,  85,  72,  11,  38,   4,  68,  85,  70,  98,  51,
         82,  83,  99,  67,  22,  97,  31],
       [ 18,  92,  47,  78,  74,  88,  46,  69,  80,  15,  26,  63,  43,
         58,   6,  88,  34,  62,   7,  64],
       [  9,  55,  89,  12,  22,  26, 100,  85,  44,  23,  40,  67,  15,
         28,  44,  68,  35,  63,  28,   5],
       [ 24,  91,  63,  33,  30,  63,  50,   6,   6,  27,  55,  82,  19,
         81,   1,  98,  12,  74,  93,  15],
       [ 52,  47,  31,  73,  73,  40,  97,  94,  65,   7,  41,  32,  21,
         43,  22,  48,  99,  93,  17,  39],
       [ 79,  45,   5,  64,  54,  88,  29,  48,  45,  69,  75,  86,   2,
         38,  73,  64,  34,  31,   7,  18],
       [ 91,   4,  97,  13,  84,  12,  20,   3,  17,  29,  72,  60,  35,
         70,  25,  88,  30,  42,  58,  91],
       [ 21,  24,  98,  75,  97,  71,  72,   9,  29,  94,  35,  62,  17,
         39,  45,   3,   6,  88,  36,  44],
       [ 46,  63,  78,  71,   5,  19,  56,  79,  17,  15,  57,  57,  96,
         74,  83,  83,  51,  94,  27,  15],
       [ 20,  78,  20,  63,  82,  63,  22,  13,  97,  62,  28,  10,  87,
         83,  34,  39,  90,  90,  77,  38],
       [ 63,  25,  42,  32,  89,  69,  87,  69,  58,  43,  93,  60,  70,
         23,  67,  91,  91,  89,  46,  58],
       [ 30,  81,  51,  47,  88,  74,  88,   6,   7,  35,  29,  18, 100,
         13,  99,  78,  29,  17,  85,  24],
       [ 34,  24,  35,  79,  50,   2, 100,  46,  36,  87,   1,  38,  64,
         13,  58,  33,  62,  94,  44,  35]])

In [129]:
m = 3
n = 3
grid1 = np.array(
[
    [10, 12, 14],
[15, 30, 16],
[12, 10, 20]
    
]
)

In [85]:
def count_factor(num):
    count = [0, 0]
    factor = [2, 5]
    for i in range(len(factor)):
        while num % factor[i] == 0:
            count[i] += 1
            num = int(num/factor[i])
    return count

In [158]:
def min_zero_path(tuple_1, tuple_2):
    zeros_1 = min(tuple_1)
    zeros_2 = min(tuple_2)
    min_2 = tuple_1[0] if (tuple_1[0] < tuple_2[0]) else tuple_2[0]
    min_5 = tuple_1[1] if (tuple_1[1] < tuple_2[1]) else tuple_2[1]
    return min_2, min_5

In [87]:
def sum_tuples(tuple1, tuple2):
    return tuple1[0] + tuple2[0], tuple1[1] + tuple2[1]

In [88]:
# def min_grid_zeros(grid, i, j, twos, fives):
#     if i >= grid.shape[0]  j >= grid.shape[1]:
#         return twos, fives
#     else:
#         twos, fives = twos + count_factor(grid[i, j], 2), fives + count_factor(grid[i, j], 5)
#         print(twos, fives, i, j)
#         twos_r, fives_r = min_grid_zeros(grid, i, j+1, twos, fives)
#         zeros_r = min(twos_r, fives_r)
#         twos_d, fives_d = min_grid_zeros(grid, i+1, j, twos, fives)
#         zeros_d = min(twos_d, fives_d)
#         if zeros_d < zeros_r:
#             return twos_d, fives_d
#         else:
#             return twos_r, fives_r

In [92]:
# def min_grid_zeros(grid, i, j, m, n, twos, fives):
#     print(i, j)
#     if i == m-1 and j == n-1:
#         return count_factor(grid[i, j])
#     elif i == m - 1:
#         return sum_tuples(count_factor(grid[i, j]), min_grid_zeros(grid, i, j+1, m, n , twos, fives))
#     elif j == n - 1:
#         return sum_tuples(count_factor(grid[i, j]), min_grid_zeros(grid, i+1, j, m, n , twos, fives))
#     else:
#         return min_zero_path(
#             sum_tuples(count_factor(grid[i, j]), min_grid_zeros(grid, i+1, j, m, n , twos, fives)),
#             sum_tuples(count_factor(grid[i, j]), min_grid_zeros(grid, i, j+1, m, n , twos, fives))
#         )

In [93]:
# def res_min_grid_zeros(grid):
#     return min(min_grid_zeros(grid, 0, 0, grid.shape[0], grid.shape[1], 0 , 0))

In [100]:
# res_min_grid_zeros(grid1)


0 0
1 0
2 0
2 1
2 2
2 3
1 1
2 1
2 2
2 3
1 2
2 2
2 3
1 3
2 3
0 1
1 1
2 1
2 2
2 3
1 2
2 2
2 3
1 3
2 3
0 2
1 2
2 2
2 3
1 3
2 3
0 3
1 3
2 3
Out[100]:
3

In [159]:
def min_trail_zeros(grid):
    m, n = grid.shape
    t2s = np.zeros((m,n))
    t5s = np.zeros((m,n))
    twos, fives = count_factor(grid[0, 0])
    t2s[0, 0] = twos
    t5s[0, 0] = fives
    for j in range(1, n):
        t2s[0, j], t5s[0, j] = sum_tuples((t2s[0, j-1], t5s[0, j-1]), count_factor(grid[0, j]))
    for i in range(1, m):
        t2s[i, 0], t5s[i, 0] = sum_tuples((t2s[i-1, 0], t5s[i-1, 0]), count_factor(grid[i, 0]))
    for i in range(1, m):
        for j in range(1, n):
            temp = count_factor(grid[i, j])
            t2s[i, j], t5s[i, j] = min_zero_path(
                sum_tuples((t2s[i-1, j], t5s[i-1, j]), temp),
                sum_tuples((t2s[i, j-1], t5s[i, j-1]), temp)
            )
    return t2s, t5s

In [171]:
t, f = min_trail_zeros(grid)

In [182]:
def min_zeros(grid):
    t, f = min_trail_zeros(grid)
    m, n = grid.shape
    return int(min(t[m-1, n-1], f[m-1, n-1]))

In [184]:
min_zeros(grid1)


Out[184]:
2

In [175]:
import pandas as pd
x = pd.DataFrame(grid)

In [176]:
x


Out[176]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 100 54 66 10 65 19 48 87 97 74 69 78 74 22 13 75 41 51 16 13
1 6 37 20 96 90 21 38 2 10 87 85 48 67 5 47 13 22 42 81 44
2 1 5 36 20 69 57 18 48 11 73 16 54 32 56 63 90 42 66 54 20
3 30 96 10 37 92 1 2 35 81 47 19 14 95 47 68 78 46 34 18 75
4 15 97 9 95 98 44 30 77 33 48 90 50 39 100 97 5 97 27 47 75
5 30 84 24 83 42 62 86 25 96 5 12 25 100 47 35 15 39 30 39 9
6 8 76 24 16 5 97 89 11 40 41 57 84 95 84 66 72 4 70 12 82
7 34 92 50 85 72 11 38 4 68 85 70 98 51 82 83 99 67 22 97 31
8 18 92 47 78 74 88 46 69 80 15 26 63 43 58 6 88 34 62 7 64
9 9 55 89 12 22 26 100 85 44 23 40 67 15 28 44 68 35 63 28 5
10 24 91 63 33 30 63 50 6 6 27 55 82 19 81 1 98 12 74 93 15
11 52 47 31 73 73 40 97 94 65 7 41 32 21 43 22 48 99 93 17 39
12 79 45 5 64 54 88 29 48 45 69 75 86 2 38 73 64 34 31 7 18
13 91 4 97 13 84 12 20 3 17 29 72 60 35 70 25 88 30 42 58 91
14 21 24 98 75 97 71 72 9 29 94 35 62 17 39 45 3 6 88 36 44
15 46 63 78 71 5 19 56 79 17 15 57 57 96 74 83 83 51 94 27 15
16 20 78 20 63 82 63 22 13 97 62 28 10 87 83 34 39 90 90 77 38
17 63 25 42 32 89 69 87 69 58 43 93 60 70 23 67 91 91 89 46 58
18 30 81 51 47 88 74 88 6 7 35 29 18 100 13 99 78 29 17 85 24
19 34 24 35 79 50 2 100 46 36 87 1 38 64 13 58 33 62 94 44 35

In [180]:
y = pd.DataFrame(t)
y


Out[180]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 2.0 3.0 4.0 5.0 5.0 5.0 9.0 9.0 9.0 10.0 10.0 11.0 12.0 13.0 13.0 13.0 13.0 13.0 17.0 17.0
1 3.0 3.0 5.0 10.0 6.0 5.0 6.0 7.0 8.0 8.0 8.0 12.0 12.0 12.0 12.0 12.0 13.0 14.0 14.0 16.0
2 3.0 3.0 5.0 7.0 6.0 5.0 6.0 10.0 8.0 8.0 12.0 13.0 17.0 15.0 12.0 13.0 14.0 15.0 15.0 17.0
3 4.0 8.0 6.0 6.0 8.0 5.0 6.0 6.0 6.0 6.0 6.0 7.0 7.0 7.0 9.0 10.0 11.0 12.0 13.0 13.0
4 4.0 4.0 4.0 4.0 5.0 7.0 7.0 6.0 6.0 10.0 7.0 8.0 7.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0
5 5.0 6.0 7.0 4.0 5.0 6.0 7.0 6.0 11.0 10.0 9.0 8.0 9.0 9.0 9.0 9.0 9.0 10.0 9.0 9.0
6 8.0 8.0 10.0 8.0 5.0 5.0 5.0 5.0 8.0 8.0 8.0 10.0 9.0 11.0 10.0 12.0 11.0 11.0 11.0 10.0
7 9.0 10.0 11.0 8.0 8.0 5.0 6.0 7.0 9.0 8.0 9.0 10.0 9.0 10.0 10.0 10.0 10.0 11.0 11.0 10.0
8 10.0 12.0 11.0 9.0 9.0 8.0 7.0 7.0 11.0 8.0 9.0 9.0 9.0 10.0 11.0 13.0 11.0 12.0 11.0 16.0
9 10.0 10.0 10.0 11.0 10.0 9.0 9.0 7.0 9.0 8.0 11.0 9.0 9.0 11.0 13.0 15.0 11.0 11.0 13.0 13.0
10 13.0 10.0 10.0 10.0 11.0 9.0 10.0 8.0 9.0 8.0 8.0 9.0 9.0 9.0 9.0 10.0 12.0 12.0 12.0 12.0
11 15.0 10.0 10.0 10.0 10.0 12.0 10.0 9.0 9.0 8.0 8.0 13.0 9.0 9.0 10.0 14.0 12.0 12.0 12.0 12.0
12 15.0 10.0 10.0 16.0 11.0 14.0 10.0 13.0 9.0 8.0 8.0 9.0 10.0 10.0 10.0 16.0 13.0 12.0 12.0 13.0
13 15.0 12.0 10.0 10.0 12.0 14.0 12.0 12.0 9.0 8.0 11.0 11.0 10.0 11.0 10.0 13.0 14.0 13.0 13.0 13.0
14 15.0 15.0 11.0 10.0 10.0 10.0 13.0 12.0 9.0 9.0 9.0 10.0 10.0 10.0 10.0 10.0 11.0 14.0 15.0 15.0
15 16.0 15.0 12.0 10.0 10.0 10.0 13.0 12.0 9.0 9.0 9.0 9.0 14.0 11.0 10.0 10.0 10.0 11.0 11.0 11.0
16 18.0 16.0 14.0 10.0 11.0 10.0 11.0 11.0 9.0 10.0 11.0 10.0 10.0 10.0 11.0 10.0 11.0 12.0 11.0 12.0
17 18.0 16.0 15.0 15.0 11.0 10.0 10.0 10.0 10.0 10.0 10.0 12.0 11.0 10.0 10.0 10.0 10.0 10.0 11.0 12.0
18 19.0 16.0 15.0 15.0 14.0 11.0 13.0 11.0 10.0 10.0 10.0 11.0 13.0 10.0 10.0 11.0 10.0 10.0 10.0 13.0
19 20.0 19.0 15.0 15.0 15.0 12.0 14.0 12.0 12.0 10.0 10.0 11.0 17.0 10.0 11.0 11.0 11.0 11.0 12.0 12.0

In [181]:
z = pd.DataFrame(f)
z


Out[181]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 2.0 2.0 2.0 3.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 6.0 6.0 6.0 6.0 6.0
1 2.0 2.0 3.0 3.0 4.0 4.0 4.0 4.0 5.0 4.0 5.0 4.0 4.0 5.0 4.0 4.0 4.0 4.0 4.0 4.0
2 2.0 3.0 3.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 5.0 4.0 4.0 4.0 5.0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