logo AlgoBeat OnlineJudge
登录 注册

#216168. [ICPC 2024 NAC] Dihedral Group

内存限制:2048 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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

:::align{center} :::

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:

You are given an arbitrary clockwise labeling of the vertices of a regular -gon using the integers through , and a second sequence to test. Determine whether it’s possible to apply some series of dihedral actions to the -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 and , () where is the number of vertices of the polygon and is the length of the sequence to be tested.

The next line contains space-separated integers (). This is the initial labeling of the polygon vertices. It is guaranteed that each integer from to appears exactly once.

The next line contains space-separated integers (). This is the sequence to be tested.

输出格式

Output a single integer, which is 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 otherwise.

样例

样例输入 1

3 3
1 2 3
1 3 2

样例输出 1

1

样例输入 2

3 1
1 2 3
1

样例输出 2

1

样例输入 3

4 2
1 2 3 4
1 3

样例输出 3

0

样例输入 4

4 4
1 2 3 4
2 3 4 1

样例输出 4

1

样例输入 5

4 4
1 2 3 4
3 2 1 4

样例输出 5

1

样例输入 6

5 3
1 3 5 4 2
2 1 3

样例输出 6

1

样例输入 7

5 4
1 3 5 4 2
2 1 5 3

样例输出 7

0