QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 512 MB 満点: 100

#7990. Broadcast

統計

Little I is learning to use PyTorch, a very popular Python library for machine learning training.

Little I has noticed that PyTorch has a mechanism called "broadcasting" for tensor operations. You can think of tensors as high-dimensional arrays. For a $k$-dimensional tensor $A$, we denote its dimensions by a sequence of length $k$, $(a_1, a_2, \dots, a_k)$, meaning $A$ is an $a_1 \times a_2 \times \dots \times a_k$ tensor.

For two tensors $A$ and $B$ with dimensions $(a_1, a_2, \dots, a_m)$ and $(b_1, b_2, \dots, b_n)$ respectively, $A$ and $B$ are said to be broadcastable if and only if the following property holds:

  • For any integer $0 \le i \le \min(n, m) - 1$, either $a_{m-i} = b_{n-i}$, or at least one of $a_{m-i}$ and $b_{n-i}$ is $1$.

Little I has two tensors with dimensions $(p_1, p_2, \dots, p_m)$ and $(q_1, q_2, \dots, q_n)$, which are not necessarily broadcastable.

To fix this, Little I can perform several operations (zero or more) using built-in PyTorch functions. Each operation consists of modifying the sequence $p$ or $q$ as follows:

  • Choose either $p$ or $q$ and insert a $1$ at any position in the chosen sequence.

Little I wants to know the minimum number of operations required to make the two tensors broadcastable.

Input

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

The second line contains $m$ integers $p_1, p_2, \dots, p_m$ ($1 \le p_i \le 2000$), describing the length of each dimension of the first tensor.

The third line contains $n$ integers $q_1, q_2, \dots, q_n$ ($1 \le q_i \le 2000$), describing the length of each dimension of the second tensor.

Output

Output a single integer representing the minimum number of $1$s that must be inserted to make the two tensors broadcastable.

Examples

Input 1

4 2
2 1 3 2
4 2

Output 1

1

Note 1

By inserting a $1$ before the second position of sequence $q$ (resulting in 4 1 2), the two tensors become broadcastable.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.