We need to determine if two sequences of barrier distances (relative to different starting points) come from the same set of absolute barrier positions on a circle of length L. That is, there exists a set S of n points on the circle (positions modulo L) such that if we start at point A, moving counterclockwise, the distances to the barriers (i.e., the angular distances from A to each barrier, measured along the direction of travel) are given by the first sorted list. Similarly, starting from point B, the distances to the same barriers yield the second sorted list. Both A and B are integer points (assumed? It says they started from some integral point on the track). Barriers are also at integer distances from each other? Actually, the statement: "All barriers are located at integer distance from each other along the track." Means that the distances between consecutive barriers (in the order along the track) are integers? But since L and positions are integers, it's automatically integer distances if all coordinates are integers? Actually, "All barriers are located at integer distance from each other along the track." might be interpreted as the positions are integer distances from some reference, but they say "in distinct points of which there are n barriers. ... All barriers are located at integer distance from each other along the track." It's ambiguous but perhaps they mean that the barriers are placed at integer coordinates on the circle? Typically, in these problems, L and distances are integers, and points are considered modulo L. They said: "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." Also: "Each of the friends started from some integral point on the track." So all positions are integer-valued mod L. So we have a circle of length L with integer coordinates 0..L-1 (or any integer representation). The barriers occupy n distinct integer positions modulo L. Each runner starts at an integer position (maybe unknown) and then records the distances (positive along counterclockwise direction) from his start to each barrier, sorted increasingly. Since start and barriers are integer, distances will be integer as well.
Given two sorted lists A[0..n-1] and B[0..n-1] of distinct integers between 0 and L-1 inclusive, can we determine if there exists a set S (size n, integer positions mod L) and start positions s1, s2 (integers mod L) such that the distances from s1 to each barrier (i.e., (b - s1) mod L, taking the positive modulo result in [0, L-1]) sorted yields A, and similarly from s2 yields B? Also note that the runner always runs counterclockwise; the distances are measured along that direction, so for a barrier at position x and start at s, the distance is (x - s) mod L, where mod yields a number in [0, L-1] (since distances are between 0 and L-1 inclusive). If barrier is exactly at start, distance 0 is possible? Yes, as they say distances between 0 and L-1 inclusive. In examples, they don't have 0 except in second test? Actually second test B has 0. So possible.
So we need to check if A and B represent the same cyclic set up to a rotation. More precisely, the set of absolute barrier positions (mod L) can be derived from A by adding a constant offset modulo L to each element? Not exactly: If we know the start point s1, then the barrier positions are (s1 + a_i) mod L for each a_i in A. So the set S = { (s1 + a_i) mod L } for some s1. Similarly, from B, S = { (s2 + b_j) mod L } for some s2. So S is a set of n integers modulo L. The condition is that there exist s1, s2 such that these two sets coincide. That is equivalent to saying that the multisets of differences between A and B? Or that the two sequences A and B are "cyclic shifts" of each other when considering the set of gaps? Let's think.
Given two sorted lists of distances, we need to see if there exists a rotation (i.e., a constant d) such that the set A (interpreted as distances from some origin) can be transformed into set B after possibly a different rotation? Actually, the unknown s1 and s2 give two representations of the same set S. So the sets S - s1 (mod L) = A (as set, but sorted) and S - s2 = B. So A and B are just different views of S under different coordinate origins. Therefore, A and B must be the same multiset of relative positions up to a global rotation. In other words, there exists a constant shift d such that when we add d modulo L to each element of A (and then sort mod L), we obtain B. Because if S = {s1 + a_i} = {s2 + b_j}, then for each i, there exists j such that s1 + a_i = s2 + b_j => b_j = (s1 - s2) + a_i (mod L). So the set B is exactly the set { (a_i + delta) mod L } for delta = s1 - s2 (mod L). Conversely, if there exists a delta such that the set of (a_i + delta) mod L equals the set B (as sets, order doesn't matter), then we can set s1 arbitrary, s2 = s1 - delta, and S = {s1 + a_i}. However, note that the input provides A and B as sorted lists (distinct integers). So we need to check if there is some integer delta (0 ≤ delta < L) such that B (as a set) equals { (a + delta) mod L | a in A }. This is essentially checking if the two cyclic patterns are the same up to rotation.
But careful: When we shift by delta mod L, the sorted order of the shifted values might not be simply A + delta because after mod L, some numbers may wrap around. The set of numbers modulo L is cyclic. So we need to check if there exists a rotation that aligns the two sets. The classic method: Represent the sets as a binary string of length L indicating presence of barriers, then check if one string is a rotation of the other. Since L ≤ 100, n ≤ 50, we can do that directly. But note: The runners' distances are from start to barriers, which are measured as (barrier - start mod L). This indeed gives numbers in [0, L-1]. So the set of distances for a given start is exactly the set of positions of barriers relative to that start. So if we have the absolute barrier set S, the distances from start s are { (x - s) mod L | x ∈ S }. So the two distance sets A and B are related by a rotation: B = { (a + delta) mod L | a ∈ A } where delta = s1 - s2 (mod L). So check if A rotated by some delta yields exactly B.
Thus the problem reduces to: Given two sorted lists of distinct integers A and B of size n, and given L, check if there exists an integer k such that { (a + k) mod L | a in A } equals B (as a set). Since n ≤ 50 and L ≤ 100, we can brute-force over all possible k from 0 to L-1, compute the transformed set, sort it, and compare to B sorted. Since A is already sorted, we can for each k compute (a_i + k) mod L, then sort those n numbers (or we can keep them as set and compare to B). Sorting each time O(n log n) with L=100, n=50, so trivial.
But is there any nuance? The note: "All barriers are located at integer distance from each other along the track." That might impose additional condition that distances between consecutive barriers are integers, but since we already have integer positions modulo L, that is automatically satisfied if L is integer and barriers are integer positions? Actually, "integer distance from each other" could be interpreted as the arc lengths between consecutive barriers are integers, which is given if all coordinates are integers. So it's consistent. But is there a scenario where the set of barriers as integer positions might not be equally spaced but still integer distances? That's fine.
Also, the runners start at integral points, and distances recorded are integer, given. So our reduction should hold.
But wait: The example 2: n=4, L=9, A = [2,3,5,8], B = [0,1,3,6]. Check if rotation works. Let's test: If we shift A by something: maybe delta= ? (2+? mod9 =0 => ?=7 or -2 mod9=7). Check: A+7 mod9 => [2+7=9 mod9=0, 3+7=10 mod9=1, 5+7=12 mod9=3, 8+7=15 mod9=6] => [0,1,3,6] sorted = B. So yes.
Example 1: A=[2,4,6], B=[1,5,7]. Check delta: 2+? mod8=1 => ?=7 (since 2+7=9 mod8=1). Then A+7 => [2+7=9 mod8=1, 4+7=11 mod8=3? Wait 11 mod8=3, not 5. Let's compute properly: For L=8, 2+7=9 ≡ 1, 4+7=11 ≡ 3, 6+7=13 ≡ 5. That yields [1,3,5] not [1,5,7]. So that delta fails. Try other delta: 2+?=5 mod8 => ?=3, then A+3 => [2+3=5,4+3=7,6+3=9≡1] => [5,7,1] sorted [1,5,7] = B. So delta=3 works. So rotation works.
Third example: n=2, L=4, A=[1,3], B=[1,2]. No rotation of A yields B? Check: A+0 => [1,3] != B. +1 => [2,0] sorted [0,2] != B. +2 => [3,1] sorted [1,3] != B. +3 => [0,2] sorted [0,2] != B. So NO.
Thus brute force works.
But we must consider: The barriers are at integer points, and both runners start at integer points. So the sets of distances A and B are given as integers. Our brute force over k from 0 to L-1 is valid because k = s1 - s2 mod L can be any integer between 0 and L-1 inclusive.
Edge cases: If the runners started at the same point, then delta=0 should make A equal B? Not necessarily because they might have recorded distances in different order? Actually if they start at same point, they should record the same set of distances. But input could have A and B different. Then delta=0 would not match; but there could be a different delta? Wait, if they started at same point, then s1=s2, so delta=0. So A should equal B. But the problem asks whether they could have run the same track (i.e., same absolute barrier positions). That does not require that they started at the same point. So we allow any delta. So our check is correct.
Another nuance: When we rotate A by delta, we get a set of numbers, but the order of the numbers as recorded by the runner might be different because the runner records distances in ascending order as he encounters barriers. Indeed, the distances are sorted ascending because they are measured from start in increasing order as you go around the circle. So B is given sorted. After rotation, the transformed set may not be sorted because of wrap-around. But we need to compare as sets. So we must sort the transformed set to compare with sorted B.
Thus algorithm: Read n, L, A list, B list (sorted ascending). For shift from 0 to L-1:
Compute C = [ (a + shift) % L for a in A ]
Sort C
If C equals B, print YES and exit.
If none match, print NO.
Complexities fine.
But let's double-check: Could there be a scenario where the two sets of distances correspond to the same absolute barrier set but not via a simple rotation? Suppose we have S set. A = { (x - s1) mod L for x in S } sorted. B = { (x - s2) mod L } sorted. It's clear that B = { (a + s1 - s2) mod L for a in A }. So yes, it is a rotation.
Thus answer is correct.
But why does the problem statement mention "All barriers are located at integer distance from each other along the track"? It might be to guarantee that distances are integer, and the start points are integer, but we already have integer inputs. So maybe it's just context.
Now, consider constraints: n ≤ 50, L ≤ 100. So brute force is fine.
But wait, there is a possible nuance: The runners each wrote n integers in ascending order, each between 0 and L-1 inclusive. However, they wrote the distance from their start along the track to each barrier. But note: When you start at some point, the distances to barriers are computed as the arcs from start to barrier in counterclockwise direction. Since there are n barriers, you will encounter each barrier exactly once per lap, so indeed you have n distances. But is it possible that the same distance appears from different barriers? No, because barriers are distinct points, and distances are modulo L, so they should be distinct unless two barriers are exactly opposite? Actually if start and barrier positions are all distinct modulo L, (x - s) mod L can be equal for two different x? That would mean x1 ≡ x2 (mod L) which they aren't. So distances are distinct. Input lists are distinct.
Now, I think it's straightforward. However, could there be a scenario where the two lists are of the same set but not via a rotation because the circle has length L and we treat distances mod L? The rotation approach covers it.
But wait, there is a subtlety: The runners always run counterclockwise. The distances are measured from start to barrier along that direction. So for a barrier located exactly at start, distance is 0. However, if a barrier is at the starting point, the runner would record 0. Both lists are sorted ascending, and 0 can be present. In our rotation method, if A contains 0, then shifting by delta would produce delta mod L in C. But we must ensure that when we compare C to B, we treat all numbers mod L in [0, L-1]. That's fine.
What about the case where the sets are the same but the ordering of distances might differ due to different starting points? That's why we sort after shifting.
But is it necessary to sort? For each shift we could create a set and compare to B set, but B is sorted input, we can just sort C.
Thus solution is trivial.
But let's think if there is any trick: Possibly the barriers are located at integer distances from each other means that the distances between consecutive barriers are integers, but not necessarily all positions are integer? The problem says "All barriers are located at integer distance from each other along the track." And "Each of the friends started from some integral point on the track." So the start points are integral, but barriers themselves might not be at integer coordinates? The phrase "integer distance from each other" might refer to the distances along the track between barriers being integer, which would mean that if you list the positions in order around the circle, the differences (arc lengths) are integers. But does that force the positions themselves to be integer? Not necessarily, if we pick an arbitrary origin. However, the distances recorded by runners are measured from their start to each barrier, and those are integers because they are given as integers between 0 and L-1. And the start is integral. If barriers are not necessarily at integer positions, can the distances be integers? Distance from an integer start to a barrier that is not at integer position would not be integer unless the barrier position also is integer modulo L? Typically, if you have a circle of length L, and you assign coordinates from 0 to L (with 0 and L identified), then a point's coordinate is a real number. If the start is integral and the distance (x - s) mod L is integer, then x must be integer modulo L. Because (x - s) ≡ integer (mod L) and s integer => x integer mod L. However, if we consider the distance as the minimal positive arc? Actually the distance is the arc length along the direction; it's not taken modulo? They say "distance from their start along the track to each of the n barriers." And they wrote integers between 0 and L-1. So it's the actual distance measured along the track in counterclockwise direction. If the start and barrier positions are real numbers, the distance would be the difference modulo L, which might not be integer. But the problem gives integers, so it's implied that positions are integer. So I think barriers are also at integer positions.
Thus the simple solution stands.
But the problem might be from Codeforces (I recall a problem "Running with barriers on the circle" or something). Actually there is Codeforces Round #... I remember a problem "Kefa and Park"? Not sure. Anyway, it's standard.
Given the low constraints, we can also solve by checking if the cyclic differences between consecutive barriers (the gaps) are the same up to rotation. Since the set of gaps (n numbers that sum to L) is a cyclic sequence. If we compute gaps from A and B (including wrap-around gap from last to first + L), then see if these gap sequences are rotations of each other. This also works. But the rotation of sets approach is easier.
Implementation details:
- Read n, L.
- Read list A of length n.
- Read list B of length n.
- Sort A and B (but they already sorted as per input, but we can still sort to be safe).
- For k in range(L):
C = sorted([(a + k) % L for a in A])
if C == B: print("YES") and return.
- Print "NO".
But careful: Since A and B are sorted ascending, we can possibly avoid sorting each time by using a set representation or by checking if the multiset of shifted values equals B without sorting. Sorting is fine.
Time complexity: O(L * n log n) ≤ 100 * 50 log 50 ≈ trivial.
We should consider the case where n=0? Not possible because n≥1.
Edge cases: When L is up to 100, but there might be duplicate elements? Input says distinct integers, so no duplicates.
One more nuance: The runner writes distances in ascending order, but note that if there is a barrier at distance 0, it's fine. However, when we shift A by k, we might get values that wrap around; the sorted order of these wrapped values might not correspond to the order a runner would record? But as sets, it's fine.
Let's test some scenarios.
Suppose L=5, n=2, barriers at positions {1,3}. If start at 0: distances = [1,3]; start at 2: distances = [ (1-2) mod5=4, (3-2) mod5=1 ] => [1,4] sorted [1,4]. So A=[1,3], B=[1,4]. Check rotation: A+? yields [1,4]? 1+? mod5=1 => ?=0 or 5? 0 gives [1,3] not [1,4]; 1+?=4 => ?=3 => then [1+3=4,3+3=6 mod5=1] => [4,1] sorted [1,4] works. So delta=3 works.
Now consider if L=6, n=3, S={0,2,4} (evenly spaced). For start 0, A=[0,2,4]; for start 1, distances: (0-1 mod6=5,2-1=1,4-1=3) sorted [1,3,5]; so A=[0,2,4], B=[1,3,5]. Check rotation: A+? => if ?=1: [1,3,5] works. Good.
Now consider if there is a scenario where the rotation method would incorrectly say NO due to multiple possible representations because of symmetrical sets? No.
Thus solution is straightforward.
But before finalizing, let's read problem statement again to ensure we didn't misinterpret: "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)." So they ask whether the tracks coincide, meaning that if they both ran on the same physical track (same barrier placements), then by appropriately choosing start positions for each, the recorded sequences would be these. But we have the recorded sequences. So we need to check if there exists a track (i.e., a set of barrier positions) and two start points (maybe different) that produce these sequences. Equivalently, is there a set S such that A and B are both rotations of S? But from earlier, if there exists S such that A = { (x - s1) mod L } and B = { (x - s2) mod L }, then B is rotation of A. Conversely, if B is rotation of A, then we can set S = { (a + s1) mod L } for some s1, and then B corresponds to s2 = s1 - delta. So yes, the condition is exactly that there exists a rotation between the sequences.
Thus problem solved.
But wait: The problem says "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." Note that the distances are in ascending order as they encounter barriers. But if two barriers are at the same distance from start? Not possible as argued. So sorted order is natural.
Now consider scenario: L=5, n=2, A=[0,1], B=[2,4]. Is there a rotation? A+2 => [2,3] not B. A+4 => [4,0] sorted [0,4] not B. So NO. But could there be a set S? Suppose we try to find S and s1, s2 such that A corresponds to s1, B to s2. A=[0,1] implies that from s1, there are barriers at distances 0 and 1, i.e., at positions s1+0 and s1+1 mod L. So S contains s1 and s1+1. Then B must be distances from s2: s2 to s1 and s2 to s1+1. Let's compute those distances: (s1 - s2) mod L and (s1+1 - s2) mod L. They must be {2,4} in some order. So there exists integer delta = s1-s2 such that {delta, delta+1} mod L = {2,4}. So either delta=2 and delta+1=3 -> not {2,4}. Or delta=4 and delta+1=5 mod5=0 -> set {4,0} = {0,4}. Not {2,4}. So no solution. So NO, correct.
Thus algorithm stands.
One more nuance: The positions are on a circle of length L. The distances are measured modulo L, but there is also the fact that the runner runs a full lap and records distances in ascending order; if there is a barrier exactly at start, distance 0; if there is a barrier just before the start (i.e., at L-ε), the distance would be something like small? But since all positions are integer, if start is at integer, barriers are integer, so distances are integer. So good.
Now, we should consider also the possibility that the runners might start at points that are not necessarily integer? But the problem says integral point. So integer.
Thus solution done.
We'll write code in Python for simplicity.
But before coding, let's think about any hidden traps: The input lines contain n integers each. They are distinct and sorted ascending. We'll read them as list of ints.
One nuance: When we rotate A by shift, we compute (a+shift)%L. But note: The modulo operation in Python yields numbers from 0 to L-1. That's fine.
But we need to be careful: If A contains numbers in [0, L-1], after adding shift and taking modulo, we may get 0 if wrap-around. So that's okay.
Now, complexity: O(L * n log n) ≈ 100 * 50 * log50 ≈ fine.
Alternative: We could compute the cyclic gaps and compare rotations of that sequence. That might be more elegant but not necessary.
Given the problem constraints, the brute force is acceptable.
We'll implement.
But wait: The problem says "n ≤ L ≤ 100". So L could be exactly n? For example, n=5, L=5, then barriers occupy every integer point? Possibly. Then any rotation would map to the same set? If A and B are permutations of [0,1,2,3,4]? Actually they are sets of size n, so if L=n, the only possible set of barriers is the whole set of points? Not necessarily: There are n barriers, L length, with n ≤ L. Could be n = L, then the set S would be all integer points on the circle, so the distances from any start would be the full set {0,...,L-1} but in some order? Actually if S is all points, then distances from start s would be all numbers 0..L-1 (since for each distance d, there is barrier at (s+d) mod L). So the recorded list would be all integers 0..L-1 sorted, which is just 0,1,...,L-1. So A and B must both be exactly that. But input might have A and B not full set? But n = L, so n barriers, L length, so there are L barriers on a circle of length L. Since points are integer and distinct, they must occupy every integer coordinate. So indeed the set S must be all positions. So A and B must be 0..L-1 sorted. If they are, then rotation check will succeed? For any shift, shifting the full set yields again full set, so check would succeed. But if A is full set and B is full set, delta=0 will match; also any delta yields same set? Actually if S is all points, then A = {0,...,L-1} regardless of start? Wait careful: If there is a barrier at every integer point, then for any start s, the distances from s to barriers are all numbers 0..L-1 because for each d in 0..L-1, the point (s+d) mod L is a barrier. So the sorted list will be exactly 0,1,...,L-1. So both A and B would be exactly 0..L-1. So if they are, then our rotation check: For any delta, the shifted set of A (which is all numbers) will still be all numbers, so it will equal B. So we will output YES. Good. If one of them is not full set but n = L, that's impossible because there are only L distinct integer points, so the set must be full. But input could still provide a list that is not full set (e.g., n=5, L=5, A = [0,1,2,3,4] is full; any other set of 5 distinct numbers from 0..4 is the same set, because there are only 5 numbers. So any list of 5 distinct numbers from 0..4 is essentially the full set, just sorted. Actually if n=5 and L=5, there are exactly 5 integers from 0 to 4. So any list of 5 distinct numbers will be exactly those 5 numbers. So it's forced to be full set. So it's fine.
Now, edge case: n=1. Then there is a single barrier. The distances from start to that barrier are some integer d (0 ≤ d ≤ L-1). For two runners, we have A=[a], B=[b]. Is there a set S (single barrier) and starts s1,s2 such that (s1+ a) mod L = (s2+ b) mod L? This is equivalent to existence of s1,s2 such that s1+ a ≡ s2+ b (mod L) => a - b ≡ s2 - s1 (mod L). Since s1,s2 can be any integers mod L, we can always set s2 - s1 ≡ a - b mod L. So any a,b are possible? But wait, the barrier positions must be same. For a given a and b, we can choose s1 arbitrarily, set barrier = (s1+a) mod L, then set s2 such that (s2+b) ≡ barrier mod L => s2 ≡ barrier - b mod L. That's always possible. So for n=1, any two lists of length 1 should be YES? But check example: n=1, L any, A=[x], B=[y] always YES? Let's test with our rotation condition: For n=1, we need a shift k such that (a + k) mod L = b. That's possible if we set k = (b - a) mod L. Since k is an integer mod L, always exists. So YES always. That matches intuition: a single barrier anywhere can produce any distance a from some start, and any distance b from another start, as long as we adjust start points accordingly. So for n=1, answer always YES. Our brute force will find k = (b - a) mod L, which is within 0..L-1, so it will output YES. Good.
Now, for n>1, not all pairs are possible.
Thus solution is correct.
Now let's think if there is any trick due to the fact that distances are written in ascending order, but the actual order of barriers encountered is always the sorted distances. That is consistent.
Thus we are ready.
But before finalizing, consider if there is a scenario where rotation is not enough because the sets might be the same but the rotation might not map the sorted list to the sorted list because the sorted order changes after wrap. However, we sort after shifting, so that's fine.
One more nuance: The sets A and B are given as sorted ascending. When we shift A by delta, the resulting set C may not be in the same cyclic order as B when sorted, but it is a set. For the track to coincide, we need that the set of shifted A equals B as sets. Sorting and comparing sets works.
But could there be a scenario where the sets are equal but the condition of starting points being integral imposes extra restriction? Since we already treat positions as integers mod L, the existence of integer shift is fine.
Thus answer is straightforward.
Now, is there any possibility that the two runners could have started at the same point but the lists are different? That would indicate different tracks. But our check would detect that.
I'll produce Python code accordingly.
But we should also consider reading input: there might be multiple test cases? The problem statement only describes one case. So single test case.
Thus code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
L = int(next(it))
A = [int(next(it)) for _ in range(n)]
B = [int(next(it)) for _ in range(n)]
A_sorted = sorted(A) # but already sorted
B_sorted = sorted(B)
for shift in range(L):
C = sorted((a + shift) % L for a in A_sorted)
if C == B_sorted:
print("YES")
return
print("NO")
if name == "main":
solve()
Check with given examples.
Example1:
3 8
2 4 6
1 5 7
A=[2,4,6], B=[1,5,7]
shift=3 => C=[(2+3)%8=5, (4+3)%8=7, (6+3)%8=1] sorted [1,5,7] => YES.
Example2: shift=7 => C=[0,1,3,6] sorted = B? B=[0,1,3,6] -> YES.
Example3: none -> NO.
Works.
Now, what about potential performance issues? None.
Thus final answer.
But wait: Could there be a scenario where the track length L is less than the maximum distance recorded? No, distances are between 0 and L-1 inclusive.
All good.
Now, I'll produce final answer.
But I recall some solutions in Codeforces used a different approach: compute differences between consecutive barriers (including wrap-around) for both lists, then check if one list of differences is a rotation of the other. Because the relative gaps between barriers are invariant under rotation of start point. Actually if we take the distances from a start, the gaps between consecutive distances (sorted) correspond to the arcs between barriers in order around the circle. Specifically, let A sorted: a0 < a1 < ... < a_{n-1}. Then the gaps are g0 = a1 - a0, g1 = a2 - a1, ..., g_{n-2} = a_{n-1} - a_{n-2}, and g_{n-1} = (L - a_{n-1}) + a0 (the wrap-around). This list of n gaps sums to L and is cyclic. If two start points are used, the gap sequences will be rotations of each other (since the barrier order around the circle is fixed, and starting point just changes where you start listing distances, but the cyclic order of gaps remains the same). So we can compare gap sequences. That also works and is O(n) after sorting maybe. But given L small, brute force is simpler.
But is there any nuance where the sets of distances might not correspond to a valid barrier arrangement because the sum of gaps must be L? That's automatically satisfied because we use L and the distances. The input distances are within 0..L-1 sorted, but they might not satisfy that the wrap-around gap is positive? Actually it is automatically defined; we don't need to check validity because any such set of n distinct integers in [0, L-1] can be realized as distances from some start for some barrier set? Let's think: For any sorted list A of n distinct integers in [0, L-1], can we find a set S and start s such that the distances from s to S are exactly A? Yes: choose any s (say 0), then set S = { (s + a) mod L for a in A }. That gives exactly A as distances from s. So any such list is valid for some barrier set. So no constraints.
Thus our brute force is correct.
Now, about the statement "All barriers are located at integer distance from each other along the track." It might also imply that the gaps are integers, which they are. So fine.
Now final answer.
One more test: n=2, L=3, A=[0,1], B=[0,2]. Shift? Try: shift=0 => [0,1] != B; shift=1 => [1,2] != B; shift=2 => [2,0] sorted [0,2] == B? [2,0] sorted is [0,2] yes. So YES. Means same track? Let's construct: S from A with s1 arbitrary: say s1=0 => S = {0,1}. Then from B: need s2 such that distances [0,2] from s2 correspond to S: s2 +0 ≡ x, s2+2 ≡ y, with {x,y} = {0,1}. So possibilities: either s2 ≡ 0 and s2+2 ≡ 2 mod3 =2 not 1; or s2 ≡ 1 and s2+2 ≡ 0? s2=1 => 1+2=3 mod3=0 gives distances from s2: to barrier at 1: (1-1)=0, to barrier at 0: (0-1) mod3=2, so distances {0,2} which matches B. So YES. So rotation works.
Now, consider n=2, L=4, A=[0,3], B=[1,2]. shift=1 => [1,0] sorted [0,1] != B; shift=2 => [2,1] sorted [1,2] == B? [2,1] sorted is [1,2] yes. So YES. So [0,3] and [1,2] are rotations of each other? Let's check: A set {0,3}, shift by 2 gives {2,5 mod4=1} = {1,2} set, yes. So YES.
Thus all fine.
I'll produce final answer.
But there might be a subtlety: The runners wrote distances in ascending order. However, if we shift A by delta, the set C might not be in ascending order, but we sort it. That's okay. However, consider if the barrier positions themselves are not necessarily integer? But we argued they are.
Thus done.
Now, I will produce the final answer.
Given this is a reasoning response, I need to output the solution explanation. But the user likely expects the code? The instruction: "Write the program which will check..." So they want the program. In the environment, we need to output the solution code. Usually in these tasks, we provide the code.
Thus I will output a Python solution with solve().
Also note that input reading might include extra whitespace, we'll handle it.
Ok. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
L = int(next(it))
A = [int(next(it)) for _ in range(n)]
B = [int(next(it)) for _ in range(n)]
A_sorted = sorted(A) # already sorted, but to be safe
B_sorted = sorted(B)
for shift in range(L):
# apply rotation and sort the result
C = sorted((a + shift) % L for a in A_sorted)
if C == B_sorted:
print("YES")
return
print("NO")
if name == "main":
solve()