#P10043. [CCPC 2023 北京市赛] 广播

[CCPC 2023 北京市赛] 广播

Problem Description

Xiao I is learning to use Pytorch. It is a very popular Python library for machine learning training.

Xiao I noticed that Pytorch has a mechanism for tensor operations called "broadcast". You can think of a tensor as a high-dimensional array. For a kk-dimensional tensor AA, we use a sequence of length kk, (a1,a2,⋯ ,ak)(a_1,a_2,\cdots,a_k), to represent the size of each dimension. That is, AA is a tensor of shape a1×a2×⋯×aka_1 \times a_2 \times \cdots \times a_k.

For two tensors AA and BB, suppose their dimensions are (a1,a2,⋯ ,am)(a_1,a_2,\cdots,a_m) and (b1,b2,⋯ ,bn)(b_1,b_2,\cdots,b_n) respectively. We say AA and BB are broadcastable if and only if the following property holds:

  • For any integer 0≤i≤min⁡(n,m)−10 \le i \le \min(n,m) - 1, either am−i=bn−ia_{m-i} = b_{n-i}, or at least one of am−ia_{m-i} and bn−ib_{n-i} is 11.

Now Xiao I has two tensors with dimensions (p1,p2,⋯ ,pm)(p_1,p_2,\cdots,p_m) and (q1,q2,⋯ ,qn)(q_1,q_2,\cdots,q_n). They are not necessarily broadcastable.

So, Xiao I can use Pytorch built-in functions to perform some operations (possibly none). In each operation, he modifies sequence pp or qq as follows:

  • Choose pp or qq, and insert a 11 at any position in the chosen sequence.

Xiao I wants to know the minimum number of operations needed to make the two tensors broadcastable.

Input Format

The first line contains two integers m,n(1≤m,n≤2000)m,n(1 \le m,n \le 2000), representing the number of dimensions of the two tensors.

The second line contains mm integers p1,p2,⋯ ,pm(1≤pi≤2000)p_1,p_2,\cdots,p_m (1 \le p_i \le 2000), describing the length of each dimension of the first tensor.

The third line contains nn integers q1,q2,⋯ ,qn(1≤qi≤2000)q_1,q_2,\cdots,q_n (1 \le q_i \le 2000), describing the length of each dimension of the second tensor.

Output Format

Output one integer in a single line, representing the minimum number of inserted 11's needed to make the two tensors broadcastable.

4 2
2 1 3 2
4 2
1

Hint

Insert a 11 before the second position of sequence qq (getting 4 1 2), and then the two tensors will become broadcastable.

Translated by ChatGPT 5