Fast Fourier Transform, Algorithms, Pattern Matching, Convolution, Combination Sum.
'Reverse' the q matrix as follows q_new = [[0]*m2 for i in range(n2)] for i in range(n2): for j in range(m2): q_new[i][j] = q[n2-1-i][m2-1-j] # Pad each row with 0s g1 = m1 + m2 if g1 & (g1-1) != 0: b = int(math.log(g1, 2))+1 g1 = (1 << b) for i in range(n1): p[i] += [0]*(g1-m1) for i in range(n2):