No.1544 [Cherry 2nd Tune C] Synchroscope
タグ : / 解いたユーザー数 187
作問者 : 👑
Kazun
/ テスター :
Manuel1024
Problem Statement
There are two sequences $X,Y$. The sequence $X$ infinitely repeats the sequence $A$ of length $N$, and the sequence $Y$ infinitely repeats the sequence $B$ of length $M$.
More precisely, the sequence $X$ is constructed from the sequence $A$ as $X=(A_1,A_2,\dots, A_N,A_1,\dots,A_N,A_1,\dots,A_N,\dots)$. The same applies to $Y$.
If there exists a positive integer $i$ satisfying $X_i=Y_i$, output the smallest such integer. If none exists, report so.
Note that the first term of a sequence is term $1$. Also, for a sequence $Z$ and a positive integer $r$, $Z_r$ denotes the $r$-th term of the sequence $Z$.
Constraints
- $1 \leq N,M \leq 5000$
- $1 \leq A_i,B_i \leq 10^4$
- All input values are integers.
Input
The input is given from standard input in the following format.
$N$ $M$ $A_1$ $\dots$ $A_N$ $B_1$ $\dots$ $B_M$
Output
If there exists a positive integer $i$ satisfying $X_i=Y_i$, output the smallest such integer; otherwise, output $-1$. Do not forget the newline at the end.
Sample
Sample 1
Input
2 3 1 2 2 3 3
Output
4
Since $A=(1,2), B=(2,3,3)$, we have $X=(1,2,1,2,1,2,\dots), Y=(2,3,3,2,3,3,2,3,3,\dots)$. Here, $X_1 \neq Y_1, X_2 \neq Y_2, X_3 \neq Y_3, X_4=Y_4$, so the answer is $4$.
Sample 2
Input
4 1 3 9 1 5 4
Output
-1
Since no integer appears in both $X, Y$, there is no integer such that $X_i=Y_i$.
Sample 3
Input
10 7 3 4 10 8 19 1 12 39 2 5 8 100 3 38 29 4 11
Output
31
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。