#P9914. Meeting Points
Meeting Points
Meeting Points
There are \(n + m\) points on the Cartesian plane. In particular, there are two types of points:
- \(n\) points of type \(\rm A\), which are initially at \( (1, 0), (2, 0), \dots, (n, 0) \).
- \(m\) points of type \(\rm B\), which are initially at \( (0, 1), (0, 2), \dots, (0, m) \).
At time \(t = 0\), both groups begin to move simultaneously.
- The \(i\)-th \(\rm A\) point moves upward (positive y-direction) with a constant speed of \(a_i\) units per second. (If \(a_i = 0\), it remains stationary.)
- The \(j\)-th \(\rm B\) point moves rightward (positive x-direction) with a constant speed of \(b_j\) units per second. (If \(b_j = 0\), it remains stationary.)
A meeting occurs if at some time \(t\) an \(\rm A\) point and a \(\rm B\) point occupy the same coordinate. Suppose the \(i\)-th \(\rm A\) point is at \((i, a_i \cdot t)\) and the \(j\)-th \(\rm B\) point is at \((b_j \cdot t, j)\). They meet if:
[ \begin{cases} i = b_j \cdot t, \ a_i \cdot t = j. \end{cases} ]
From the above equations, for a meeting to occur, there must exist a positive time \(t\) such that:
[ \frac{i}{b_j} = \frac{j}{a_i} \quad \Longleftrightarrow \quad i\cdot a_i = j\cdot b_j, ]
Note that if \(a_i=0\) or \(b_j=0\), the corresponding point remains stationary and will never meet any other point since their initial positions are different.
Your task is to count the number of pairs \((i, j)\) (with \(1 \le i \le n\) and \(1 \le j \le m\)) such that the \(i\)-th \(\rm A\) point and the \(j\)-th \(\rm B\) point will ever meet.
Input Format: The input consists of three lines.
inputFormat
The first line contains two integers \(n\) and \(m\) representing the number of \(\rm A\) points and \(\rm B\) points respectively.
The second line contains \(n\) integers: \(a_1, a_2, \dots, a_n\) where \(a_i\) is the speed of the \(i\)-th \(\rm A\) point.
The third line contains \(m\) integers: \(b_1, b_2, \dots, b_m\) where \(b_j\) is the speed of the \(j\)-th \(\rm B\) point.
outputFormat
Output a single integer representing the number of pairs \((i, j)\) that will meet at some moment.
sample
3 3
1 2 3
1 2 3
3