Easily Broadcastable Tensors
InterviewTime limit1sMemory limit2048 MB
Find the fewest 1s to insert into two size lists so that, aligned from the right, every overlapping pair is equal or contains a 1.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Little I is learning to use PyTorch, a very popular Python library for machine learning training.
Little I noticed that PyTorch has a mechanism for tensor operations called 'broadcasting'. You can think of tensors as multidimensional arrays. For a -dimensional tensor , a sequence of length , written , gives the lengths of its dimensions. So is a tensor of size .
For two tensors and , with dimensions and , and are easily broadcastable if and only if the following holds: for every integer , either , or at least one of and is .
Little I has two tensors with dimensions and , and they may not be easily broadcastable.
Little I can perform operations (or none) to make them easily broadcastable. Each operation chooses or and inserts a at any position in the chosen sequence.
Find the minimum number of operations required to make the two tensors easily broadcastable.
Input
The first line contains two integers and (), the dimensions of the two tensors.
The second line contains integers (), the length of each dimension of the first tensor.
The third line contains integers (), the length of each dimension of the second tensor.
Output
Print a single integer: the minimum number of insertions of required to make the two tensors easily broadcastable.
Hint
In the sample, inserting a before the second position of sequence gives , which makes the two tensors easily broadcastable.