#P10043. Minimum Insertions for Broadcastable Tensors
Minimum Insertions for Broadcastable Tensors
Minimum Insertions for Broadcastable Tensors
In this problem, you are given the shapes of two tensors represented by two sequences of positive integers. For a tensor with dimensions A given by \((a_1,a_2,\cdots,a_k)\), it represents a tensor of shape \(a_1 \times a_2 \times \cdots \times a_k\). In PyTorch and many machine learning frameworks, tensors can be broadcast together if they satisfy a specific condition.
Two tensors with shapes \((p_1,p_2,\cdots,p_m)\) and \((q_1,q_2,\cdots,q_n)\) are broadcastable if and only if for every integer \(0 \le i \le \min(m,n)-1\), the following holds: \[ \text{Either } p_{m-i} = q_{n-i} \quad \text{or} \quad p_{m-i} = 1 \quad \text{or} \quad q_{n-i} = 1. \]
Sometimes the given tensor shapes are not broadcastable. To remedy this, you are allowed to perform an operation any number of times where you choose one of the sequences and insert a \(1\) at any position in that sequence. Your task is to determine the minimum number of operations required to make the two tensors broadcastable.
inputFormat
The first line contains two integers \(m\) and \(n\) \((1 \le m,n \le 500)\) representing the number of dimensions of the first and second tensor respectively.
The second line contains \(m\) positive integers \(p_1, p_2, \dots, p_m\) representing the dimensions of the first tensor.
The third line contains \(n\) positive integers \(q_1, q_2, \dots, q_n\) representing the dimensions of the second tensor.
outputFormat
Output a single integer representing the minimum number of operations (insertions of \(1\)) required to make the two tensors broadcastable.
sample
2 2
3 4
1 4
0