Periodic Sequence

시간 제한1초메모리 제한2048 MB

요약
두 자릿수 수열이 주어질 때, 무한 반복한 수열이 순환 이동으로 같아질 수 있는지 판별한다.
난이도

보통10점 중 6점

유형
문자열, 문자열 매칭
정답자
아직 제출이 없습니다

문제

As you may know, some fractions of integers AB\frac{A}{B} result in an infinite periodic decimal representation. For example, 47\frac{4}{7} results in 0.57142857‾0.57\overline{142857}, which means that the 142857142857 part is repeating itself. Note that we could also write 0.5714285714‾0.5714\overline{285714} or 0.571428571428571‾0.571\overline{428571428571}. As you see, it is not trivial to check if two such sequences are equal.

Therefore, you have to help us. You are given only the periodic part of two sequences and need to check if they are equal. Note that the periodic parts are considered equal if they can be made equal by repetition and cyclic shifting.

입력

The first line contains two integers nn and mm (1≤n,m≤5⋅1051 \leq n, m \leq 5 \cdot 10^5) --- the length of the first and second sequence.

The second line contains nn integers a_ia\_i (0≤a_i<100 \leq a\_i < 10) --- the first sequence.

The third line contains mm integers b_ib\_i (0≤b_i<100 \leq b\_i < 10) --- the second sequence.

출력

Print YES if the two sequences are equal and NO if they are not.

예제2

  1. 예제 1

    입력
    6 3
    1 5 6 1 5 6
    6 1 5
    
    예상 출력
    YES
    
  2. 예제 2

    입력
    7 3
    1 5 6 1 5 6 7
    5 6 7
    
    예상 출력
    NO