We need to determine if there exists an integer k, 1 <= k < n, such that rotating all segments by k steps (clockwise) yields exactly the same set of segments. The points are arranged on a circle. A rotation maps point i to i' = i + k (mod n), with wrapping so that 1..n become 1..n (presumably using mod where we treat numbers modulo n, mapping to 1..n). Actually careful: The points are numbered 1 to n clockwise. Rotation by k (clockwise) maps point i to ((i + k -1) mod n) + 1. We'll denote this as i -> i+k mod n, but with 1-index.
We have m segments, each as unordered pair (a, b). After rotation, becomes ( (a+k-1 mod n)+1 , (b+k-1 mod n)+1 ). The set of segments should be identical. So there exists a rotation (k not multiple of n) that preserves the multiset? Actually "the new image will be the same as the original one." Means the set of segments after rotation exactly matches the original set. Since segments are unordered and no duplicates (guaranteed no coinciding segments). So it's a set isomorphism under rotation.
We need to check if there exists k (1 <= k < n) such that for every segment (a,b), the rotated segment (a+k mod n, b+k mod n) is also a segment in the original set, and the mapping is bijective (since rotation is a bijection on the set of segments). Actually if the set of segments is the same, then for each segment s, its rotation is also in the set. So the set is invariant under that cyclic shift.
Thus the problem reduces to: Given a set of unordered pairs from Z_n (or numbers 1..n), determine if there exists a non-zero shift k such that applying shift to each pair yields the same set.
We need to output Yes if such k exists, else No.
Constraints: n up to 100k, m up to 200k. So we need an efficient algorithm.
Observations: This is essentially checking if the set of segments has rotational symmetry. One approach: We can think of representing the configuration as a pattern on the circle's points and chords. But we only have chords connecting points? Actually segments: they are straight segments, endpoints among the n points. They could be chords, diameters, or possibly edges along circumference? Actually "straight segments on the disc, the endpoints of which are all among the aforementioned n points." So they are chords (including edges that are arcs? No, straight segments are chords of the circle, connecting two points). They can cross interior. So it's a set of chords (including possibly diameters). So we need to check if rotating the whole set of chords yields same set.
We can treat each segment as an unordered pair of points. Under rotation by k, the pair is shifted. So the set S is invariant under shift k if and only if S = S + k (mod n). Equivalent to saying that S is periodic with period k (dividing n? Actually not necessarily dividing n; for invariance, we need that for all pairs, adding k yields another pair; and since shift is bijection, it's equivalent to S being a union of orbits of the cyclic group generated by k acting on the set of pairs. So S must be a union of orbits of that group action on unordered pairs.
We need existence of any k (1 <= k < n). We can test divisors? But n up to 100k, we could try all divisors of n? But maybe there are non-divisor periods? For a set of points on a discrete circle, if the set is invariant under rotation by k, then the group generated by k gives some period d = gcd(k, n). The rotation by k actually generates the subgroup of rotations by multiples of d. Since the set is invariant under k, it's also invariant under all multiples of k, hence under rotation by d (because d is combination: gcd(k,n) = d, then we can rotate by d as some integer combination of k). Wait: In a cyclic group of order n, the subgroup generated by k is all multiples of d where d = gcd(k,n). So if the set is invariant under shift by k, then it is also invariant under shift by d (since d is a multiple of k? Actually d = gcd(k,n) is a combination: There exist integers u, v such that d = uk + vn, but we consider operations mod n; rotating by d is equivalent to rotating by uk (mod n) because vn is full rotation. So if it's invariant under k, it's invariant under any multiple of k mod n, thus under d (if d can be expressed as (uk mod n) for some integer u). Indeed, because d ≡ uk (mod n) for some u (since k generates the subgroup of size n/d, its multiples produce all multiples of d). So invariance under k implies invariance under all shifts that are multiples of k modulo n, and thus under the shift by d (the minimal positive shift in that subgroup). Therefore, if there exists any k such that invariance holds, then invariance also holds for d = gcd(k,n) (with 1 <= d < n possibly). But d might be 0 if k = n? Not allowed. So essentially, if there is any symmetry, there is also symmetry by a shift that divides n? Because d = gcd(k,n) divides n, and d < n if k not multiple of n. But d could be n if k is 0 mod n, but k < n, so d = n only if k = 0? Actually gcd(k,n) = n only if n divides k, but k < n, so that cannot happen unless n=1? n >=2, so d < n. So d is a proper divisor of n? Not necessarily: If n and k are not co-prime, d divides n, but d might not divide n? It always divides n because gcd(k,n) divides n. So d is a divisor of n. But if k and n are coprime, then d=1. Invariance under shift by 1 would mean the set is invariant under any shift (since group generated is whole group), i.e., all rotations. But is invariance under shift by 1 necessary if invariance under shift by coprime k? Let's see: If k is coprime to n, then the subgroup generated by k is the whole group of size n. So shift by 1 can be expressed as some multiple of k mod n (since k is a generator). So if invariance under shift by k, then invariance under shift by 1 holds. That would mean the set is invariant under all rotations, i.e., all chords are present? Not necessarily; but invariance under shift by 1 means that if a chord (a,b) is in S, then for all t, (a+t,b+t) is also in S. Since there are n distinct rotations, the set must be a union of full orbits of all pairs under the full group. But the full group acts transitively on pairs? Not exactly: The orbit of a chord under the full rotation group is the set of all chords that are the same up to rotation. That orbit's size depends on the chord's properties: chords defined by difference d = (b-a) mod n (or the length). Under full rotation group, any chord with the same "length" (i.e., same unordered distance around the circle) can be rotated into each other. But there are also chords that are symmetric under some rotations (like diameters when n even: rotating by n/2 maps diameter to itself). So the orbit size might be n if the chord is not fixed by any nontrivial rotation, or n/2 if it's a diameter? Actually for an unordered pair {a, b}, its stabilizer under rotations: rotations that map the set {a,b} to itself. That happens when the rotation sends a to a and b to b (identity), or swaps a and b. A rotation that swaps a and b must satisfy: a + k ≡ b (mod n) and b + k ≡ a (mod n) => 2k ≡ 0 (mod n) and k ≡ b - a (mod n). This is possible if n is even and k = n/2, and then b-a must be n/2 mod n. So the only non-identity rotation fixing a chord is rotation by n/2 when the chord is a diameter (i.e., the points are opposite). So for diameters, the stabilizer has size 2, orbit size = n/2. For non-diameter chords, stabilizer is trivial, orbit size = n. So if the set is invariant under full rotation group (shift by 1), then for each chord present, its entire orbit must be present. That would mean either all chords of that length are present, or possibly certain symmetrical sets. But anyway.
But the existence of any symmetry shift k does not necessarily require that k is a divisor of n? The shift k itself might not divide n but the minimal period d = gcd(k,n) does. And invariance under k implies invariance under d. So if we find any k, then d = gcd(k,n) also works. So it suffices to check only the divisors of n? Actually d is a divisor of n. So if there exists some k, there exists a divisor d of n (d < n) such that invariance holds. Conversely, if invariance holds for some divisor d, then that's a valid k (since 1 <= d < n). So we can restrict the search to positive divisors of n less than n. However, there is a nuance: Could it be that invariance holds for some k that is not a divisor, but for d = gcd(k,n) invariance fails? Let's verify: Suppose S is invariant under shift by k, but not under shift by d. But as argued, because d is in the subgroup generated by k, there exists some integer t such that shift by d = shift by tk (mod n). Since shift by k is a symmetry, applying it t times yields that the set is invariant under shift by tk. But does invariance under k imply invariance under tk for any integer t? If we apply shift by k repeatedly, we get shifts by 2k, 3k, ... modulo n. Since S is invariant under shift by k, then S = shift_k(S) = shift_k^t(S) = shift_{tk}(S) because applying a symmetry repeatedly yields same set. But careful: shift_k(S) = S, then applying shift_k again to both sides: shift_k(shift_k(S)) = shift_k(S) => shift_{2k}(S) = S. Inductively, shift_{tk}(S) = S for any integer t. So indeed invariance under k implies invariance under any multiple of k modulo n. Hence invariance under d holds because d ≡ uk (mod n) for some integer u (since d is in the subgroup generated by k). Actually we need to ensure that there exists some integer t such that tk ≡ d (mod n). Since the subgroup generated by k is exactly the set of multiples of d (the gcd). In other words, the subgroup generated by k consists of all residues that are multiples of d modulo n. Since d is a multiple of d, d itself is in that subgroup. So there exists t such that tk ≡ d (mod n). Therefore, shift by d is shift by t*k, which is a symmetry. So invariance under k implies invariance under d. So indeed if any k works, then the divisor d = gcd(k,n) works. Therefore, we can restrict to checking all divisors d of n (with 1 <= d < n) for invariance. Also note that if n is prime, only divisor is 1. So we would check only shift by 1.
But is it possible that no divisor works but some non-divisor works? According to above, no, because if such a k works, then d = gcd(k,n) works. So checking divisors is sufficient.
Now we need to check for each divisor d of n (d < n), whether shift by d leaves S invariant. However, number of divisors of n can be up to about 128 for n up to 100k? Actually maximum number of divisors for numbers up to 100k is around 192? Let's compute: 100k = 100000, the highly composite numbers near that: 83160 has 128 divisors; 98280 has 128; 100800 (outside) has maybe more. So divisors up to maybe ~128. So checking each divisor individually is feasible if we can test invariance efficiently O(m) per divisor? That would be O(m * number_of_divisors) which could be up to 200k * 128 = 25.6 million, which might be okay in C++? But we must be careful with constant factors, but 25 million operations is okay. However, we also need to handle data structures for pairs. But we must test if S is equal to S shifted by d. That is: For every segment (a,b), the shifted segment ((a+d-1 mod n)+1, (b+d-1 mod n)+1) must also be present in S. Since S is a set, we can store segments in a hash set (or sorted set) and then check each segment's shifted version is in set. But note that we need to consider that segments are unordered. So we need to store pairs in a canonical form (e.g., (min, max) or (a,b) with a<b). However, after shifting, we might get (a',b') possibly with a'>b'? We'll canonicalize to sorted order before checking.
But we must also consider that the shift must be an exact set equality. That is, the set of shifted segments must be exactly the original set. Since the shift is a bijection on the set of all possible segments? Actually shifting is a permutation on the set of all possible n*(n-1)/2 segments. So if we check that for every segment s in S, shift(s) ∈ S, then because sizes are equal (|S| = m), it's automatically a bijection, i.e., the shifted set is a subset of S and same cardinality implies equality. So we just need to verify that for every segment in S, its shifted version (canonicalized) is also in S.
But careful: There is a nuance when a segment is a diameter (when n is even and b-a = n/2 mod n). Its shifted version might be the same as the original if d = n/2? But that's okay, it's still in S. The condition is that all shifted segments are present. Since the shift operation is a permutation on all chords, applying it to the set S yields a set of the same size. So if S contains all images, then it's exactly the shifted set, and since shift is invertible (shift by n-d), S = shift_d(S). So indeed it's sufficient to check that shift_d(s) ∈ S for all s ∈ S.
Thus for each divisor d, we can iterate over segments and check if shifted version is present in a hash set. Complexity: For each divisor, O(m) lookups in hash set, O(1) average. So O(m * num_divisors). With m up to 200k, num_divisors up to ~128, we get ~25.6M operations. That's acceptable in C++.
But there is a catch: For n large, the number of divisors might be small. However, we must be careful about modulo operations and canonical representation.
Alternative approach: Use string matching / hashing representation of the configuration as a circular array? Might be more efficient but not necessary.
Edge Cases: When m=0? But m≥1. Good.
Now there is also possibility that k = n? Not allowed because 1 ≤ k < n. So we ignore.
But we must consider that we should output "Yes" if there exists ANY k, not necessarily divisor. So checking divisors is sufficient.
But wait: Is it possible that for some k, invariance holds, but for d = gcd(k,n) invariance does not hold? The reasoning earlier seems sound, but let's double-check with an example.
Suppose n = 12. Consider a set S that is invariant under shift by 4, but not under shift by 2? gcd(4,12)=4, not 2. Actually d = gcd(4,12)=4. So invariance under 4 would imply invariance under multiples: 8, 0, and also 4. But does it imply invariance under 2? 2 is not a multiple of 4 modulo 12? 4 generates subgroup {0,4,8}. That's all multiples of 4 modulo 12. 2 is not in that subgroup. So invariance under 4 does NOT necessarily imply invariance under 2. However, d = gcd(k,n) is the minimal positive element of the subgroup generated by k. Indeed, if k=4, the subgroup generated is of order 3, containing 0,4,8. So the minimal positive shift in that subgroup is 4 (since 4 is the smallest positive). So d = 4, which divides n? 4 divides 12. So checking divisor d=4 would be fine. So we don't need to check 2 if invariance only under 4. So indeed if invariance under some k, then d = gcd(k,n) works because d belongs to the subgroup (it is the gcd, which is the smallest positive integer expressible as combination of k and n). But is d always in the subgroup generated by k modulo n? Yes: Since d = gcd(k,n), there exist integers u,v such that uk + vn = d. Then modulo n, uk ≡ d (mod n). So d is a multiple of k modulo n? Actually it's uk mod n = d. So shift by d is shift by uk (mod n). Since shift by k is a symmetry, shift by uk is also a symmetry (by repeated application). Hence shift by d is a symmetry. So invariance under k implies invariance under d. So indeed the minimal period d works and d divides n (since d = gcd(k,n) divides n). So checking all divisors is sufficient.
Thus algorithm:
- Read n, m.
- For each segment, read a, b. Normalize to (min(a,b), max(a,b)). Insert into a hash set (e.g., unordered_set with hash of pair, or use set of long long: encode as aN + b? But N up to 100k, so a (n+1) + b is safe: encode = a * (n+1) + b, since n up to 100k, product ~1e10 fits in 64-bit. Or use pair<int,int> with custom hash.)
- Compute all divisors of n that are less than n. For each d in divisors:
- For each segment (a,b) in original list (maybe store them in a vector of pairs in normalized form), compute shifted endpoints: a' = (a + d - 1) % n + 1? Actually if we treat 1-indexed: a' = ((a-1 + d) % n) + 1. Similarly b'.
- Normalize the shifted pair: let x = min(a', b'), y = max(a', b').
- Check if (x,y) is present in the hash set.
- If any segment's shifted version is missing, then d is not a symmetry.
- If all present, then we found a symmetry, output "Yes" and exit.
- If no divisor works, output "No".
Potential pitfalls:
- Handling of duplicate segments? Input says no segments coincide, so set has size m.
- Since m can be up to 200k, the inner loop for each divisor iterates over all segments. That's O(m * num_divisors) = up to 25M. Should be okay in C++ with fast I/O, but we need to ensure that hash lookups are efficient. Use unordered_set with a good hash.
But careful: For n large (e.g., 100000), storing pairs as 64-bit integers is efficient. Encode: ( (uint64_t)a << 20 ) + b? Better: encode = a * (n+1) + b, as n+1 <= 100001, product fits in ~1e10 < 2^63. So using 64-bit ints is fine. We'll store in an unordered_set<uint64_t>. However, unordered_set may have high overhead; but 200k entries is fine.
Alternatively, we could use a sorted vector and binary search, but O(log m) per lookup would be slower for 25M lookups.
Hash set with 200k entries, lookups 25M: average maybe 0.1-0.2 sec? Possibly fine.
But there is a catch: Number of divisors could be as high as around 128. 200k * 128 = 25.6M lookups. Each lookup involves a modulo operation for shift, and normalization. That's okay.
We can also prune early: If m is not divisible by something? Actually not necessary.
But there is a nuance: The shift operation must be applied to both endpoints. However, the set S is unordered; after shifting we need to check if that unordered pair exists. Our original set stores unordered normalized pairs (a<b). So shifting might produce a pair where a' > b', we normalize. Good.
Edge Cases: For segments that are diameters when n even, shifting by n/2 could produce the same pair but swapped; but normalization handles that.
Now, one more thought: Could there be symmetry with k that is a divisor of n but not tested because we only test divisors less than n? Yes, we test all positive divisors less than n. That includes 1 and n's proper divisors. Note: If n is prime, only 1 is tested. That's correct.
But consider n=4, test divisor d=1,2? 2 is divisor <4. Yes.
Now, what about k=0? Not allowed.
Now, there is a subtlety: The set S must be invariant under shift by d exactly, not just that each shifted segment is present. As argued, if for each segment its shifted version is present, then since shift is a permutation on all chords, the shifted set is a subset of S. Since they have same size (both m), it must be equal. However, note that shift_d is a permutation on the set of all chords, but is it a permutation when restricted to S? We are checking that for every s in S, shift_d(s) ∈ S. That implies that shift_d maps S into S. Since shift_d is injective (as a function on all chords), the image of S under shift_d has size |S| = m (if shift_d is injective). But is shift_d injective on the set of all chords? Yes, because if shift_d(s1) = shift_d(s2), then applying shift_{-d} yields s1 = s2. So the mapping is a bijection from the set of all chords to itself. So the restriction to S is an injection from S to S. Since S is finite, injection implies surjection, so the image is exactly S. Therefore the condition shift_d(s) ∈ S for all s ∈ S implies S = shift_d(S). So it's sufficient.
Thus algorithm is correct.
But we need to generate divisors of n. Compute all factors d from 1 to sqrt(n), add d and n/d if d < n, n/d < n. Exclude n itself.
Now, we might further optimize: Instead of checking all divisors, we could check only k that satisfy that the multiset of differences is periodic. But divisors approach is fine.
We must also consider the case where S is empty? But m≥1. So fine.
Now, let's test with examples.
Example 1: n=12,m=6 segments: (1,3),(3,7),(5,7),(7,11),(9,11),(11,3). Check divisor d? The expected answer Yes. The symmetry is rotation by 4? Actually note says 120 degrees => n=12, so 120 degrees corresponds to 12/3=4 steps? Actually full circle 360 degrees, so 120 degrees is 1/3 of circle: n/3 = 4. So k=4. gcd(4,12)=4, which is a divisor. So our check of d=4 should succeed.
Example 2: n=9,m=6: pairs: (4,5),(5,6),(7,8),(8,9),(1,2),(2,3). Rotation by 3? 120 deg => n/3=3. gcd(3,9)=3 divisor. Yes.
Example 3: n=10,m=3: (1,2),(3,2),(7,2). Probably not symmetric. Check divisors: 1,2,5? 10's divisors: 1,2,5,10. Excluding 10: 1,2,5. Check shift by 1: shifted segments: (2,3),(4,3),(8,3)? Are these in set? No. So fails. Shift by 2: (3,4),(5,4),(9,4)? Not in set. Shift by 5: (6,7),(8,7),(2,7)? Not. So No.
Example 4: n=10,m=2: (1,6),(2,7). Rotating by 5? Actually 5 steps maps 1->6, 6->1? So (1,6) maps to (6,1) same chord. (2,7) maps to (7,2) same. So shift by 5 works. 5 is divisor of 10 (5). So Yes.
Thus algorithm works.
Potential pitfalls: Large n, m, but m up to 200k, okay.
But there's a nuance: The shift operation is modulo n. We must ensure that we handle mod correctly for numbers 1..n. Since n up to 100k, we can compute a' = ((a-1 + d) % n) + 1. That's fine.
Time Complexity: O(m * d(n)) where d(n) is number of divisors of n. That's at most around 128, so about 25M. That's fine.
But we can perhaps improve by early break: if m is not a multiple of something? Not needed.
However, there is one more nuance: The divisor d we test might be large, but still O(m) loops. Works.
Now, we need to be careful with hash collisions? Using unordered_set of 64-bit ints is safe; collisions possible but improbable.
Alternatively, we can use a bitset or something else? Not needed.
Now, implement.
Steps:
- Read n, m.
- Initialize vector<pair<int,int>> segments; // normalized
Also unordered_set segSet; // encode as a * (n+1) + b (since b <= n)
For each i, read a,b; if a>b swap; store as (a,b) in vector, and insert a*(n+1)+b into set.
(Use 1-index, n+1 is safe: n+1 <= 100001)
- Compute divisors: vector divs; for d from 1 to sqrt(n): if n%d == 0: if d < n: divs.push_back(d); if d != n/d and n/d < n: divs.push_back(n/d); (but careful not to include n itself; if d==1, n/d==n, exclude; if d==n, not included because d <= sqrt(n) < n for n>1). So condition: if d < n: add d; if other = n/d, if other != d and other < n: add other.
Or simpler: iterate over all i from 1 to n-1? No, only divisors.
- For each d in divs:
- bool ok = true;
- For each segment (a,b) in segments:
compute a2 = ((a-1 + d) % n) + 1;
b2 = ((b-1 + d) % n) + 1;
if (a2 > b2) swap(a2,b2);
long long code = (long long)a2 * (n+1) + b2;
if (segSet.find(code) == segSet.end()) { ok = false; break; }
- If ok, output "Yes" and return.
- After loop, output "No".
That's it.
But wait: Are we missing the possibility that k could be a multiple of a divisor that we didn't test? No, because if k works, then d = gcd(k,n) works, and we test all divisors, including that d. So if any k works, we will output Yes.
But is it possible that d = n (i.e., k=0)? Not allowed. So we exclude n.
Now, test with a scenario: Suppose n=6, segments: all 3 diameters? Actually n=6 even, diameters: (1,4), (2,5), (3,6). Rotating by 2 maps (1,4) -> (3,6) -> (5,2) -> etc. Check d=2? d=2 works because set of diameters is invariant under any rotation that is multiple of 2? Actually check: shift by 2: (1,4)->(3,6) present; (2,5)->(4,1) which is same as (1,4) present; (3,6)->(5,2) present. So yes. Also d=3? shift by 3: (1,4)->(4,1) present; (2,5)->(5,2) present; (3,6)->(6,3) present. So d=3 works as well. d=1? shift by 1: (1,4)->(2,5) present; (2,5)->(3,6) present; (3,6)->(4,1) present. So d=1 works, meaning full symmetry. So indeed if set is fully symmetric under all rotations (d=1 works), then all divisors also work. So it's fine.
Edge Cases: m=1. Check divisors. Example: n=10, m=1, segment (1,6). Rotations: (1,6) is a diameter? Actually 10 even, but 6-1=5 = n/2, yes diameter. So shift by 5 maps to itself. So d=5 works. So answer Yes. Our algorithm: divisors of 10: 1,2,5. Check d=1: shift (1,6) -> (2,7) not in set, fails. d=2 fails. d=5: shift (1,6) -> (6,1) normalized same, present. So Yes.
If segment is not symmetric, e.g., (1,2) only. Then only shift by 0 gives itself. So no divisor works. So No.
Now, what about segments that are symmetric under some rotation but not all? As in example 4: (1,6) and (2,7) both diameters? Actually n=10, (1,6) diameter, (2,7) diameter. Shift by 5 maps each to itself. So d=5 works. Divisors: 5 works. Good.
Now, consider a more complex configuration where invariance holds for k=4 on n=12 but not for d=4? Already works. But what about a configuration that is invariant under shift by 8 (k=8) but not under shift by 4? But if it's invariant under shift by 8, then applying it twice gives shift by 16 ≡ 4 mod 12? Since 8+8=16 mod12=4. So shift by 8 is symmetry => shift by 4 = shift_8(shift_8) is symmetry? Let's check: If S = shift_8(S), then applying shift_8 again: shift_8(shift_8(S)) = shift_16(S) = shift_4(S). But we also have shift_8(S) = S, so shift_4(S) = shift_8(shift_8(S)) = shift_8(S) = S? Actually careful: shift_8(S) = S. Then apply shift_8 to both sides: shift_8(shift_8(S)) = shift_8(S) => shift_16(S) = S. Since 16 mod 12 = 4, we get shift_4(S) = S. So indeed invariance under shift by 8 implies invariance under shift by 4. So d = gcd(8,12)=4 also works. So it's consistent.
Thus checking divisors is safe.
Now, complexity: O(m * number_of_divisors). Acceptable.
Potential memory: Store segments vector of size m, and set of size m. Fine.
Now, we should consider the case where n is large but m is small, e.g., m=1. The loops over divisors still fine.
But there might be a case where number of divisors is large (like 128) and m is also 200k, so 25.6M operations. Each operation includes modulo, multiplication, and hash lookup. Should be okay in < 1 sec in C++ with optimization.
But can we do better? Perhaps we could check only k that are divisors of n? Already doing that. Also we can precompute the shifted codes for each segment for each d? Not necessary.
One more nuance: The modulo operation (a-1+d)%n could be replaced by addition and if >= n subtract n, which may be faster. But fine.
Now, we must also consider that the hash set might have collisions and rehashing. We'll reserve enough capacity.
Now, is there any possibility that the symmetry exists for k but we missed because we didn't test k where gcd(k,n) = d but d does not work but k does? As argued earlier, if k works then d works. So no.
Thus solution is ready.
But wait, there is a potential nuance: The rotation is clockwise by k units. In the problem, they say integer k (1 ≤ k < n), such that if all segments are rotated clockwise around the center of the circle by k units, the new image will be the same as the original one. This is exactly what we are checking.
Now, we need to consider if segments are considered as undirected? Yes, they are just segments, orientation doesn't matter.
Now, the algorithm as described seems straightforward.
However, there is a catch: The set S may have multiple chords that are the same after shift? But we already canonicalized.
But what about the scenario where the set S is empty? Not possible.
Now, let's test with a more tricky case: Suppose n=6, segments: (1,2), (3,4), (5,6). This set is invariant under shift by 2? Check: shift by 2: (1,2)->(3,4) present; (3,4)->(5,6) present; (5,6)->(1,2) present. So yes. gcd(2,6)=2 works. Our algorithm: divisors: 1,2,3? 3 also divisor? 3<6, yes. Check d=2 works. Output Yes.
Now, suppose segments: (1,2), (4,5). n=6. Check d=1? shift by 1: (1,2)->(2,3) not present, fails. d=2: (1,2)->(3,4) not present; fails. d=3: (1,2)->(4,5) present? (1,2) shift by 3 -> (4,5) present, and (4,5) shift by 3 -> (1,2) present, so d=3 works. gcd(3,6)=3, yes. So answer Yes. That matches.
Now, consider scenario where there is symmetry under shift by 2 but not under shift by 4? In n=8, shift by 2 and shift by 4 are both divisors (2 and 4). If invariance under 2, then invariance under 4 might or might not hold? Let's see: If invariance under 2, then applying twice gives invariance under 4? shift_2(S)=S => shift_4(S)=shift_2(shift_2(S))=S. So yes, invariance under 2 implies invariance under 4. So if a set is invariant under 2, it's automatically invariant under 4? Actually careful: shift_2(S)=S. Then shift_4(S) = shift_2(shift_2(S)) = shift_2(S) = S. So yes, invariance under 2 implies invariance under any multiple, including 4. So if we have invariance under 2, d=2 works, and we would find that. But also d=4 would also work, but we might stop earlier at d=2. So it's fine.
Now, think about the possibility that the set S is invariant under shift by k, but k is not a divisor, but we only check divisors. As argued, gcd works. So we are safe.
Thus algorithm is sound.
Now, let's consider potential pitfalls due to modulo normalization for the endpoints when computing shifted version. For large n, (a-1+d) could exceed 2*n, but we do modulo.
Implementation details:
- Read input using scanf/printf or fast cin/cout with sync.
- Use vector of pairs<int,int> segs(m).
- Use unordered_set Set. Possibly set.reserve(2*m) to avoid rehashing.
But 64-bit hash: encode = ( (long long) a ) * (n+1) + b. Since n+1 up to 100001, multiplication yields up to 1e10, fits in long long. We'll use this encoding.
But need to be careful: The encoding must be unique for each unordered pair (a,b) with a<b. It is.
Now, one more nuance: In the set, we store the code for each segment. When we check shifted segment, we compute code similarly. Good.
Now, what about a segment where a = b? Not allowed (a_i ≠ b_i). So no self-loops.
Now, the algorithm will correctly handle multiple test cases? The problem seems single case.
Now, is there any possibility of integer overflow when computing (a-1+d) with a up to n and d up to n-1? a-1+d up to 2n-2, modulo n is fine.
Now, we can also optimize by precomputing for each point its value under shift by each divisor? But that would be a lot.
Now, I'd like to think about alternative approach: We could represent the configuration as a cyclic string of length n with edges? But chords are not limited to adjacent points. So not.
Thus the direct approach is fine.
But before finalizing, I want to double-check the reasoning about only needing to test divisors. Suppose we have invariance under k, we can show that invariance under d = gcd(k,n) holds. Proof: Let d = gcd(k,n). There exist integers u, v such that uk + vn = d. Since rotation by n is identity, rotation by vn does nothing. So rotation by d is same as rotation by uk (applied u times). Since rotation by k is a symmetry (applied to the set yields the same set), rotation by u*k is also a symmetry (by composition). Thus rotation by d is symmetry. So yes.
Thus only need to check divisors.
Now, is it necessary to also check k = n/2 when n even, even if it's not a divisor? Actually it's a divisor if n even. So covered.
Now, complexity wise: O(m * tau(n)) where tau(n) is number of divisors. This is acceptable.
Now, but there is a nuance: if we check d=1, and the set is not symmetric under full rotation, we quickly break. But we still check all divisors if none works.
But we could also try to find smallest k such that something holds, but not needed.
Now, I'll also consider whether there is a case where invariance holds for some non-divisor k but d = gcd(k,n) fails because of some special property? The proof is mathematical: If the set is invariant under rotation by k, then for any integer t, the set is invariant under rotation by tk mod n. Since there exists t such that tk ≡ d mod n, the set is invariant under rotation by d. So it must hold. There's no loophole.
Now, implement.
Potential pitfalls in code:
- We must ensure that when we compute divisors, we don't include n itself. Check: if d < n then add; if n/d < n then add, and ensure not duplicate.
- The order of divisors doesn't matter.
Now, test with some random cases.
Edge: n=2, m=1? n>=2. Points 1 and 2. The only possible segment is (1,2). Rotating by k=1 maps (1,2) to (2,1) which is same. So answer Yes. Our algorithm: divisors of 2: 1 (since 2 excluded). Check d=1: shift (1,2) -> (2,1) normalized (1,2) present. So Yes.
Edge: n=2, m=0 not allowed.
Edge: n=100000, m=200000 random. Might be slower but still okay.
But we must be careful: For d=1, we are iterating through 200k segments, doing shift (which is just +1 mod n) and hash lookup. That's fine.
Now, we should also consider possibility of performance degradation due to unordered_set with collisions. To avoid that, we can use a custom hash: The 64-bit integer can be used directly, as the standard library has hash for long long. Usually fine.
But one more improvement: We could precompute for each segment the shifted codes for each divisor? That would be heavy (200k*128 = 25M entries). But we already do similar.
Maybe we can also sort segments and use binary search: O(m log m) per divisor. That's 200k * 128 * log(200k) ~ 200k12818 = 460M, too high. So hash set is better.
Now, we must consider that unordered_set's find operation might have high constant, but still okay for 25M.
But what about memory? unordered_set with 200k entries, each storing a long long plus overhead, maybe ~16 bytes per entry? That's ~3.2 MB plus overhead, okay.
Thus solution should pass.
Now, before coding, let's re-evaluate if there is any other hidden nuance.
Potential nuance: The chords are straight segments. Rotational symmetry means the set of chords as geometric objects is invariant. Because chords are unlabeled? Actually the points are labeled in the description. But after rotation, the points are mapped to new points. The image is considered the same if the set of segments coincides exactly. So it's exactly the set invariance under rotation of the labels. So our modeling is correct.
Now, note: The problem says "the new image will be the same as the original one." In geometry, if you rotate the disc, the positions of points are also rotated, so the labeling of points is not fixed. The points are fixed on the disc? Actually the points are placed at equally spaced positions on the circumference. After rotation, the points move to new positions. So the rotated image will have a segment connecting the images of the original endpoints. So yes, it's a mapping of point indices by +k.
Thus correct.
Now, another nuance: The input guarantees no segments coincide, but doesn't guarantee that there are no overlapping chords? But they are distinct segments.
Now, we must also consider that chords are undirected, so (a,b) and (b,a) represent the same segment. Input presumably gives each segment once. We canonicalize.
Now, think about the possibility that the set S is invariant under shift by k, but our check might fail due to the way we canonicalize? Example: Suppose we have a segment (1, n) (i.e., a=1, b=n). Normalized as (1,n). Shift by d: a' = 1+d mod n? Let's compute: For n=5, segment (5,1) normalized as (1,5). Shift by d=1: a' = 2, b' = 1? Actually (1+1)%5+1 = 2? Wait compute: ((1-1+1)%5)+1 = 1%5+1? Let's do: a=1 => (0+1)%5 =1 => +1 =2. b=5 => (4+1)%5 =0 => +1 =1. So shifted pair is (2,1) which normalized becomes (1,2). So check if (1,2) exists. That's correct.
Thus no issue.
Now, is there any scenario where the set S might be invariant under some rotation but our check fails because the shift might map a segment to itself but with endpoints swapped and our canonicalization yields same code, so it's fine.
Now, final note: The algorithm is O(m * number_of_divisors). However, there is a subtle point: if n is large and has many divisors, but m is also large, it's okay. But what about worst-case: n = 99792? Actually need to compute maximum tau(n) for n ≤ 100000. According to known data, tau( 98280 ) = 128? Let's check: 98280 = 2^3 * 3^3 * 5 * 7 * 13? Actually I recall 83160 has 128 divisors. 98280 also 128. So maximum is 128. So m=200k, total operations 25.6M. This is fine.
But we should also note that for each segment we compute two modulo operations. That's okay.
Now, let's also think about an alternative approach: Instead of checking all divisors, we could compute a fingerprint of the configuration as a polynomial or something and check if it's periodic. But not necessary.
Thus final answer: implement as described.
Potential improvements: We can precompute for each segment a list of shifted codes for all divisors? Not necessary.
Now, confirm with the examples:
Example 1:
n=12, m=6, segments as given. Our algorithm should find d=4 works.
Let's simulate quickly: Compute all divisors of 12 less than 12: 1,2,3,4,6? 6 is <12, yes. 6 is also divisor. Check d=1? Probably fails. d=2? fails? Possibly? d=3? maybe? Actually rotation by 3? Check segments: (1,3)->(4,6) is that in set? (4,6) not present. So fails. d=4? shift (1,3)->(5,7): is (5,7) present? Yes. (3,7)->(7,11): present? (7,11) present. (5,7)->(9,11): present. (7,11)->(11,3): present. (9,11)->(1,3): present. (11,3)->(3,7): present. So passes. So Yes.
Example 2: n=9 divisors: 1,3? Also 9? exclude. So check d=1? shift by 1: (4,5)->(5,6) present? (5,6) present, (5,6)->(6,7)? not present, so fails. d=3: shift (4,5)->(7,8) present, (5,6)->(8,9) present, (7,8)->(1,2) present, (8,9)->(2,3) present, (1,2)->(4,5) present, (2,3)->(5,6) present. So passes.
Example 3: we already said fails.
Example 4: passes.
Thus algorithm yields correct outputs.
Now, could there be a situation where the set is symmetric under rotation by k, but we fail to detect because we only check divisors, and d = gcd(k,n) works, but the algorithm may skip that divisor if we only generate divisors less than n? We include all proper divisors, so d is included.
But wait: Suppose n = 12, and invariance under k = 8. gcd(8,12)=4, we test d=4. So we catch it. Good.
Now, what about invariance under k = 0? Not allowed.
Now, what about invariance under k = 6 in n=12? gcd(6,12)=6, test d=6. So if invariance under 6 works, d=6 works. So we need to include divisor 6.
Thus we must include all divisors less than n.
Now, is there a scenario where invariance holds for k = n/2 when n is even? That's divisor.
All good.
Now, one more nuance: The problem statement says "if there is an integer k (1 ≤ k < n), such that if all segments are rotated clockwise around the center of the circle by k units, the new image will be the same as the original one." This is exactly our condition. So we are done.
Now, we can implement.
Potential pitfalls in code: Use of modulo with negative numbers? Not needed.
Now, final note: There might be a more efficient approach using string hashing or constructing a difference multiset. But not needed.
Now, I'll write the solution in C++.
Implementation steps:
-
Include <bits/stdc++.h> using namespace std;
-
Read n,m.
-
vector<pair<int,int>> segs; segs.reserve(m);
unordered_set segSet; segSet.reserve(m*2); // maybe set size m.
-
For i=0..m-1:
read a,b; if (a>b) swap(a,b); segs.emplace_back(a,b); segSet.insert((long long)a*(n+1)+b);
-
Compute divisors:
vector divs;
for (int d = 1; d*d <= n; ++d) {
if (n % d == 0) {
if (d < n) divs.push_back(d);
int other = n/d;
if (other != d && other < n) divs.push_back(other);
}
}
Note: d starts from 1. For n=1? Not possible (n>=2). So fine.
-
For each d in divs:
bool ok = true;
for (const auto& p : segs) {
int a = p.first, b = p.second;
// compute shifted
int a2 = a + d;
if (a2 > n) a2 -= n; // because a in [1,n], d in [1,n-1], so a+d <= 2n-1. But mod with n: if a2 > n then subtract n.
// Actually need modulo correctly: a2 = ((a-1 + d) % n) + 1. But simpler: a2 = a + d; if (a2 > n) a2 -= n;
// That works because a in 1..n, d < n, so a+d <= n + (n-1) = 2n-1. Subtracting n once if > n yields correct mod because if a+d <= n then no wrap; if > n, then a+d-n is in [1, n-1]. That's correct. But what if a+d == 2n? Not possible because max a+d = n + (n-1) = 2n-1, so at most one subtraction needed. So we can use conditional.
int a2 = a + d;
if (a2 > n) a2 -= n;
int b2 = b + d;
if (b2 > n) b2 -= n;
// normalize
if (a2 > b2) swap(a2, b2);
long long code = (long long)a2 * (n+1) + b2;
if (segSet.find(code) == segSet.end()) {
ok = false;
break;
}
}
if (ok) {
cout << "Yes" << endl;
return 0;
}
-
After loop: cout << "No" << endl;
Now, check with example where d = n-1? That's not a divisor necessarily. But if n is e.g., 10, d=9 is not a divisor, but if invariance holds for k=9, then gcd(9,10)=1, so d=1 should work. So we will test d=1. So it's covered.
Now, but is it possible that invariance holds for k = n-1 but not for k = 1? Let's test: If set is invariant under rotation by 9 in n=10, that means S = shift_9(S). Then shift_9 applied twice gives shift_18 = shift_8 (mod 10). That may not be S? Actually if shift_9 is symmetry, then shift_9^2 = shift_18 mod10 = shift_8 should also be symmetry (since composition of symmetries). So shift_8 is symmetry, then shift_8^? Eventually we might get shift_2? Actually let's check: Starting from symmetry under shift_9, then shift_9^2 = shift_8, shift_9^3 = shift_7, ..., shift_9^9 = shift_1? Because 9*9=81 mod10=1. So indeed, if shift_9 is symmetry, then shift_1 is shift_9^9, which is symmetry. So invariance under shift_9 implies invariance under shift_1. So d=1 works. So yes.
Thus checking only divisors is sufficient.
Now, we might consider optimizing by checking divisors in increasing order? Not necessary.
Now, we should also consider the possibility that m is large and many divisors fail early. Our loop breaks at first missing segment. That's good.
Now, test with a case where d=1 is actually a symmetry (full rotational symmetry). That would require that for every chord of a given type, all rotations are present. So if S is not all chords, it might still be fully symmetric if it consists of entire orbits. For example, S contains all diameters (if n even) is symmetric under all rotations? Rotating a set of all diameters by 1 yields all diameters again (since the set of all diameters is the set of all pairs opposite). So that set is fully symmetric (d=1 works). So if we have all diameters, d=1 works, so answer Yes. Our algorithm will detect d=1 quickly. So output Yes.
Now, verify if we need to treat large n with many divisors. The code should be fine.
Now, potential issue: The hash set's key is a 64-bit integer derived from a and b. But we must ensure that (n+1) is enough to guarantee uniqueness. Since a and b are between 1 and n, with a<b, we can encode as a*(n+1)+b. This is unique because b <= n, so a*(n+1)+b yields a distinct number for each ordered pair (a,b) with a <= n, b <= n. Since we enforce a<b, no overlap. Good.
Now, but note: n+1 may overflow int when multiplied by a, but we cast to long long.
Now, I'll provide final code.
But before finalizing, consider if there is a more efficient method to avoid iterating over all divisors in worst case? For n with many divisors but m also large, 25M operations is okay. But we need to ensure that the constant factor is low.
Let's do quick estimate: 25 million * (a few operations) ~ 0.1-0.2 seconds maybe? In C++ with optimization, likely under 1 second.
But to be safe, we can also add an early check: If m is odd and we test k that yields? Not necessary.
Now, also note: We can reduce number of divisors we test by only testing those that are possible based on the structure of segments? For instance, the rotation k must map the multiset of differences. But not needed.
Now, implement.
Potential small improvements: Use vector for segs as two separate arrays? Not needed.
Now, one more nuance: When we compute shifted endpoints using if > n subtract n, we must be careful: Since d could be 0? No.
But what about d = n? Not included.
Now, confirm that a+d can be equal to n exactly? If a <= n, d <= n-1, a+d could be exactly n when a = n - d. Then a+d = n, which is not > n, so a2 = n (since a+d = n). That's correct: point n stays n? Actually rotating by d: if a = n - d + 1? Let's check: a = n-d? Suppose n=10, d=3, a=7, then a+3=10, which should map to point 10? Actually points are 1..10. Rotating 7 by 3 steps clockwise: 7->8,9,10 => point 10. So a2=10 correct. So condition a2 > n subtract n would not trigger because a2 == n is not > n. So correct.
Now, what about b+d = n? similar.
Now, what about a+d = n+1? Then subtract n gives 1. That's correct.
Thus fine.
Now, we should also consider that we may have to perform many modulo operations; but our simple conditional is efficient.
Now, code.
But before final code, I want to think about potential pitfalls: Input lines may have duplicate segments? Guaranteed no segments coincide, so okay.
Now, what about a segment where after shift, the two endpoints are same? That would happen if a=b? Not possible because a != b.
Now, what about a segment where after shift, the normalized order is same but code may be different? Already considered.
Now, we should also consider that the set S might be invariant under shift by d, but the shift might map a segment to a different segment that is also present, but our check might incorrectly fail if there are collisions in hash? Not likely.
Now, final answer: Implement as described.
I'll produce the final solution.
But let's also consider alternative approach that might be simpler: Represent each segment by the difference vector ( (b-a) mod n, but unordered). The set is symmetric if the multiset of differences is invariant under rotation of the indices? Actually each segment is associated with its starting point? Not exactly.
But I'll stick with the divisor check.
Now, before finishing, I want to double-check with a tricky scenario: n=6, segments: (1,4), (2,5), (3,6). This set is all diameters. Our algorithm: divisors: 1,2,3,6? exclude 6, so 1,2,3. Check d=1: shift (1,4)->(2,5) present; (2,5)->(3,6) present; (3,6)->(4,1) which is (1,4) present. So d=1 passes, so Yes. That's correct: the image is rotationally symmetric; any rotation maps diameters to diameters.
Now, consider a set of diameters but missing one: n=6, segments: (1,4), (2,5). Is this symmetric? Rotating by 2: (1,4)->(3,6) not present, so not invariant under 2. Rotating by 3: (1,4)->(4,1) same, (2,5)->(5,2) same. So d=3 works? Check shift by 3: (1,4) -> (4,1) present; (2,5) -> (5,2) present. So set is invariant under rotation by 3. So Yes. Our algorithm: n=6 divisors: 1,2,3. d=3 passes. So Yes.
Now, consider set: (1,4), (3,6). Rotating by 2: (1,4)->(3,6) present; (3,6)->(5,2) which is (2,5) not present. So fails. Rotating by 3: (1,4)->(4,1) present; (3,6)->(6,3) present. So d=3 works. So Yes.
Now, consider set: (1,4) only. d=3 works? (1,4) -> (4,1) present. So Yes. d=3 is divisor. So Yes.
Now, consider set: (1,4), (2,3). Rotating by 2? (1,4)->(3,6) not present. Rotating by 3? (1,4)->(4,1) present; (2,3)->(5,6) not present. Rotating by 1? (1,4)->(2,5) not present. So no symmetry. So No.
Thus algorithm would give correct.
Now, what about chords that are not diameters? e.g., n=12, set: all chords of length 2 (i.e., pairs (i,i+2))? Actually length meaning difference 2 mod12. That set has 12 chords (since each i from 1..12 gives chord (i,i+2) mod12). This set is invariant under rotation by 1? Because shift by 1 maps (i,i+2) to (i+1,i+3) which is also a chord of length 2. So d=1 works. So Yes.
If only some of them, e.g., (1,3), (4,6), (7,9), (10,12). This set is invariant under rotation by 3? Let's see: shift by 3: (1,3)->(4,6) present; (4,6)->(7,9) present; (7,9)->(10,12) present; (10,12)->(1,3) present. So d=3 works. So Yes.
Our algorithm would detect d=3.
Now, consider a set that is invariant under shift by 4 but not under 2? But as argued, invariance under 4 implies invariance under 2? Actually check: shift by 4: (1,5) -> (5,9) -> (9,1) -> etc. But if the set contains (1,5) and (5,9) but not (9,1)? Then shift by 4 applied to (1,5) gives (5,9) present; applied to (5,9) gives (9,1) which might not be present; so not invariant. So to be invariant under 4, the set must be closed under repeated application. That includes shift by 8 and shift by 12(=0) and shift by 4, and shift by 8. But does it require shift by 2? No, because applying shift by 4 twice gives shift by 8, not 2. So invariance under 4 does not automatically guarantee invariance under 2. But if invariance under 4 holds, does it guarantee invariance under 2? Not necessarily. Let's construct a counterexample: n=8, k=4. Suppose S = { (1,3), (5,7) }. Check shift by 4: (1,3)->(5,7) present; (5,7)->(1,3) present. So S is invariant under shift by 4. Is it invariant under shift by 2? shift by 2: (1,3)->(3,5) not in S; fails. So invariance under 4 does not imply invariance under 2. But our earlier claim that invariance under k implies invariance under d = gcd(k,n) still holds: here k=4, n=8, gcd=4. So d=4 works. We do test d=4. So we don't need to test d=2. So it's fine that invariance under 4 does not imply 2. So checking only divisors is sufficient; we might miss symmetry if we only test 2 and not 4? But we test 4 as a divisor, so we'll catch it.
Now, if invariance under 4, then d=4 is a divisor. So we test it.
Now, what about invariance under 6 for n=8? gcd(6,8)=2. If invariance under 6, then invariance under 2 must hold. So we will test 2.
Thus all good.
Now, what about the case where n is prime and the only divisor is 1. Then we only test shift by 1. So if the set is symmetric under some non-1 shift, say k=2 for n=5? gcd(2,5)=1, so invariance under 2 implies invariance under 1. So if there is any symmetry, then shift by 1 must also be a symmetry. So for prime n, either the set is fully symmetric (invariant under all rotations) or not symmetric at all. That seems plausible: because if there is a non-trivial rotational symmetry, then due to group properties, the set must be invariant under the whole group? Actually for n prime, any non-zero k is generator. So invariance under k implies invariance under all rotations. So indeed the only possible symmetry is full rotational symmetry. So our test of d=1 is correct.
Thus algorithm covers all.
Now, there is a subtlety: Could it be that the set S is empty? m≥1 so no.
Now, we also need to handle input where segments may be given in any order. Fine.
Now, let's consider performance with huge m=200k and number of divisors ~128. The inner loop of 200k * 128 = 25.6M iterations, each doing two additions, two conditional subtractions, a comparison, swap possibly, multiplication and hash lookup. That's fine.
But we must ensure that we don't accidentally copy the segments vector many times; we just iterate over constant reference.
Now, potential micro-optimizations: Use local variables for n, segs, segSet. Use auto& for loops.
Now, code.
We'll implement as:
int n, m;
cin >> n >> m;
vector<pair<int,int>> segs(m);
unordered_set segSet;
segSet.reserve(m * 2);
for (int i=0;i<m;i++) {
int a,b;
cin >> a >> b;
if (a > b) swap(a,b);
segs[i] = {a,b};
segSet.insert((long long)a * (n+1) + b);
}
// compute divisors
vector divs;
for (int d=1; d*d <= n; ++d) {
if (n % d == 0) {
if (d < n) divs.push_back(d);
int other = n/d;
if (other != d && other < n) divs.push_back(other);
}
}
// sort divisors? Not needed.
for (int d : divs) {
bool ok = true;
for (const auto& p : segs) {
int a = p.first, b = p.second;
int a2 = a + d;
if (a2 > n) a2 -= n;
int b2 = b + d;
if (b2 > n) b2 -= n;
if (a2 > b2) swap(a2, b2);
long long code = (long long)a2 * (n+1) + b2;
if (!segSet.count(code)) {
ok = false;
break;
}
}
if (ok) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
Now, I'll test with examples manually or think of edge cases.
Potential bug: For d such that a+d might be > n, we subtract n once. But what if a+d > 2n? Not possible because d < n, a <= n, so a+d <= 2n-1. So one subtraction enough.
But what about when a+d = n? That's not > n, so no subtraction; a2 = n, correct.
Now, what about when b+d > n? Similar.
Now, consider d = 0? Not in divisors.
Now, what about segment with a= n? Then a+d > n if d>=1, so subtract n gives d. So a2 = d. For example, n=10, a=10, d=3 => a2 = 13 -> 13>10 subtract ->3. That matches ((10-1+3)%10)+1 = (12%10)+1=2+1=3? Wait: (10-1+3)=12, 12%10=2, +1=3. Yes. So correct.
Now, test with d = n-1, but we are not testing it unless it's a divisor. But if it were tested, a=1, d=n-1: a+d = n, not > n? Actually n-1+1 = n, so no subtraction: a2 = n. That is correct: rotating 1 by n-1 steps clockwise is equivalent to rotating 1 by -1 (i.e., counterclockwise) which gives n. So correct.
But we only test divisors, not all d. So fine.
Now, one more nuance: Could there be a symmetry where k is not a divisor but we didn't test it, and gcd(k,n) is a divisor but fails? According to proof, if k works, gcd(k,n) works. So if k works, the divisor works. So our test would catch it. So no.
Now, I'm satisfied.
Now, let's think about potential memory/time issues with unordered_set. There is a possibility that collisions degrade performance. But we can use a custom hash to reduce collisions: maybe use a simple hash like using the 64-bit integer itself, which is the identity function; unordered_set's hash for 64-bit integer is usually good. We can also use std::set with O(log m) but that would be slower (25M * log 200k ~ 25M*18 = 450M, maybe borderline). So unordered_set is better.
We can also use a boolean array of size n*n? Not possible (1e10).
So unordered_set is fine.
We could also use a bitset of all possible chords? Not possible.
Thus solution.
Now, let's double-check the encoding uniqueness: For a and b between 1 and n, we use a*(n+1)+b. Since b <= n, and a <= n, the maximum is n*(n+1)+n = n^2+2n. Since n <= 100k, n^2 is 1e10, which fits in 64-bit. So okay.
But be careful: For a=100000, b=100000, but a<b is enforced so b cannot equal a. So we don't have a=b, but still encoding works.
Now, we might also store the segment as pair<int,int> in a set using custom hash; but the integer encoding is simple.
Now, possible improvement: Since we use (n+1) as multiplier, we can precompute mult = n+1.
Now, I'll produce final code.
But before final, I want to test with random small cases to ensure correctness. Let's write a quick mental test:
n=4, all possible chords? Actually all possible unordered pairs: (1,2),(1,3),(1,4),(2,3),(2,4),(3,4). This set is symmetric under all rotations, so d=1 works. Divisors: 1,2 (since 4's divisors: 1,2,4). Check d=1: for each segment, shift by 1: (1,2)->(2,3) present; (1,3)->(2,4) present; (1,4)->(2,1) = (1,2) present; (2,3)->(3,4) present; (2,4)->(3,1) = (1,3) present; (3,4)->(4,1) = (1,4) present. So passes.
Now, subset: (1,2),(2,3),(3,4),(4,1). This is the cycle around the circle? Actually these are edges of the quadrilateral. Rotating by 1 maps (1,2)->(2,3) present; (2,3)->(3,4) present; (3,4)->(4,1) present; (4,1)->(1,2) present. So d=1 works. So full symmetry.
Now, subset: (1,3),(2,4) (the two diagonals). Rotating by 1: (1,3)->(2,4) present; (2,4)->(3,1) = (1,3) present. So d=1 works. So yes.
Now, subset: (1,3) only. Rotating by 2: (1,3)->(3,1) same, so d=2 works. gcd(2,4)=2 divisor, test d=2: passes. So Yes.
Now, subset: (1,2),(3,4). Rotating by 2: (1,2)->(3,4) present; (3,4)->(1,2) present. So d=2 works. So Yes.
Now, subset: (1,2),(2,3). Rotating by? d=1? (1,2)->(2,3) present; (2,3)->(3,4) not present. d=2? (1,2)->(3,4) not present. d=1? fails. So No.
Thus seems fine.
Now, any scenario where invariance holds but our algorithm fails due to normalization with min/max? No.
Now, we might need to consider that after shifting, the two endpoints may be the same? Not possible because shift is bijection and a != b => a' != b'.
Now, output "Yes" or "No" with correct case? The problem says you can output each letter in any case. We'll output exactly "Yes" or "No".
Now, final code.
One more thought: For large n, number of divisors may include 1 always. So we will always test d=1. In many cases, d=1 will fail quickly if the set is not fully symmetric. That's fine.
Now, possible optimization: Before checking all divisors, we could precompute the smallest period of the "difference multiset"? Not needed.
Now, ensure we use fast I/O: ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
Now, finalize.
But wait: Could there be a scenario where the set S is symmetric under rotation by k, but when we test d = gcd(k,n), we might have d = 1, and we test d=1 and it fails because we only check that each segment's shifted version is present. But if the set is symmetric under k, we argued d=1 should also be a symmetry. Is that always true? Let's verify the proof: For n=6, k=2. If shift_2(S)=S, then does shift_1(S)=S? shift_2 generates subgroup {0,2,4}. Does shift_1 belong to that subgroup? No, because 1 is not a multiple of 2 mod 6. But we can get shift_1 as combination of shift_2? Because 2 and 6 are not coprime; the subgroup generated by 2 is {0,2,4}. So shift_1 is not in that subgroup. So we cannot get shift_1 from shift_2 by repeated composition. So invariance under shift_2 does NOT imply invariance under shift_1. Check our earlier example: n=6, S = {(1,4),(2,5),(3,6)}? That's all diameters; shift_2 works? Check: shift_2: (1,4)->(3,6) present; (2,5)->(4,1) present; (3,6)->(5,2) present. So shift_2 works. But shift_1 also works? shift_1: (1,4)->(2,5) present; (2,5)->(3,6) present; (3,6)->(4,1) present. So indeed shift_1 works. But does invariance under shift_2 force shift_1? Not necessarily. Let's try to find a set that is invariant under shift_2 but not under shift_1. For n=6, suppose S = {(1,4), (3,6)}? Check shift_2: (1,4)->(3,6) present; (3,6)->(5,2) not present, so fails. So need a set closed under shift_2 but not under shift_1. Let's attempt S = {(1,4), (3,6), (5,2)}? That's all diameters again. That is also invariant under shift_1. What about S = {(1,4), (3,6)} is not closed under shift_2 because shift_2(3,6) missing. So maybe there is no set invariant under shift_2 but not under shift_1 for n=6? Let's try to find a set that is a union of orbits under the subgroup generated by 2. The orbits of the group generated by shift_2 acting on unordered pairs. For each unordered pair {a,b}, its orbit under the subgroup is the set { {a+2t, b+2t} mod 6 | t=0,1,2}. Since the subgroup has size 3, orbits have size dividing 3. If S is invariant under shift_2, it must be a union of these orbits. Could such a union be not invariant under shift_1? Possibly if shift_1 does not map an orbit to itself. But if shift_1 maps an orbit to a different orbit, then S would need to contain both orbits for invariance? Actually invariance under shift_1 would require that applying shift_1 to S yields S. If S is a union of some orbits under subgroup G (generated by 2), then shift_1(S) is a union of shift_1 of those orbits. For S to be invariant under shift_1, shift_1 must permute these orbits. If it doesn't, then shift_1(S) != S. So it's possible that S is a union of some orbits under G, but not all, and shift_1 maps one of these orbits to an orbit not in S, causing shift_1(S) to differ. But does that violate invariance under shift_2? Not necessarily; shift_2 invariance only requires closure under the subgroup G, not under shift_1. So such an S might exist. Let's try to construct for n=6.
First, list all unordered pairs of {1..6}. But it's many (15). But we can think of orbits under G = {0,2,4}. Let's compute orbits of chords under the action of G. Since G has 3 elements, each orbit size is either 1 or 3 (if stabilizer trivial) or possibly 3? Stabilizer in G: chords fixed by a non-zero element of G. A non-zero element of G is shift by 2 or 4. When does shift by 2 fix an unordered pair {a,b}? It either maps a->a, b->b (impossible because shift by 2 is not identity) or swaps a and b: need a+2 ≡ b (mod6) and b+2 ≡ a (mod6) => 2 ≡ b-a and 2 ≡ a-b mod6 => 2 ≡ -2 mod6 => 4 ≡0 mod6? Not true. Actually condition for swapping: a+2 ≡ b and b+2 ≡ a => 2 ≡ b-a and 2 ≡ a-b => adding gives 4≡0 mod6? That would require 6|4, false. So no unordered pair is fixed by shift by 2 or 4? But maybe if the chord is a diameter? For n=6, diameters are opposite points: difference 3. Shift by 2: (1,4) -> (3,6). That's not the same set, but could it swap? (1,4) under shift by 2 gives (3,6), not the same unordered pair. So no fixed points? However, there might be chords where the orbit size is less than 3 if the chord is invariant under some element of G. Let's examine: G = {0,2,4}. For a chord to be invariant under shift by 2, we need {a,b} = {a+2,b+2} as sets. That means either a+2 = a and b+2 = b mod6 => 2 ≡ 0 mod6 false; or a+2 = b and b+2 = a mod6 => 2 ≡ b-a and 2 ≡ a-b => 2 ≡ -(b-a) => b-a ≡ -2 mod6 => b-a ≡ 4 mod6; also from first: b-a ≡ 2 mod6? Contradiction unless 2 ≡ -2 mod6? That would be 4≡0 mod6? false. So no invariant chords under shift by 2. Similarly for shift by 4. Thus all orbits under G have size exactly 3 (since the group acts freely). Therefore, the set of all chords is partitioned into orbits of size 3. For S to be invariant under G, it must be a union of whole orbits. Now, does such a union automatically invariant under shift by 1? Not necessarily. shift by 1 maps orbits to orbits. It could map an orbit to a different orbit. If S contains some orbits but not others, shift_1 might map an orbit in S to an orbit not in S, so shift_1(S) ≠ S. Therefore, it's possible that S is invariant under shift_2 but not under shift_1. Let's try to construct an explicit example.
We need to pick some orbits (size 3 each) such that the union is closed under adding 2, but not closed under adding 1. Let's enumerate chords and compute orbits under G. Represent chord as (a,b) with a<b. We'll label points 1..6. Compute all chords and group them.
List all unordered pairs (a,b):
1: (1,2)
2: (1,3)
3: (1,4)
4: (1,5)
5: (1,6)
6: (2,3)
7: (2,4)
8: (2,5)
9: (2,6)
10: (3,4)
11: (3,5)
12: (3,6)
13: (4,5)
14: (4,6)
15: (5,6)
Now, compute orbit under shift by 2 (i.e., add 2 mod6, wrap 6->? Actually 5+2=7->1? Let's define mapping: i -> i+2 if i+2 <=6 else i+2-6. So mod6 with values 1..6: adding 2 then if >6 subtract 6.
Apply shift by 2 to each chord. Let's compute orbits. I'll do systematically:
Take (1,2): shift by 2 -> (3,4). shift by 2 again -> (5,6). shift by 2 again -> (1,2). So orbit: {(1,2), (3,4), (5,6)}.
(1,3): +2 -> (3,5). +2 -> (5,1) which is (1,5). +2 -> back to (1,3). So orbit: {(1,3), (3,5), (1,5)}? Wait check (5,1) normalized to (1,5). So yes.
(1,4): +2 -> (3,6). +2 -> (5,2) which is (2,5). +2 -> back to (1,4). So orbit: {(1,4), (3,6), (2,5)}.
(1,6): +2 -> (3,2) -> (2,3). +2 -> (5,4) -> (4,5). +2 -> back to (1,6). So orbit: {(1,6), (2,3), (4,5)}.
(2,4): +2 -> (4,6). +2 -> (6,2) -> (2,6). +2 -> back to (2,4). So orbit: {(2,4), (4,6), (2,6)}.
(2,5)? Already in orbit of (1,4). (2,5) is there.
(2,6) is in orbit of (2,4) already.
(3,4) is in orbit of (1,2). (3,5) in orbit of (1,3). (3,6) in orbit of (1,4). (4,5) in orbit of (1,6). (4,6) in orbit of (2,4). (5,6) in orbit of (1,2).
Now, what about (1,5) we have. (1,5) is in orbit of (1,3). (2,3) is in orbit of (1,6). (2,4) we have. (2,6) we have. (3,5) we have. (3,6) we have. (4,5) we have. (4,6) we have. (5,6) we have. That covers all 15 chords, partitioned into 5 orbits of size 3 each. Indeed:
Orbit A: (1,2),(3,4),(5,6)
Orbit B: (1,3),(3,5),(1,5) [Wait (1,5) appears, but note (1,5) is not in order? Actually (1,5) is a chord.]
Orbit C: (1,4),(3,6),(2,5)
Orbit D: (1,6),(2,3),(4,5)
Orbit E: (2,4),(4,6),(2,6)
Now, for S to be invariant under shift by 2, S must be a union of whole orbits. So possible S are unions of some of these 5 orbits.
Now, we need S that is invariant under shift by 2 but not under shift by 1. Let's test a candidate: S = Orbit A only = {(1,2),(3,4),(5,6)}. Check shift by 1: (1,2) -> (2,3) which is not in S (since Orbit D). So shift_1(S) != S. So S is not invariant under shift_1. But is S invariant under shift_2? Check: shift_2 of (1,2) is (3,4) which is in S; (3,4) -> (5,6) in S; (5,6) -> (1,2) in S. So yes, S is invariant under shift_2. And S is not empty, m=3. So there exists a set S that is invariant under k=2 but not under k=1. According to our earlier reasoning, if invariance under k=2 holds, then d = gcd(2,6)=2 also works. That's fine. So we will test divisor d=2 and find it works, output Yes. So our algorithm would output Yes because d=2 is a divisor and it works. So we don't need shift_1.
Now, what about invariance under k=4? That is same as shift by 2 essentially, because gcd(4,6)=2, so shift by 4 also works? But shift by 4 is in the subgroup generated by 2? Actually shift by 4 = shift_2 twice. So if S is invariant under shift_2, it's also invariant under shift_4. So d=2 works.
Now, this example shows that invariance under a non-divisor? 2 is divisor, so fine.
Now, consider invariance under k=3 for n=6. gcd(3,6)=3, so d=3 works. Check if there is a set invariant under 3 but not under 1? Yes, e.g., Orbit? Let's see orbits under shift by 3: shift by 3 generates subgroup {0,3}. Orbits size 2 (since 3 is an involution). Example: S = {(1,4)} only? Check shift by 3: (1,4)->(4,1) same, so invariant. But also shift by 1? Not invariant. So d=3 works. So we will test d=3 and find it.
Thus algorithm works.
Now, what about invariance under k=2 for n=8? Let's test a possible set: orbits under shift by 2. n=8, shift by 2 generates subgroup {0,2,4,6} of size 4. There will be orbits of size 4 (or smaller if stabilized). Could there be a set invariant under shift by 2 but not under shift by 1? Yes, e.g., S = {(1,3), (3,5), (5,7), (7,1)}? Actually that's full orbit of (1,3) under shift by 2? (1,3) +2 = (3,5), +2 = (5,7), +2 = (7,1) => size 4. This set is invariant under shift by 2. But shift by 1? (1,3) -> (2,4) not in S, so fails. So invariance under shift by 2 is sufficient for symmetry. Our algorithm would test d=2 (divisor) and find Yes.
Thus all good.
Now, the only worry: The set of divisors we compute includes all proper divisors. For n=8, proper divisors: 1,2,4? Actually 8's divisors: 1,2,4,8. So we test 1,2,4. So d=2 is included.
Now, what about n=12, divisors: 1,2,3,4,6. So all possible d's are covered.
Now, note: In the example we constructed for n=6, S = Orbit A, which is symmetric under shift by 2. The answer should be Yes because there exists k=2. Our algorithm would test d=2 and succeed.
Now, confirm: The problem's first example had k=4 for n=12, that's a divisor.
Thus all fine.
Now, one more nuance: The rotation is clockwise by k units. Could there be a scenario where the set is symmetric under anticlockwise rotation but not clockwise? Since k ranges from 1 to n-1, anticlockwise by k is same as clockwise by n-k. So if there is symmetry under anticlockwise, then there is symmetry under clockwise with k' = n-k (unless k'=0). So it's covered.
Now, algorithm is correct.
Now, let's consider potential issues with large n and many divisors: For each divisor, we do O(m) operations, but we also have to compute shifted endpoints for each segment. That's okay.
Now, what about memory: The vector segs stores m pairs, each two ints, 8 bytes each? Actually pair<int,int> maybe 8 bytes, plus overhead? So 200k * 8 = 1.6 MB, fine.
Set of long long: each maybe 8 bytes for key, plus overhead of unordered_set node (maybe 16-24 bytes). 200k * 24 = 4.8 MB, still okay.
Thus overall memory < 10 MB.
Now, code with custom hash for unordered_set may improve speed but not needed.
One more micro-optimization: Instead of using if (a2 > n) a2 -= n, we can use a2 = (a + d - 1) % n + 1; but modulo operation may be slower. But we can do as described.
Now, but careful: For a2 = a + d; if (a2 > n) a2 -= n; This works only if d < n and a <= n, so a+d <= 2n-1. So one subtraction enough. Yes.
Now, we need to ensure that for a2 we compute correctly when a+d = n? Not > n, so a2 = n, correct.
Now, but what about b2 similarly.
Now, double-check: For a = n, d = n-1 => a+d = 2n-1 > n, subtract n => n-1. So a2 = n-1, which is correct: rotating point n by n-1 steps clockwise gives point n-1? Actually from n, moving clockwise: 1 step goes to 1, 2 steps to 2, ..., n-1 steps to n-1. So yes.
Now, we must also consider that a2 and b2 could be equal? Not possible.
Now, we should also consider that after shifting, the pair might be the same as original but swapped, but our normalization ensures we look up correctly.
Now, edge case: When d = n/2 for even n, the shifted pair may be the same but reversed; fine.
Now, I'll implement final code.
But before final, we must be careful about integer overflow in multiplication for code: (long long)a2 * (n+1) + b2, all within 64-bit.
Now, alternative: Use unordered_set<pair<int,int>> with custom hash, but that may be slower.
Now, code.
One more thought: Should we also test k = 0? Not allowed.
Now, could there be a case where d is a divisor but the set is symmetric under shift by d but our algorithm fails due to the way we handle modulo? I think not.
Now, test with a random case using script? Not possible here.
Now, I'll produce final answer.
But before final, let's read the problem statement again to ensure no misinterpretation: "There are m straight segments on the disc, the endpoints of which are all among the aforementioned n points." It doesn't say that the segments are chords inside the disc, but they are straight segments, so they are chords (including possibly if endpoints are adjacent, the segment is along the edge? But straight segment between two points on the circumference is a chord, not necessarily the arc. So fine.)
Now, "Inaka wants to know if her image is rotationally symmetrical, i.e. if there is an integer k (1 ≤ k < n), such that if all segments are rotated clockwise around the center of the circle by k units, the new image will be the same as the original one." So indeed.
Now, the examples: first two have images that become the same after 120 deg rotation.
Thus our algorithm matches.
Now, I'm ready to write final solution.
But let's also consider if there is any faster method: We could compute the minimal rotation that maps the set to itself using polynomial hashing of the "difference sequence". However, the set is not a sequence but a set of chords. Could treat each point's adjacency list? Not necessary.
Given constraints, O(m * divisors) is acceptable.
Now, implementation details in code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int,int>> segs(m);
unordered_set<long long> segSet;
segSet.reserve(m * 2); // to avoid rehash
long long mult = n + 1;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
segs[i] = {a, b};
segSet.insert(a * mult + b);
}
vector<int> divisors;
for (int d = 1; d * d <= n; ++d) {
if (n % d == 0) {
if (d < n) divisors.push_back(d);
int other = n / d;
if (other != d && other < n) divisors.push_back(other);
}
}
// maybe sort divisors if needed, but not necessary
for (int d : divisors) {
bool ok = true;
for (const auto& p : segs) {
int a = p.first, b = p.second;
int a2 = a + d;
if (a2 > n) a2 -= n;
int b2 = b + d;
if (b2 > n) b2 -= n;
if (a2 > b2) swap(a2, b2);
long long code = a2 * mult + b2;
if (segSet.find(code) == segSet.end()) {
ok = false;
break;
}
}
if (ok) {
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
}
Now, double-check: In the code, we used mult = n+1, and computed code as a2 * mult + b2. For a2 and b2 up to n, this is fine.
Now, we might need to handle case where n=100000, d=50000, a=60000, a+d=110000 > n, subtract n gives 10000, correct.
Now, possible bug: For a2 = a + d, if a+d > 2n? Not possible. Good.
Now, test with example 4: n=10, m=2, segments: (1,6) and (2,7) after normalization? Input: 1 6, 2 7. We normalize to (1,6) and (2,7). Divisors of 10: 1,2,5,10? Actually proper divisors: 1,2,5. d=1: (1,6)->(2,7) present? (2,7) is in set, yes; (2,7)->(3,8) not present, so fails. d=2: (1,6)->(3,8) not present, fails. d=5: (1,6)->(6,1) normalized (1,6) present; (2,7)->(7,2) normalized (2,7) present. So passes. Output Yes.
Now, check example 2: n=9, m=6 segments. Divisors: 1,3? Actually 9's divisors: 1,3,9. Proper: 1,3. d=1 fails, d=3 passes. So Yes.
Now, check example 3: n=10, m=3 segments. Divisors: 1,2,5. All fail => No.
Now, example 1: n=12 divisors: 1,2,3,4,6. d=4 passes.
Thus correct.
Now, potential edge: n=1? Not allowed.
Now, is there any scenario where we need to consider rotation by k that is not a divisor but the gcd is a divisor that we skip because we only consider divisors less than n? We include all proper divisors, so we include all possible gcds.
Now, any scenario where d = n/2 for even n but n/2 may not be a divisor? Actually it is a divisor. So included.
Thus final answer is ready.
But let's think about the possibility of performance issues due to unordered_set collisions causing O(m) per lookup in worst-case. But with a good hash function (like using the integer directly) and appropriate load factor, average is O(1). In contests, it's generally fine.
If we want to be extra safe, we can use a custom hash like:
struct hash_pair {
size_t operator()(const pair<int,int>& p) const {
return (size_t)p.first * 100003 + p.second;
}
};
But we used integer encoding which should be fine.
Now, one more nuance: The number of divisors might be up to 128, but we may also have to test d = 0? Not.
Now, we can also sort divisors in increasing order to possibly find smaller k quicker, but not needed.
Now, the code is ready.
Now, I'll produce final answer in the chat. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> segs(m);
unordered_set<long long> segSet;
segSet.reserve(2 * m); // avoid rehashing
const long long mult = n + 1; // for encoding
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
segs[i] = {a, b};
segSet.insert(a * mult + b);
}
// collect all proper divisors of n
vector<int> divisors;
for (int d = 1; d * d <= n; ++d) {
if (n % d == 0) {
if (d < n) divisors.push_back(d);
int other = n / d;
if (other != d && other < n) divisors.push_back(other);
}
}
// test each divisor
for (int d : divisors) {
bool ok = true;
for (const auto& p : segs) {
int a = p.first, b = p.second;
int a2 = a + d;
if (a2 > n) a2 -= n;
int b2 = b + d;
if (b2 > n) b2 -= n;
if (a2 > b2) swap(a2, b2);
long long code = a2 * mult + b2;
if (segSet.find(code) == segSet.end()) {
ok = false;
break;
}
}
if (ok) {
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
}