問題一覧 > 通常問題

No.1544 [Cherry 2nd Tune C] Synchroscope

レベル : / AI推定難易度 : 600 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 187
作問者 : 👑 Kazun / テスター : Manuel1024
お気に入りにしたユーザー ProblemId : 6148 / 自分の提出
問題文最終更新日: 2021-06-14 02:56:53
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 299 (BANNED CONTEST) (順位表) の他の問題:

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もしくは右上の雲マークをクリックしてアカウントを作成してください。