Dihedral Group

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

요약
정n각형의 시계 방향 레이블과 시험 수열이 주어질 때, 회전이나 반사를 적용해 시험 수열이 연속한 호로 나타나는지 판별한다.
난이도

보통10점 중 6점

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

문제

In mathematics, the dihedral group D_nD\_n is the group of symmetries of a regular nn-gon. Rotations and reflections are elements of D_nD\_n, and in fact all elements of the dihedral group can be expressed as a series of rotations and reflections. Elements of D_nD\_n act on the nn-gon by permuting its vertices. For example, consider a regular pentagon with vertices initially labeled 11, 33, 55, 44, 22 (clockwise, starting from the top):

Applying the above three dihedral actions to the pentagon (a rotation, reflection, and then another rotation) produces the following relabelings of the pentagon's vertices:

1,3,5,4,2→2,1,3,5,4→2,4,5,3,1→1,2,4,5,3.1, 3, 5, 4, 2 \rightarrow 2, 1, 3, 5, 4 \rightarrow 2, 4, 5, 3, 1 \rightarrow 1, 2, 4, 5, 3.

You are given an arbitrary clockwise labeling of the vertices of a regular nn-gon using the integers 11 through nn, and a second sequence to test. Determine whether it's possible to apply some series of dihedral actions to the nn-gon so that the test sequence appears as a contiguous clockwise sequence of vertex labels on the transformed polygon.

입력

The first line of input has two integers nn and mm, (1≤m≤n≤5⋅1041 \leq m \leq n \leq 5 \cdot 10^{4}) where nn is the number of vertices of the polygon and mm is the length of the sequence to be tested.

The next line contains nn space-separated integers dd (1≤d≤n1 \le d \le n). This is the initial labeling of the polygon vertices. It is guaranteed that each integer from 11 to nn appears exactly once.

The next line contains mm space-separated integers tt (1≤t≤n1 \le t \le n). This is the sequence to be tested.

출력

Output a single integer, which is 11 if the test sequence could appear as a contiguous sequence of vertex labels after applying some series of dihedral actions to the initial polygon, and 00 otherwise.

예제7

  1. 예제 1

    입력
    3 3
    1 2 3
    1 3 2
    
    예상 출력
    1
    
  2. 예제 2

    입력
    3 1
    1 2 3
    1
    
    예상 출력
    1
    
  3. 예제 3

    입력
    4 2
    1 2 3 4
    1 3
    
    예상 출력
    0
    
  4. 예제 4

    입력
    4 4
    1 2 3 4
    2 3 4 1
    
    예상 출력
    1
    
  5. 예제 5

    입력
    4 4
    1 2 3 4
    3 2 1 4
    
    예상 출력
    1
    
  6. 예제 6

    입력
    5 3
    1 3 5 4 2
    2 1 3
    
    예상 출력
    1
    
  7. 예제 7

    입력
    5 4
    1 3 5 4 2
    2 1 5 3
    
    예상 출력
    0