Dasha and friends
time limit per test 2 second memory limit per test 256 megabytes
Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation:
The track is the circle with length L, in distinct points of which there are n barriers. Athlete always run the track in counterclockwise direction if you look on him from above. All barriers are located at integer distance from each other along the track.

Her friends the parrot Kefa and the leopard Sasha participated in competitions and each of them ran one lap. Each of the friends started from some integral point on the track. Both friends wrote the distance from their start along the track to each of the n barriers. Thus, each of them wrote n integers in the ascending order, each of them was between 0 and L-1, inclusively.

Consider an example. Let L=8, blue points are barriers, and green points are Kefa’s start (A) and Sasha’s start (B). Then Kefa writes down the sequence [2,4,6], and Sasha writes down [1,5,7].
There are several tracks in the country, all of them have same length and same number of barriers, but the positions of the barriers can differ among different tracks. Now Dasha is interested if it is possible that Kefa and Sasha ran the same track or they participated on different tracks.

Write the program which will check that Kefa’s and Sasha’s tracks coincide (it means that one can be obtained from the other by changing the start position). Note that they always run the track in one direction — counterclockwise, if you look on a track from above.

传送门:CF761B

Input

The first line contains two integers n and L (1≤n≤50, n≤L≤100) — the number of barriers on a track and its length. The second line contains n distinct integers in the ascending order — the distance from Kefa's start to each barrier in the order of its appearance. All integers are in the range from 0 to L-1 inclusively. The second line contains n distinct integers in the ascending order — the distance from Sasha's start to each barrier in the order of its overcoming. All integers are in the range from 0 to L-1 inclusively.

Output

Print "YES" (without quotes), if Kefa and Sasha ran the coinciding tracks (it means that the position of all barriers coincides, if they start running from the same points on the track). Otherwise print "NO" (without quotes).

Sample Input

1
2
3
3 8
2 4 6
1 5 7

Sample Output

1
YES

题解

把环断成线段,以第一个人所在位置为原点,枚举一下第二个人所有起点,计算起点到每个障碍的距离,再排个序与第二个数组比较就好,如果一模一样,那就是跑的同一条道路。
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.*;  
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int l=sc.nextInt();
int[]s1=new int[n];
int[]s2=new int[n];
for(int i=0;i<n;i++) s1[i]=sc.nextInt();
for(int i=0;i<n;i++) s2[i]=sc.nextInt();
int flag=0;
ArrayList<Integer>list=new ArrayList<Integer>(); //存放起点到每个障碍的距离
for(int i=0;i<l;i++){
list.clear();
for(int j=0;j<n;j++){
if(s1[j]<i) list.add(l-i+s1[j]); //如果障碍比起点小,因为是逆时针的,所以距离是l-i+s1[j];
else list.add(s1[j]-i);
}
Collections.sort(list);
int k=0;
for(k=0;k<n;k++){
if(list.get(k)!=s2[k]) break; //比较距离是否一样
}
if(k==n) flag=1; //如果全部一样
if(flag==1) break;
}
if(flag==1) System.out.println("YES");
else System.out.println("NO");
}

}

}