Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler!
However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l).
Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d).
Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y.
Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.
Input
The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.
The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin.
Output
In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler.
In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.
Examples
Input
3 250 185 230
0 185 250
Output
1
230
Input
4 250 185 230
0 20 185 250
Output
0
Input
2 300 185 230
0 300
Output
2
185 230
Note
In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.
In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
We are given a ruler of length l, with n marks at positions a1=0, a2,..., an=l (increasing). We can measure a distance d if there exist i ≤ j such that aj - ai = d. So essentially, the set of all pairwise differences (including same mark giving zero) from the set A = {a1,...,an}. Since distances are positive (except zero), we need d = x and d = y to be measurable.
We can add additional marks at any integer positions between 0 and l inclusive (non-negative integer distances from origin) to help. We need to minimize the number of added marks, and output that number and any set of positions.
We need to find the minimal number v such that by adding v integer points (not already existing presumably, but could be existing? Actually we can add marks at any integer non-negative distance not exceeding the length. If we add a mark at a position that already has a mark, that's redundant but allowed? But it would not help. So we will assume we add distinct new marks that are not already present, but it's not required to be distinct? The problem says "add marks", typically it's understood that you add new marks; you could add at existing but it's wasteful. So we should add positions not in A. But it doesn't matter.
Goal: make both x and y measurable.
Observations: The set of measurable distances from a set A is all differences a_i - a_j (i>j). So it's the set D = {a_i - a_j | 1 ≤ j < i ≤ n}. This set is subset of integers (since positions are integers). So we need x ∈ D and y ∈ D.
We have initial marks A. Check if x is already measurable; similarly y.
Case 0: both measurable: v=0, no marks needed.
Case 1: only one of them is measurable. Then we need to add marks to make the missing one measurable. We can possibly add a single mark that simultaneously makes the missing distance measurable (maybe both become measurable?). But if only one missing, we may just need 1 mark. But careful: Adding a mark may cause both distances to become measurable even if initially only one missing. But we want minimum number. So if adding one mark can achieve both, that's still v=1, but if we add a mark specifically for missing, that's also 1. So v=1 is possible.
But there are situations where adding one mark cannot make both missing distances measurable if the other was already measurable? Actually we only need to make the missing measurable; the already measurable will remain measurable. So adding one mark can potentially create the missing difference with an existing mark or between two new marks? Since we only add one mark, any new difference involves that mark and existing marks (or zero difference with itself). So to measure distance d (missing), we need either: (i) there exists existing mark at p such that p + d ∈ A ∪ {new} but since new is only one, p + d could be new or existing. Actually we have new mark at position p (new). Then differences involving new mark: |p - a_i| for existing a_i. Also difference between new and itself is 0. So the set of new measurable distances after adding one mark includes all distances |p - a_i| for all i. Also we could get differences between two existing marks (already there). So to get distance d, we need either some a_i such that p - a_i = d or a_i - p = d (i.e., p = a_i ± d). Or possibly d = |p - p| =0, not relevant. So condition: ∃ a_i ∈ A such that p = a_i + d or p = a_i - d, provided p is within [0, l] and integer. Also if we add multiple marks, more combos.
Thus to make missing distance d measurable with one added mark, we need to pick p such that for some existing a_i, |p - a_i| = d and 0 ≤ p ≤ l. This means there must exist a_i with a_i + d ≤ l or a_i - d ≥ 0 such that we can place mark at that location. However, it might be that such p already exists as a mark; if so, then d would already be measurable because that difference would exist? Let's check: If there is existing a_j = a_i + d, then we already have that difference measurable (since a_j - a_i = d). So if missing d, then there is no pair a_i, a_j with difference d. That implies for any a_i, a_i + d is either > l or not an existing mark. So if we choose p = a_i + d, it will be a new mark (unless it's already existing, which would contradict missing). Similarly, p = a_i - d must be nonnegative; if it's an existing mark, then we would have a_i - that mark = d, so d would be measurable. So for missing d, both p = a_i + d and p = a_i - d are not in A (if they exist within [0,l] but not in A). However, they could coincide with another a_j? That would make d measurable, contradiction. So they are not in A. So we can place a new mark there.
Thus for missing d, it's sufficient to find any a_i such that a_i + d ≤ l or a_i - d ≥ 0; then adding that p yields d measurable. But is it always possible? Since A contains 0 and l. For d ≤ l, we have a_i = 0 gives p = d, which is within [0,l] (since d ≤ l). So p = d is always possible. But note: if d is missing, then p = d is not an existing mark (since if d were an existing mark, then 0 and d give difference d? Actually if d is a mark, then a_j = d, a_i = 0 yields d. So if d is missing, then d is not an existing mark. However, that doesn't guarantee that d is not in A. Wait: If d is not measurable, it doesn't necessarily mean that d is not in A. For example, marks: {0, 2, 5}. Measurable distances: differences: 2,5,3. d=5 is measurable because 5-0=5, even though 5 is a mark? Actually yes. So if d is a mark itself, then 0 and d give d. So if d is missing as a measurable distance, that means there is no pair of marks with difference d. That includes the pair (0, d). So if d ∈ A, then (0, d) gives d, so d would be measurable. Therefore, if d is missing, d ∉ A (except maybe d = 0, but x,y positive). So indeed, d is not a mark. So p = d is a valid integer position not in A, and within [0,l]. So by adding a single mark at d, we can measure d (since difference between d and 0 = d). Thus, for any missing distance, we can always fix it by adding a mark at that distance (provided d ≤ l, which is given x<y≤l). So the minimal number v is at most 2.
Thus the problem reduces to: Can we achieve v=0 (both already measurable)? If not, can we achieve v=1 by adding a single mark that makes both x and y measurable? This would be possible if there exists a position p (integer between 0 and l) such that after adding p, both x and y become measurable. That can happen in several ways:
The added mark p creates both distances x and y with existing marks. That is, there exist i,j such that either p - a_i = x and p - a_j = y (or absolute differences, but careful: since p can be larger or smaller, we consider p = a_i + x, or p = a_i - x, similarly for y). But note that one distance could be obtained from two different existing marks. So we need a p such that: ∃ a_i (maybe same or different) with |p - a_i| = x and ∃ a_j with |p - a_j| = y. This includes the possibility that the same a_i works for both (i.e., |p - a_i| = x and |p - a_i| = y => impossible since x≠y). So need at least one pair (or could also be that x or y becomes measurable via difference between two new marks? But only one new mark, so only differences involving new mark; difference between two existing marks is already considered. So the only way to get a new measurable distance is via the new mark. So for x (if missing) we need p such that |p - a_i| = x for some a_i; similarly for y (if missing) we need p such that |p - a_j| = y. Note if both were missing, then we need p to satisfy both; if one was already measurable, we only need p to satisfy the missing one, but it's okay if it also satisfies the other (maybe not needed). But we want v=1 overall, so if we can find p that satisfies at least the missing distances (both if both missing, or just one if one missing) without causing any issues, then v=1. Since we can always add p = d for the missing d if only one missing, that works. But that might not help the other; but that's okay because the other is already measurable. So v=1 is always possible if exactly one distance is missing? Yes, because we can add p = missing distance (if missing x, add x; if missing y, add y). But careful: Could adding p = missing distance cause the other distance to become unmeasurable? No, adding marks only adds differences, doesn't remove existing ones. So it's safe. So if exactly one of x,y is already measurable, we can add one mark at the missing distance to get it measurable. So v=1 is achievable.
But is there any edge case where adding p = missing distance fails because that position is already occupied? As argued, if missing distance d is missing, d cannot be a mark, because 0 and d would give d. So it's free. So it's fine.
Thus the only interesting case is when both x and y are missing initially. Then v could be 1 if we can add a single mark that creates both distances simultaneously. Otherwise v=2 (by adding two marks, e.g., at x and y).
But we need to output actual positions (any set). So we need to decide if there exists a p such that both x and y become measurable after adding p.
Now, we need to compute if such p exists.
Given A (set of initial marks). The condition: There exist i, j (maybe same or different) such that either:
p = a_i + x (if a_i + x ≤ l) and p = a_j + y (if a_j + y ≤ l) -> p equals two expressions, so a_i + x = a_j + y => a_j - a_i = x - y (negative, but we can also have other combos). Actually p can be derived from different conditions: p = a_i ± x, and p = a_j ± y, with the ± chosen appropriately (plus or minus). There are four possibilities: p = a_i + x, p = a_j + y; p = a_i + x, p = a_j - y; p = a_i - x, p = a_j + y; p = a_i - x, p = a_j - y. Additionally, it's possible that the same a_i works for both distances but with different signs? That would require |p - a_i| = x and |p - a_i| = y, impossible. So need at least two distinct a_i,a_j (or same a_i but one using + and the other -; but then we have p = a_i + x and p = a_i - y => a_i + x = a_i - y => x = -y impossible). So must involve two different marks, or same mark with both conditions but that's impossible.
Thus we need to find if there exist a, b in A such that p = a ± x = b ± y is within [0,l] (integer). But p must also be integer; all a_i are integers, x,y given integers? Input: x and y are positive integers? The statement says integers? It says "positive space-separated integers n, l, x, y" and "n integers a_i". So yes, everything integer. So p will be integer automatically.
But careful: The difference can be measured also if there exist i,j such that after adding p, we can get x from difference between p and some existing mark, and y from difference between two existing marks? But if both x and y were missing initially, then y is not measurable initially. So after adding p, we could get y via difference between two existing marks? No, existing marks didn't change, so y would still be missing if not created by p. But could y become measurable via difference between two existing marks? That set is unchanged. So y would remain missing unless p creates it. Similarly x. So indeed both missing distances need to be created by the new mark p. Thus we need p such that both |p - a_i| = x and |p - a_j| = y for some a_i, a_j (maybe i=j? impossible). So the condition above is necessary and sufficient.
However, there is another possibility: Could the added mark p allow measuring x via difference between p and another new mark? But we only add one mark, so no. So condition stands.
But also note: There is the possibility that adding p can create y via difference between p and itself? No.
Thus we need to test existence of p as described.
Alternatively, perhaps after adding p, x could become measurable as difference between two existing marks that were not previously considered? No, that set is unchanged.
Thus the problem reduces to: Given set A, does there exist p in [0,l] (integer) such that p ∉ A? (though it could be in A? If p ∈ A, then adding it would not change anything, but we can still add it; but then it wouldn't create new differences because it's already there? Actually if p is already a mark, then the differences involving that mark already existed, because the mark was present. Adding a duplicate does not create new differences. So if p ∈ A, it wouldn't help. So we need p not in A (or we could add a mark at a position that already exists, but that would be redundant and wouldn't create new differences; it wouldn't make the missing distances measurable because they were missing despite that mark. Wait, if p is already in A, then the differences between p and other marks were already considered. Since x was missing, that means for all i, |p - a_i| ≠ x. So p cannot create x if it's already there. So we need p not in A. So we need to find p ∉ A satisfying the condition.)
Now, there is another scenario: What if after adding p, x becomes measurable via difference between p and some other new mark? Not possible.
So solution: If both x and y are missing, we check if there exists p such that p = a_i + x and p = a_j + y for some i,j; or p = a_i + x, p = a_j - y; or p = a_i - x, p = a_j + y; or p = a_i - x, p = a_j - y.
But we also need to ensure p is within [0,l] and (maybe not already a mark? But if p is already a mark, then we would have had the difference? Let's analyze: Suppose we find p that equals a_i + x = a_j + y, and p is an existing mark. Then since p is in A, we have a_i and p in A, so difference p - a_i = x, so x would be measurable initially. Contradiction because we assumed both missing. So such p cannot be in A. Similarly for other combinations, if p = a_i + x = a_j - y, then we have p - a_i = x, so x would be measurable if p ∈ A; also a_j - p = y, so y would be measurable if both a_j and p ∈ A. So if p ∈ A, then both distances would be measurable, contradiction. So indeed any p that satisfies both conditions must not be in A. However, it could happen that p = a_i + x = a_j - y, and p not in A, but a_i + x may be equal to an existing mark? That would cause x to be measurable, so not possible. So p will automatically be non-existing. So we don't need to explicitly check membership, just check that the derived p is integer between 0 and l, and that the conditions are satisfied with respect to the given set (i.e., a_i + x = p, etc). But we also need to ensure that the required opposite operation is valid: For p = a_i - x, we need a_i - x ≥ 0; for p = a_j - y, need a_j - y ≥ 0.
Thus we can search for such p by iterating over marks and checking candidate p values, then verifying if the other distance is also achievable with some mark.
A direct O(n^2) is too slow (n up to 1e5). We need O(n log n) or similar.
Observation: The condition p = a_i + x (or a_i - x) means that p is at distance x from some mark a_i. So p must lie in the set of points that are at distance x from any mark. Similarly for y. So we want p that belongs to both sets: the set S_x = { a_i + x | a_i ∈ A, a_i + x ≤ l } ∪ { a_i - x | a_i ∈ A, a_i - x ≥ 0 } and similarly S_y. But p must be such that there exists some a_i giving x and some a_j giving y (maybe different). So p ∈ S_x ∩ S_y. Additionally, we need that p ∉ A? Actually if p ∈ A, then the condition that p is at distance x from a_i would imply x is measurable if that a_i is also in A; but that would conflict with initial missing. So such p would not appear because if p ∈ A and a_i ∈ A with difference x, then x would already be measurable; but we assumed both missing. However, it could be that p ∈ A but the a_i that gives distance x is not an existing mark? Wait, p is in A, a_i is in A (since a_i is from A). So if p = a_i + x, both in A, then x is measurable. So that cannot happen if x missing. So S_x ∩ A must be empty for the a_i we use. But S_x may contain points that are in A. However, if p ∈ A ∩ S_x, then there is some a_i with p - a_i = x, so x would be measurable. So if x is missing, S_x ∩ A = ∅? Not necessarily: Could there be a point p ∈ A that is at distance x from some a_i, but that a_i is not in A? No, a_i is from A. So p ∈ A and a_i ∈ A => x measurable. So if x missing, there is no such pair. So indeed S_x ∩ A = ∅. Actually S_x is defined using a_i in A; the set of points at distance x from A. Among those points, some may coincide with A (if there exists a_i, a_j such that a_j - a_i = x). That would imply x measurable. Since x missing, no such pair, thus S_x ∩ A = ∅. Similarly S_y ∩ A = ∅. So any p in S_x ∩ S_y is automatically not in A. Good.
Thus we just need to check if S_x ∩ S_y is non-empty, and pick any p in it. Additionally, p must be integer and within [0,l]; all such points are integers and within bounds by construction.
So we can compute S_x as a set of up to 2n values (but may have duplicates). Similarly S_y. Then we need to check if they intersect. n up to 1e5, so 2n values, we can put them into a hash set and check intersection. That would be O(n) time and O(n) memory, acceptable.
But careful: S_x may contain points that are not integer? No, all a_i, x integer so sum/diff integer.
However, there might be case where p = a_i + x = a_i - y? That would be impossible. So fine.
Thus algorithm:
Read n,l,x,y and array a[1..n].
Determine if x measurable: For each a_i, check if a_i + x ∈ A? Or more efficiently: Since array sorted, we can for each a_i, binary search if a_i + x exists. Or use set/hash. Since n up to 1e5, we can convert A to a hash set (Python set) for O(1) lookups. Similarly for y.
Actually we need to know if both x and y are measurable.
Let has_x = any (a_i + x in A) for i from 1 to n (since a_i increasing, difference from later minus earlier could be x). But note: Since a_i sorted, checking if a_i + x exists in set works because if there exists j>i with a_j = a_i + x, then that's measurable. This covers all unordered pairs because for any pair (i,j) with a_j - a_i = x, we have a_i + x = a_j, and a_i is in A, so detection works. So we can use a set for quick membership.
Alternatively, we can use two-pointer, but set is fine.
If both measurable: output 0.
Else if only one measurable:
If x missing: add mark at x (since 0 is in A, x <= l, and x not in A else x would be measurable via (0,x)). But what if adding x interferes? No. Output 1 and x.
If y missing: add mark at y. Output 1 and y.
But careful: Could there be a scenario where adding a single mark at a different position yields both missing and also maybe makes the measurable one still measurable? But we only need one missing, so add at that distance. But need to ensure that the added position is within [0,l] and integer. x and y satisfy <=l, so okay.
However, note: If only one missing, is it always possible with 1 mark? Yes, as argued, add the missing distance itself. But there is a corner case: Suppose x is missing, but x is already a mark? That can't happen because if x is a mark, then (0,x) gives x, so x measurable. So x missing => x not in A. So fine.
So for only one missing, answer is 1 and that distance.
Else (both missing):
Compute S_x = set of all a_i + x for a_i where a_i + x ≤ l, and a_i - x for a_i where a_i - x ≥ 0.
Compute S_y similarly.
Find intersection S = S_x ∩ S_y.
If S is nonempty: v=1, pick any p from S, output 1 and p.
Else: v=2, output 2 and positions x and y (or any two that work). But we must ensure that adding x and y (two marks) will indeed make both distances measurable. Adding x: difference between x and 0 = x, so x measurable. Adding y: similarly. However, is there any risk that adding both marks might create interactions that could allow both with only one? Already we determined not possible. So v=2, output x and y (ordered any). Also need to check if x and y are distinct? x<y, so distinct. And they are not in A because both missing, so not marks. So fine.
But wait: Could there be a scenario where adding two marks but we could use a different pair that also works? But minimal is 2; output x,y is fine.
But we should consider alternative: maybe we can add one mark that is not exactly at x or y but still makes both measurable? That's covered by intersection.
Thus solution seems straightforward.
But we should also consider the possibility that adding one mark could make both measurable even if one was already measurable? That would still be v=1, but since we already have case only one missing, we can just add that missing distance. That's fine.
Now, check if any nuance: Could there be a scenario where both missing but intersection nonempty, but the p in intersection may be such that adding p creates both distances, but also maybe creates some other distances, not a problem. That's fine.
But need to ensure that after adding p, both distances become measurable. Intersection ensures that p is at distance x from some a_i and at distance y from some a_j. So both distances become measurable. However, we must also ensure that these distances are indeed new; but that's okay. So v=1.
Now, is there any possibility that p could be such that it creates x via p - a_i = x, and creates y via a_j - p = y (i.e., one plus, one minus). That is included in the intersection because we considered all ±.
Thus the algorithm is correct.
But we need to be careful about duplicate positions: In building S_x, we might include a value that appears multiple times; that's fine.
Complexity: O(n) to build sets, O(n) for checking intersection (by iterating over one set and checking membership in other). Since sets are up to 2n each.
Potential issue: n up to 1e5, but l up to 1e9, values are large but fits in int (Python int fine). Use Python set.
Edge Cases:
x and y may be equal? No, given x<y.
Already both measurable: e.g., sample 2.
Both missing and intersection exists: e.g., if A contains marks that are offset such that one added mark covers both. For instance, A={0,10,20}, l=30, x=5, y=15? Check: x=5 missing? Differences: 10,20,10; 5 not present, 15 not present. S_x: from marks: 0+5=5,0-5 negative;10+5=15,10-5=5;20+5=25,20-5=15. So S_x = {5,15,25}. S_y (y=15): 0+15=15,0-15 negative;10+15=25,10-15 negative;20+15=35>30;20-15=5. So S_y = {15,25,5}. Intersection = {5,15,25}. So we can add p=5,15,25? Check: if we add p=5, then distances: 5-0=5 -> x; from 20-5=15? 20-5=15 yes, so y measured. So v=1.
Both missing but intersection empty: then v=2.
But what about case where both missing but we could add one mark that is at distance x from some mark and also at distance y from the same mark? Not possible because x≠y. So need two different marks.
Thus algorithm stands.
But let's confirm with sample 1:
Input:
3 250 185 230
0 185 250
A: {0,185,250}
Check x=185: exists? 0+185=185 in A, so x measurable. y=230? 0+230=230 not in A, 185+45=230? 185+45=230 but 45 not in A; 250-20=230 not? 250-20=230, 20 not in A. So y missing. Only one missing, so we output 1 and 230 (or 20). Sample output is 1 and 230. Works.
Sample 2: both measurable, output 0.
Sample 3: n=2, l=300, x=185, y=230, A={0,300}. Both missing? Check: 0+185=185 not in A; 300-185=115 not in A; so x missing. y missing similarly. Both missing. Compute S_x: from 0: 0+185=185,0-185 invalid; from 300: 300+185>300? 485>300 invalid; 300-185=115. So S_x={185,115}. S_y: from 0: 0+230=230; from 300:300-230=70. S_y={230,70}. Intersection empty. So v=2, output 185 230. Sample output is 2 and 185 230. Works.
So the approach matches samples.
Now, consider some tricky cases:
Could there be a case where both missing, and intersection S_x ∩ S_y is nonempty, but the p found is not integer? Already integer.
Could there be case where both missing and p is equal to an existing mark? As argued, that would imply x or y measurable. But what if p = a_i + x = a_j - y, and p equals some a_k that is not involved? But then we would have a_k - a_i = x, making x measurable; so that contradicts missing. So intersection will not contain any existing mark. However, if there is some a_k such that a_k = p, then we have x measurable. So indeed missing implies no such.
Thus safe.
Now, is there any scenario where we can achieve v=1 by adding a mark that creates one distance and the other distance becomes measurable via difference between two existing marks that already existed? That would require that other distance was already measurable, but we assumed both missing. So no.
Thus, the solution stands.
But wait: There is an alternative scenario: After adding one mark p, maybe the distance y becomes measurable not by involving p directly, but by difference between two existing marks that were not previously considered? That's impossible; the existing set is unchanged. So no.
Hence the solution is correct.
Now, we should consider the constraints: n up to 1e5, l up to 1e9. Build sets of size up to 2n, each value up to 1e9. That's fine.
Implementation details:
We need to read input, store a list. We'll create a set marks from a (for fast lookups). Then:
code
has_x = False
has_y = False
for pos in a:
if pos + x in marks:
has_x = True
if pos + y in marks:
has_y = True
But careful: we need to consider only forward differences? Since we are iterating over all pos, checking pos+x in set captures all pairs where later mark minus earlier = x. Since pos+x must be in set. That's sufficient. But what about pos - x? That would be if earlier mark minus later? Since we need any difference absolute, but we only consider positive differences, so a_j - a_i = x, where a_j > a_i. So checking pos + x covers all possibilities because for any such pair (a_i, a_j) we have a_i + x = a_j, so when iterating over a_i, we find a_j. So we don't need to check pos - x. Good.
Thus the condition for measurability: ∃ i,j with a_j - a_i = d. Equivalent to ∃ a_i such that a_i + d ∈ A.
So we can compute has_x and has_y with a loop and set.
Alternatively, using two-pointer can also work, but O(n) with set is fine.
Now, if both have, output 0.
Else if only one missing, output 1 and the missing distance.
Else (both missing):
Build set S_x = set()
for pos in a:
if pos + x <= l: S_x.add(pos + x)
if pos - x >= 0: S_x.add(pos - x)
Similarly S_y.
Then find intersection:
common = S_x.intersection(S_y)
If common:
p = any element from common (e.g., next(iter(common)))
print 1
print p
else:
print 2
print x, y (order any)
But careful: When both missing, we have to consider that adding the mark at x or y might not be necessary if we can add one mark. Our method finds intersection.
Now, is there any scenario where S_x ∩ S_y is nonempty but the p chosen might be invalid because adding that p would cause some issue? For instance, p might be at distance x from some mark but that mark is p itself? No. So it's fine.
Now, the solution seems done.
But wait: There's a nuance: The problem says "Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler." So marks can be added at any integer position, not necessarily not already occupied. But we argued that adding at occupied doesn't help. So we can safely ignore that.
Now, let's test some extra cases.
Case: n=3, l=10, x=2, y=3, marks: 0,4,10.
x measurable? 0+2=2 not in set;4+2=6 not;4-2=2 not (but that's checking a_j - a_i only forward, so 0+2=2 not, so x missing). y: 0+3=3 not;4+3=7 not; so y missing. Both missing. S_x: from 0: 0+2=2;0-2 invalid;4+2=6;4-2=2;10+2=12>10;10-2=8 => S_x={2,6,8}. S_y: 0+3=3;0-3 invalid;4+3=7;4-3=1;10+3=13>10;10-3=7 => S_y={3,7,1}. Intersection empty. So v=2, add 2 and 3. That works.
But could there be a p=5? 5 not in S_x? 5 from? For x: 5 from 7-2? Actually 5-0=5 not 2; 5-4=1; 4-5=-1. So no.
Now case where intersection exists: Suppose marks: 0,2,5, l=7, x=2,y=3. Check: x=2 measurable? 0+2=2 yes, so x measurable. So not both missing.
We need both missing but intersection exists: Already gave example {0,10,20} with x=5,y=15. Works.
Now consider an example where both missing and intersection is from minus and plus combos: marks: 0,3,8, l=10, x=2,y=5? Check: x=2 missing? 0+2=2 no;3+2=5 (5 is not in marks? 5 not in {0,3,8}, so missing; 8-2=6 no). y=5 missing? 0+5=5 no;3+5=8? 3+5=8 is in marks, so y measurable? Wait 8 is in marks, so 8-3=5, so y measurable. So not both missing.
To have both missing and intersection: let's try marks: 0,5,12, l=15, x=3,y=7. Check: x=3? 0+3=3 no;5+3=8 no;12-3=9 no; y=7?0+7=7 no;5+7=12 yes! 12-5=7, so y measurable. So not both missing.
Try marks: 0,6,13, l=20, x=4,y=7. Check: x=4? 0+4=4 no;6+4=10 no;13-4=9 no. y=7?0+7=7 no;6+7=13 yes, so y measurable. So not both.
We need y missing too. So both missing: marks where no pair difference is x or y. For intersection to exist, we need some p such that p is at distance x from some mark and at distance y from some other mark. That p may be computed. For instance marks: 0, 10, 17, l=20, x=3,y=7. Check: x=3? 0+3=3 no;10-3=7? Actually 10-3=7 not in marks; 17-3=14 no; 0+3=3 not;10+3=13 not. So missing. y=7? 0+7=7 no;10-7=3 no;10+7=17? 10+7=17 is a mark! So y measurable because 17-10=7. So not both.
We want both missing. So need to avoid any pair diff = x or y. Let's take marks: 0,4,9, l=12, x=2,y=5. Check: x=2? 0+2=2 no;4-2=2? 4-2=2 but 2 not in marks; 4+2=6;9-2=7; so missing. y=5? 0+5=5 no;4+5=9! 9-4=5, so y measurable. Not both.
Take marks: 0,6,11, l=15, x=4,y=7. x=4? 0+4=4 no;6-4=2 no;6+4=10 no;11-4=7 no; so missing. y=7? 0+7=7 no;6+7=13 no;11-7=4 no; so missing. Both missing. Now S_x: from 0:0+4=4;0-4 invalid;6+4=10;6-4=2;11+4=15? 11+4=15<=15? 15<=l, yes, so 15;11-4=7. So S_x = {4,10,2,15,7}. S_y: from 0:0+7=7;0-7 invalid;6+7=13;6-7 invalid;11+7=18>15;11-7=4. So S_y = {7,13,4}. Intersection = {4,7}. So we can add p=4 or 7. If add p=4, then distances: from 0:4-0=4 (x); from 11:11-4=7 (y). So v=1. Great.
Now check: Is p=4 not in marks? marks are {0,6,11}, 4 not there, okay. So v=1.
Thus the algorithm works.
Now, consider if intersection yields p that equals x or y? That's okay.
Now, let's test edge cases: n=2, l=10, x=5,y=6, marks=0,10. Both missing? x=5? 0+5=5 not;10-5=5 not; so missing. y=6? 0+6=6 not;10-6=4 not; missing. S_x: 0+5=5;10-5=5 => {5}. S_y:0+6=6;10-6=4 => {6,4}. Intersection empty, so v=2. That matches expectation.
But what about possibility that adding one mark at e.g., 5 would also allow measuring 6? 5 from 0 gives 5, but 6? Could be from 10-4=6 but 4 not present. So no.
Now, consider if both missing and intersection empty but maybe adding one mark at some p creates x via p - a_i and y via p - a_j? That's exactly what intersection gives. So if no intersection, cannot.
But is there a scenario where adding one mark creates x via p - a_i and y via a_j - p, but p = a_i + x = a_j - y, which is one of the intersection conditions, but our set S_x includes a_i + x and a_i - x. So if p = a_i + x, that's in S_x. And if also p = a_j - y, then p is in S_y as a_j - y. So it's covered.
Now, what about case where both missing but we could add a single mark that creates x as difference between two new marks? Only one mark added, so no.
Thus algorithm covers.
Now, we should consider another nuance: The problem states that we can add marks at any integer non-negative distance from the origin not exceeding the length. But what if adding a mark at position d (missing distance) is not possible because d > l? But x,y ≤ l, so okay.
Now, implement.
Implementation steps:
Read input via sys.stdin.
Parse n,l,x,y.
Read list a of length n.
Convert a to set marks_set for membership tests. Since we will also need to loop over list for building S_x, we keep list.
Determine measurable:
has_x = any((ai + x) in marks_set for ai in a)
has_y = any((ai + y) in marks_set for ai in a)
This loops n times and does set lookup each. Should be fast.
Alternatively, we could use binary search because list is sorted, but set is fine.
If has_x and has_y: print 0; return (nothing else needed).
If has_x and not has_y: # y missing
print(1)
print(y) # Note: must ensure y not already a mark? It isn't because if y were a mark, then 0 and y give y, so has_y would be true. So fine.
return
If not has_x and has_y:
print(1)
print(x)
return
Else: both false.
Build S_x = set()
for ai in a:
if ai + x <= l:
S_x.add(ai + x)
if ai - x >= 0:
S_x.add(ai - x)
Build S_y similarly.
Find common = S_x & S_y
If common:
p = common.pop() # any
print(1)
print(p)
else:
print(2)
print(x, y) # order any; ensure output as two numbers separated by space.
But need to be careful: The second line should contain v space-separated integers. If v=0, we don't print second line? The problem says: In the first line print v, in the second line print v space-separated integers. For v=0, presumably we don't print a second line? Usually in such problems, when v=0, you just print 0 and that's it; no second line. The sample 2 output is "0" (just one line). So we should not print an empty line. So just print 0 and finish.
Edge Cases: Might there be a scenario where both missing but adding one mark at some position might also create x via difference between two existing marks? No.
Now, confirm correctness thoroughly.
But let's consider if both missing and intersection nonempty but p is outside [0,l]? By construction we only add points within bounds. So fine.
Now, we should think about the possibility that adding a single mark at p might also inadvertently create both distances, but one of them might have been already measurable? Already covered.
Now, consider the case where both missing and intersection exists, but the p we pick is such that p = a_i + x and p = a_j + y, but both a_i + x and a_j + y are > l? Actually we enforce <= l. So fine.
Now, a subtle case: Could it be that both missing, but we can add one mark that creates x as the difference between two new marks? Only one mark, so no.
Now, what about the possibility that we can add one mark at p such that p - a_i = x and also (a_j - p) = y? That is covered by p = a_i + x = a_j - y, which is in S_x (as a_i + x) and S_y (as a_j - y). Yes.
Now, note: The intersection S_x ∩ S_y may contain points that are derived from the same mark for both? For instance p = a_i + x = a_i - y? That would imply x = -y, impossible. So it must involve two different marks (or same mark but one plus and the other minus yields impossible). So it's fine.
Now, can S_x ∩ S_y be non-empty but the required distances are not both achievable because the marks used might be the same? Already discussed, need two marks. But if both come from the same a_i? Let's examine: Suppose p = a_i + x and p = a_i + y => x = y, impossible. p = a_i + x and p = a_i - y => x = -y, impossible. p = a_i - x and p = a_i - y => x = y, impossible. So cannot. So indeed they involve different marks. So p is at distance x from some a_i and distance y from some a_j, with i≠j (automatically). So it works.
Now, consider case where both missing and intersection is nonempty but the p found is such that the required a_i for x is not present because a_i + x = p but a_i may be > p? Actually a_i + x = p => p > a_i, so fine. For a_j - y = p => a_j > p, so fine. So both distances are positive.
Now, let's think about complexity: We'll do O(n) loops to check has_x, has_y (we could combine with building S_x, but separate loops are fine). Then another O(n) loops to build S_x and S_y. That's O(n). Set operations O(size). So total O(n) with moderate constant.
Now, memory: sets of marks (size n), plus S_x, S_y (size up to 2n). That's fine.
Now, we must be careful: In building S_x and S_y, we might add duplicate points; set handles that.
Now, consider the possibility that both missing, but we might have a solution with v=1 even if intersection empty? Wait, is there any other way to achieve both with one mark? For example, after adding one mark p, maybe x becomes measurable as difference between p and another mark, and y becomes measurable as difference between that same other mark and p? That's just p - a_i = x and a_i - p = y? That would require x = -y, impossible. Or perhaps y becomes measurable as difference between p and some other mark that is not directly derived from p, but as difference between two existing marks? But existing set unchanged, and both missing initially. So no.
What about y becoming measurable as difference between p and some other new mark? Only one new mark.
So intersection is necessary and sufficient.
Thus algorithm correct.
Now, we need to code.
But also, we can avoid building S_x and S_y if we can find a p by scanning the list and checking conditions directly. But building sets is simple.
Now, test more tricky cases:
Case: n large, l large.
Now, verify with some random tests by brute force for small n to ensure our solution yields minimal v. But since we have reasoning, seems fine.
Now, potential pitfalls: When checking measurability using set, we only check forward differences (pos + d). That's correct because difference is absolute positive, but we only need positive. However, note that if there is pair with a_j - a_i = d, then a_i + d = a_j. So indeed exists i such that a_i + d is in set. So correct.
But careful: There is possibility that d = 0, but not needed.
Now, what about if there is a pair with a_i - a_j = d? That is same as a_j + d = a_i. So still covered.
So checking pos+d in set is enough.
Now, building S_x: we consider both plus and minus. That is necessary for intersection condition because p might be less than a_i (i.e., p = a_i - x). So we include both.
Now, the solution seems straightforward.
But I recall that this problem is from Codeforces (CF 479D?) Actually it's "Long Jumps" maybe CF Round #... I've seen similar problem: "CF 479D Long Jumps" or something? Actually I recall a problem: "A. Adding a point to measure two distances". Yes, it's CF 340A? Hmm. But there's a known problem: "D. Long Jumps" from CF Round #... with x and y distances. The typical solution is exactly this: check if both present, else if one present, add the other; else check if one mark can cover both, else add two marks.
Thus solution is standard.
But there is also a nuance: If both missing, it's possible that adding one mark at a position p might also make both measurable if p is such that x and y are differences between p and the same mark? No.
However, there is a scenario: Suppose x and y are such that y = k * x? Not relevant.
Now, consider another subtlety: Could adding one mark at position p create both x and y if p is chosen such that x = |p - a_i| and y = |p - a_j|? Already that's intersection.
Thus done.
Now, code.
But we should also consider the possibility of duplicate marks? Input says strictly increasing, so no duplicates.
Now, think about output format: Print v on first line. If v > 0, print v space-separated integers in any order. For v=1, we print one integer. For v=2, we print two.
Now, confirm that we always output integer positions within [0,l] and integers.
Edge Cases: Could there be scenario where both missing and intersection contains a point that is already in A? As argued, that would imply x or y measurable. But to be safe, if common set contains a point that is in marks_set, we should still be able to use it? But if it's in A, adding it does nothing; but then would x or y become measurable? They would already be measurable if that point were in A, but we assumed both missing, so impossible. So it's fine.
Now, one more nuance: In both missing case, there could be a scenario where adding one mark at position p creates x as the difference between p and some mark, and y as the difference between p and the same mark? No.
Now, implementation details in Python:
code
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it)); l = int(next(it)); x = int(next(it)); y = int(next(it))
a = [int(next(it)) for _ in range(n)]
marks_set = set(a) # for fast lookups
has_x = False
has_y = False
for ai in a:
if not has_x and ai + x <= l and (ai + x) in marks_set:
has_x = True
if not has_y and ai + y <= l and (ai + y) in marks_set:
has_y = True
# early break if both true? But not necessary.
if has_x and has_y:
print(0)
return
if has_x and not has_y:
print(1)
print(y)
return
if not has_x and has_y:
print(1)
print(x)
return
# both missing
s_x = set()
s_y = set()
for ai in a:
if ai + x <= l:
s_x.add(ai + x)
if ai - x >= 0:
s_x.add(ai - x)
if ai + y <= l:
s_y.add(ai + y)
if ai - y >= 0:
s_y.add(ai - y)
common = s_x.intersection(s_y)
if common:
p = next(iter(common))
print(1)
print(p)
else:
print(2)
# output x and y, but ensure they are not duplicate? x<y, distinct.
print(x, y)
if __name__ == "__main__":
main()
Check: In the loop for has_x, we used condition ai + x <= l. Actually we need to check if ai + x is in marks_set. Since marks_set only contains positions up to l, but we should ensure that ai + x <= l because otherwise it cannot be a mark (since marks are ≤ l). However, if ai + x > l, it cannot be in marks_set anyway. So we could skip the bound check, because set membership will be false. But for performance, it's fine to include bound or not. Might be safe to skip bound. But if we don't check bound, we might be checking membership of a number > l, which is not in set, so it's fine. However, l can be up to 1e9, and set contains numbers up to l, so membership for numbers > l will be false quickly. It's fine.
Thus we can simplify: has_x = any((ai + x) in marks_set for ai in a). Since a_i + x could exceed l, but that's okay. But for correctness, we must ensure that we consider differences only when a_j - a_i = d with a_j ≤ l. If a_i + x > l, then a_j cannot be > l, so it's fine to skip. So membership will be false. So no problem.
Thus we can write:
code
has_x = any((ai + x) in marks_set for ai in a)
has_y = any((ai + y) in marks_set for ai in a)
This is succinct.
Now, for building s_x and s_y, we do need to enforce bounds because we only consider positions within [0,l] that we can add marks. So we must check ai + x <= l and ai - x >= 0. That's correct.
Now, we could also compute common by iterating over one set and checking membership in the other.
Now, we also need to consider a possible optimization: We could compute common on the fly: For each candidate p from s_x, check if p in s_y? But we already built sets.
Now, test with sample.
Sample 1: n=3,l=250,x=185,y=230,a=[0,185,250]; marks_set={0,185,250}. has_x: any ai+185 in set? 0+185=185 in set => true. has_y: 0+230=230 not; 185+230=415>250 not; 250+230>250 not. So false. So has_x true, has_y false => output 1 and y=230. Works.
Sample 2: n=4,l=250,x=185,y=230,a=[0,20,185,250]. has_x: 0+185=185 in set => true; has_y: 0+230=230 not;20+230=250 in set => true. So both true => 0.
Sample 3: both false. s_x from 0:0+185=185,0-185 invalid;300+185>300;300-185=115. So {185,115}. s_y:0+230=230,300-230=70 => {230,70}. Intersection empty => output 2 185 230. Works.
Now, consider a case where both missing and intersection exists:
n=3,l=15,x=4,y=7,a=[0,6,11]. s_x as before {2,4,7,10,15}. s_y {4,7,13}. Intersection {4,7}. Our code would output 1 and maybe 4. That works.
Now, check: In this case, adding mark at 4: differences: 4-0=4 (x), 11-4=7 (y). So indeed.
Now, what about p=7: 7-0=7? That would give y, not x. Actually 7-0=7 gives y; need x: 7-? 6-? 7-? from 11? 11-7=4 gives x. So yes, both.
Now, we should also consider the case where both missing and intersection exists but p is not integer? Not possible.
Now, about the case where we have multiple marks and the intersection set may contain many points; we just pick any.
Now, another nuance: In the both missing case, we output 2 marks at x and y. However, is it always guaranteed that adding both x and y will not interfere? It will definitely make both distances measurable because 0 and x give x; 0 and y give y. But could there be an issue if x==y? Not possible. So fine.
But could there be a scenario where we could achieve v=1 but our intersection detection fails due to not considering that p must be integer? It's integer.
Now, check potential pitfalls with using set for marks for measurability: Since we check (ai + x) in marks_set, we may double count but it's fine.
Now, ensure that we read input correctly: The second line contains n integers.
Now, think about extreme values: n=100000, building sets of size up to 200000 each, which is fine.
Now, consider if there's a scenario where both missing but one of the distances is measurable through difference between two marks that we didn't consider because of our check? Our check any(ai+x in marks_set) covers all. So if has_x is false, then indeed no pair difference x.
Now, about both missing: we build s_x and s_y. Could there be a scenario where s_x ∩ s_y nonempty but the p found is such that adding p would create x but not y because the mark used for y might be the same as for x? That would require p = a_i + x and p = a_i + y => x=y, impossible. Or p = a_i + x and p = a_i - y => x = -y impossible. So the marks are different. So y is measured.
Thus all good.
Now, one more: Could there be a scenario where both missing but adding one mark p makes y measurable as difference between p and some mark, but that difference is actually x? No.
Now, overall solution is clear.
But I recall a known Codeforces problem 479D - Long Jumps, where the answer also includes a possibility of adding 1 mark at position that is exactly (x+y)/2 if certain condition? Actually there is another twist: In that problem, the ruler length l, and you need to measure x and y. The typical solution includes checking if a point exists such that it measures both, but also checks if there is a point that can measure both by being at distance x from one mark and y from another; plus there is an additional case: if there is a mark at distance (x+y) from another? Actually I'm mixing with another problem: "Make it Divisible by 3"? Hmm.
Wait, I recall a problem: "Valera and Fruits" no.
Maybe I'm thinking of problem "Ruler" from Codeforces 479C? Not sure.
But let's double-check: In CF 479D "Long Jumps", I think the problem is similar: n marks on a ruler, need to measure distances x and y. The solution includes checking if both exist; else if one exists, add the other; else check if there is a mark such that you can measure both by adding one point; else add two points.
But in that problem, there is also a case where you can add a point at position a_i + x (or -) that also gives y because a_i + x ± y is another mark? Actually the additional case is: if there exists a_i such that a_i + x + y is a mark, then you can add a_i + x (or a_i + y) to get both? Wait, let's recall: In some solution, they check if there is a mark at distance x + y from some a_i, then adding a_i + x works? For example, marks: 0, 10, 20, x=5,y=15: a_i=0, then 0+5+15=20 is a mark, then adding 5 gives 5-0=5 and 20-5=15. That's exactly our intersection condition: p = 5 (a_i + x) and also p = a_j - y where a_j =20? Actually a_j =20, p = 20 - y =5, so p is in intersection. That's covered.
Another scenario: If there is a mark at distance y - x from something? Actually if a_i + x = a_j + y? That would give a_j - a_i = x - y negative, unless we consider minus. Already covered.
But there is also scenario: If there is a mark at distance y - x (with y > x) from another? Could that allow a single point to be placed such that it measures both? For example, suppose we have two marks separated by d = y - x. If we add a point at a_i + x, then distances: from a_i, we get x. From a_i + x to a_i + d? Actually a_i + d = a_i + (y - x) = a_i + y - x. Then difference between that mark and the added point: (a_i + y - x) - (a_i + x) = y - 2x. Not y. So not.
What about adding a point at a_i + (y - x) maybe? Not.
Thus our intersection condition is correct.
But there is another possibility: Adding one mark might allow measuring x as difference between two existing marks that were not measurable before? No, because existing marks unchanged.
Thus the solution is correct.
Now, one more nuance: Could there be a case where both missing and we can add one mark p such that x is measurable via p - a_i = x and y is measurable via p - a_j = y, but the a_i and a_j are the same? That would require p = a_i + x and p = a_i + y => x=y, impossible. So not.
Thus v=1 iff S_x ∩ S_y ≠ ∅.
Now, we must ensure that S_x and S_y are built correctly: include both plus and minus.
Now, we could also avoid building both sets fully by scanning and checking membership in the other set on the fly. For each candidate p from S_x, check if p in S_y? But we need S_y anyway.
Alternatively, we could compute candidate p values directly by checking for each a_i, if a_i + x (or a_i - x) is also a valid p for y, i.e., if that p is at distance y from some other mark. That would require checking if p ± y ∈ A. So we could do: For each a_i, consider p = a_i + x (if ≤ l), then check if there exists a_j such that |p - a_j| = y. That is equivalent to: either p + y ∈ A or p - y ∈ A. That can be checked with set A. Similarly for p = a_i - x. This would avoid building S_y set. Complexity O(n) with set lookups. This might be simpler.
But building both sets is also O(n). Both fine.
Let's implement that to reduce memory: we can just loop through a_i and for each candidate p (two per a_i: a_i+x and a_i-x, if in bounds), then test if (p + y) in marks_set or (p - y) in marks_set. If yes, then that p works. We also need to ensure that the mark used for y is not the same as the one used for x? Actually if p = a_i + x, and we check if p + y ∈ A, then the distance from that mark to p is y (since (p+y) - p = y). That mark is different because p+y > p, and a_i is less than p (since x>0). So distinct. If p - y ∈ A, then p - (p - y) = y, distinct as well. Could there be a conflict where p - y equals a_i? That would imply a_i = p - y = (a_i + x) - y => y = x, impossible. So distinct. So we can accept.
Thus we can find a p with O(n) without constructing S_y. However, we must also consider that the candidate p might be such that p itself is already in A? But as argued, if p in A, then x would be measurable initially (since a_i and p in A), which contradicts both missing. So such p won't be found because then has_x would be true. But can it happen that we consider p = a_i + x, and p ∈ A, but has_x false? No, because if p ∈ A, then a_i + x ∈ A, so has_x would be true. So p not in A. So fine.
Thus algorithm for both missing:
code
for ai in a:
for p in [ai + x, ai - x] if within bounds:
if (p + y) in marks_set or (p - y) in marks_set:
output 1 and p
return
output 2 and x,y
But careful: When we check (p + y) in marks_set, we must ensure p+y ≤ l? Because marks_set only contains numbers up to l, but if p+y > l, it's not in set, so check is false. Similarly p-y ≥ 0. So it's fine.
But there is also a possibility that p itself is at distance y from some mark via p - a_j = y, but we check both directions. So yes.
This algorithm is O(n) with set lookups. It's simpler and avoids building S_y.
But we must also consider that the candidate p might be derived from ai - x, and then checking (p + y) or (p - y) with marks_set. That's okay.
Thus we can implement.
Let's test with earlier example: a=[0,6,11], x=4, y=7.
ai=6: p=6+4=10 (<=15). Check: 10+7=17 not in set; 10-7=3 not in set. p=6-4=2 (>=0): 2+7=9 not; 2-7 invalid. No.
ai=11: p=11+4=15 (<=15): 15+7=22 no; 15-7=8 not in set. p=11-4=7: 7+7=14 not; 7-7=0 in set? yes, 0 in set => success, output 1,7. Works.
Thus algorithm works.
Now, we must consider also the possibility that both missing and p could be found from candidate where p = a_i + x, and p - y = a_j, with a_j possibly equal to a_i? That would imply a_i = p - y = a_i + x - y => x = y, impossible. So fine.
Now, what about the scenario where both missing but we could also add a mark p that creates x via p - a_i = x and y via p - a_j = y, but we need to ensure both conditions are satisfied. Our loop checks each candidate p derived from x, then checks if it is at distance y from any mark. That's sufficient because if there is any p that works, then p is at distance x from some mark (by definition), so it will be generated as a candidate from that mark (or maybe from another mark?). Indeed, if there exists a p such that |p - a_i| = x and |p - a_j| = y, then p = a_i ± x. So when we iterate over a_i and consider p = a_i ± x, we will encounter that p. Then we check the condition for y. So we will find it.
Thus algorithm works.
Now, also need to consider the possibility that p could be at distance x from a_i via subtraction (a_i - x) and distance y from a_j via addition (a_j + y) or subtraction. Our loop includes both ± from each a_i, so covered.
Thus we can avoid building S_x and S_y.
But note: We need to also consider the scenario where both missing but we might also find p from a_i + x that is out of bounds? We only consider in bounds because we cannot add marks outside [0,l]. So we restrict.
Thus final algorithm:
Read input.
Create set marks.
Determine has_x, has_y.
If both: output 0.
Else if only x missing: output 1 and x.
Else if only y missing: output 1 and y.
Else: both missing:
For each a in a:
for p in [a + x, a - x] (if within [0,l]):
if (p + y) in marks or (p - y) in marks:
output 1 and p; return.
If loop finishes without success: output 2 and x y.
But careful: In the case where only one missing, we used x or y. But what if both missing and we can add one mark that creates both, but that mark is exactly x or y? That would be found by our loop: e.g., if we add p = x, then we need to have some a_i such that |x - a_i| = x => a_i = 0 or a_i = 2x. And need some a_j such that |x - a_j| = y => a_j = x - y or x + y. So if such a_j exists in marks, then p = x works. Our loop will generate p = x from a_i = 0 (if 0+x = x) as candidate. Then check if x+y or x-y is in marks. So that works.
Now, ensure we don't output p that is already a mark? It's okay because if p is in marks, then has_x would be true because (p - a_i = x) with a_i in marks? Actually if p is a mark, then we have a_i (maybe 0) and p, giving x? Wait, if p = x and x is a mark, then 0 and x give x, so x would be measurable, contradicting both missing. So p won't be a mark. So fine.
Now, edge case: Could it happen that both missing, but the candidate p we find is such that (p + y) or (p - y) is in marks, but that mark is the same as the a_i used for x? E.g., a_i = 0, p = x, and p - y = x - y. If x - y is in marks? That would be negative because y > x, so not. Or p + y = x + y; if x + y is in marks, then a_i=0, p=x, a_j=x+y; then a_j - p = y, good; distinct. So no conflict.
Thus algorithm is fine.
Now, but we must also consider the possibility that the ruler already contains marks that can measure x or y after adding one mark, but our loop for both missing may also find a p that is not integer? All positions are integers.
Now, we can also consider the possibility that both missing and we could add one mark at position p such that p is at distance x from some mark and also distance y from the same mark? Not possible.
Now, test a tricky case: a = [0, 3, 8], l=10, x=3, y=5. But note x=3: 0+3=3 in set, so x measurable, so not both missing.
a = [0, 5, 10], x=2, y=8. Both missing? Check: x=2? 0+2=2 no;5+2=7 no;10-2=8? difference between 10 and 2? But we need a pair; 10-5=5, 10-0=10,5-0=5. So no 2. y=8? 10-2? Actually 10-2=8 but 2 not a mark; 5+? 5+? 5+3=8, 3 not mark. So both missing. Now, can one mark help? We try p = 2? (from 0+2). p=2: check if 2+8=10 in set? yes, 10 in set => works! So v=1. Our loop: a_i=0, p=0+2=2, check p+y=2+8=10 in marks => true, output 2. Good.
Now, a = [0, 5, 10], x=3, y=7. Both missing? 0+3=3 no;5+3=8;10-3=7? That gives 7? 10-3=7 but 3 not a mark; Actually need pair: 10-? 10-? if there is mark 3, not. So missing. y=7: 0+7=7 no;5+7=12>10;10-7=3 no. So missing. Can one mark? Try p=3 (0+3): check 3+7=10 in set -> yes! So p=3 works. Actually 3+7=10, gives y. So v=1. Good.
Now, a = [0, 4, 9], x=2, y=7? both missing? x=2? 0+2=2 no;4-2=2? but need pair: 4-? 4-2=2, but 2 not a mark. So missing. y=7? 4+3? Actually 9-2=7? 9-2=7, 2 not mark; 0+7=7 no;4+7=11>9;9-7=2 not mark. So missing. Check for p: from 0+2=2 => check 2+7=9 in set -> yes! So p=2 works. So v=1.
Now, think of case where both missing and v=2. Already seen.
Now, check if there is case where both missing and v=1 via candidate p = a_i - x? For example, a = [0, 10, 15], l=20, x=5, y=10. Both missing? x=5? 0+5=5 no;10-5=5 no;10+5=15? 15-10=5? Actually 15-10=5, so x measurable? Because 15 and 10 difference 5. So x measurable. Not both.
Let's craft: a = [0, 7, 13], l=15, x=3, y=10. Both missing? x=3? 0+3=3 no;7-3=4 no;7+3=10? 10 not mark;13-3=10 not mark; so missing. y=10? 0+10=10 no;7+3? 7+3=10, but 3 not mark; 13-3=10, 3 not mark; 13-? Actually 13-? 13-3=10, 3 not mark; 7+3? not. So missing. Now, can one mark? Try p from 0+3=3: check 3+10=13 in set? yes! So p=3 works. So v=1.
Now, think of case where both missing and v=1 but p is from a_i - x. Example: a = [0, 3, 8], l=10, x=5, y=2? But x<y so x smaller, but we can swap? Actually x<y, so x is smaller. Let's set x=2, y=5, we already have. p from a_i - x? For x=2, a_i -2 could be candidate. Example: a = [0, 5, 10], x=2, y=8. We used p=2 (a_i + x). For p from a_i - x: a_i=5, p=3 (5-2=3). Check p+y=3+8=11 not; p-y=3-8 negative; not. So not.
Maybe a = [0, 7, 12], l=15, x=3, y=8. Check missing: x=3? 0+3=3 no;7-3=4 no;7+3=10 no;12-3=9 no. y=8? 0+8=8 no;7+1? 7+1=8 no;7-? 7-? 7-? actually 7-? 7-? 7-? 7-? Not. 12-4=8, 4 not mark; So missing. p from a_i - x: a_i=7, p=4 (7-3). Check p+y=4+8=12 in set? yes! So p=4 works. So loop includes p=4 (a_i - x). Good.
Thus our loop works.
Now, we should also check if the candidate p must be integer; all good.
Now, note: In the both missing case, we could also find p from a_i + x that is also a_i - y? Not needed.
Now, implementation details: We'll use set for marks for O(1) lookup.
But we need to consider the case where the candidate p itself might be already in marks_set? As argued, if p in marks_set, then has_x would be true because we would have a_i and p in set with difference x. However, it's possible that a_i is not in set? No, a_i is from the list, so it's in marks_set. So if p ∈ marks_set, then x is measurable. Since we are in both missing, that can't happen. So we can ignore.
But there is a subtle edge: If we use a_i - x = p and p is in marks_set, then we have p and a_i in marks_set with difference x, so x measurable. So again impossible. So fine.
Now, about the case where only one missing: we output the missing distance. But could there be a scenario where only one missing but adding that distance is not possible because that distance is already occupied? But if missing, then as argued, distance d is not measurable, but could d be a mark? If d is a mark, then (0,d) gives d, so it would be measurable. So if missing, d cannot be a mark. So it's free.
Thus all set.
Now, final code.
But there is a nuance: The problem statement: "Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler." So the marks we add must be integer. x and y are integers, so okay.
Now, time to code.
But we should also test a scenario where l is exactly x or y: e.g., l = 10, x = 5, y = 10. n marks maybe include 0 and 10. Then x missing? Check: 0+5=5 not in marks; 10-5=5 not; so missing. y: 0+10=10 in marks => y measurable. So only x missing => output 1 and 5. But 5 is within [0,10] and not a mark. So works.
Now, what if l = x, but x < y ≤ l? That forces y = l as well? Actually if x < y ≤ l, and l = x, then y cannot be > l, so y = l = x, but x<y so impossible. So not.
Now, test scenario where both missing but intersection found and p is exactly l? Could be: p = a_i + x = l, then p + y > l, not check. But if p = l, then maybe p - y in marks? Let's see: a_i = l - x, p = l. Then we need a_j such that |l - a_j| = y, i.e., a_j = l - y or a_j = l + y (impossible) or a_j = l - y. So if l - y is a mark, then p works. So p = l could be candidate if a_i = l - x exists. But we only add marks within [0,l], so p = l is allowed (end of ruler). However, the ruler already has a mark at l (an = l). So p = l is already a mark. But then x would be measurable (since a_i = l - x and l are both marks) => x would be measurable, contradiction. So if both missing, p cannot be l (since that would make x measurable). So not.
Now, consider p = 0. Similar reasoning.
Thus our algorithm is robust.
Now, we should also handle input where n=2 and a=[0,l] only. That's fine.
Now, produce final code.
But before finalizing, let's think about potential corner case that could break our logic: The problem says "the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d)". So it's not absolute difference? It says aj - ai = d, and i ≤ j. So only non-negative differences. So we only consider j ≥ i. That's fine; we've been using positive differences.
Now, the set of measurable distances includes 0? But d positive. So fine.
Now, check: In our condition for measurability, we used (ai + d) in set. That corresponds to j such that aj = ai + d, with aj > ai. So correct.
Now, for building candidate p for adding marks, we considered absolute distances because the new mark can be either left or right of an existing mark; but the measurable distance is aj - ai (non-negative). So if we add p, and we have existing a_i, then if p > a_i, the distance p - a_i is measurable; if p < a_i, then a_i - p is measurable. So we consider both |p - a_i|, that's correct.
Now, in our loop for both missing, we consider p = a_i + x (p > a_i) and p = a_i - x (p < a_i). Then we check if there is some existing a_j such that either a_j - p = y (if a_j > p) or p - a_j = y (if p > a_j). That is, we check if p + y ∈ marks_set (which gives a_j = p + y > p, then a_j - p = y) or p - y ∈ marks_set (which gives a_j = p - y < p, then p - a_j = y). So that's correct.
Thus fine.
Now, complexity: O(n) time, O(n) memory for set and list.
Now, let's test more with random brute force for small n to ensure minimality. But given reasoning, it's minimal because:
If both present, 0.
If only one present, we need at least 1 because missing distance not measurable. Could it be 0? No. So 1 is minimal. And we achieve 1.
If both missing, could we achieve 0? No. Could we achieve 1? Possibly if exists p as described. Otherwise we need at least 2 because one mark can only create one new distance? Actually one mark can create multiple new distances, but we need both x and y. Could one mark create both? Yes, if it satisfies both conditions, then it's possible. If no such p exists, then one mark cannot create both. Could there be a scenario where one mark creates both but not through direct pair with existing marks? Could it create y as difference between two new marks? No, only one new mark. So indeed if no p in S_x ∩ S_y, then one mark can't make both measurable. So at least 2. And we achieve 2 by adding x and y. So minimal.
Thus algorithm yields minimal v.
Now, we need to output the positions in any order. For v=2, we output x and y. That's fine.
Now, final code.
But wait: There is one more nuance: In the case where both missing, we might be able to add one mark that makes both measurable, but the mark we add might be such that x becomes measurable via difference between two existing marks that was not present before? That's impossible as argued.
Thus solution is correct.
Now, writing final answer.
Potential pitfalls: Use of set may cause memory overhead, but it's okay.
Now, note: In Python, reading input with sys.stdin.read() and splitting is efficient.
Now, final code:
code
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it)); l = int(next(it)); x = int(next(it)); y = int(next(it))
a = [int(next(it)) for _ in range(n)]
marks = set(a)
has_x = any((ai + x) in marks for ai in a)
has_y = any((ai + y) in marks for ai in a)
if has_x and has_y:
print(0)
return
if has_x and not has_y:
print(1)
print(y)
return
if not has_x and has_y:
print(1)
print(x)
return
# both missing
for ai in a:
# p = ai + x
p = ai + x
if p <= l:
# check if p works for y
if (p + y) in marks or (p - y) in marks:
print(1)
print(p)
return
# p = ai - x
p = ai - x
if p >= 0:
if (p + y) in marks or (p - y) in marks:
print(1)
print(p)
return
# if none found
print(2)
print(x, y)
if __name__ == "__main__":
solve()
Test with sample inputs.
Sample 1:
Input:
3 250 185 230
0 185 250
has_x true, has_y false -> output 1 and 230. matches.
Sample 3:
2 300 185 230
0 300
both false, loop: ai=0: p=185 (0+185) <=300, check (185+230)=415 not, (185-230)=-55 not -> no; p=0-185 invalid. ai=300: p=300+185=485>300; p=300-185=115>=0, check (115+230)=345 not, (115-230) negative -> no. So no p found, output 2 185 230. matches.
Now, additional test:
Input:
3 15 4 7
0 6 11
both missing? Check: x=4? 0+4=4 not;6+4=10 not;11-4=7 not; so missing. y=7? 0+7=7 not;6+7=13>15;11-7=4 not; so missing. Loop: ai=0: p=4 -> 4+7=11 in marks -> output 1 4. Good.
Now, test where p from subtraction works: as earlier: [0,7,12] x=3,y=8,l=15. Loop: ai=0: p=3 -> 3+8=11 not, 3-8 invalid; p=-3 invalid. ai=7: p=10 (7+3) -> 10+8=18 not,10-8=2 not; p=4 (7-3) -> 4+8=12 in marks -> output 1 4. Good.
Now, test where both missing and intersection empty: [0,300], x=185,y=230 already.
Now, what about case where both missing but p found via p+y in marks but p+y > l? Already excluded by bounds? If p+y > l, then not in marks because marks only up to l. So check will be false.
Now, consider if both missing and p found but p is not integer? Not.
Now, final code seems ready.
But wait: In the loop, we are checking (p + y) in marks or (p - y) in marks. However, we must also ensure that the mark used for y is not the same as the one used for x? As argued, not possible due to x<y, but what if x and y are such that p - y = a_i? Could that happen? Suppose p = a_i + x, and we check p - y = a_i? Then a_i + x - y = a_i => x = y, impossible. If p = a_i - x, and check p + y = a_i? Then a_i - x + y = a_i => y = x, impossible. If p = a_i - x and p - y = a_i? That would give a_i - x - y = a_i => -x - y = 0 impossible. So no conflict. So safe.
Thus final code.
But we should also consider the possibility that both missing and we find a p that is already a mark? As argued, impossible because that would imply x measurable. So it's fine.
Now, one more nuance: In the only one missing case, we output the missing distance. But what if the missing distance is not measurable after adding that mark because maybe the ruler length is exactly that distance? Actually adding at d will always allow measuring d because there is mark at 0 (origin) and the new mark at d gives difference d. However, we must ensure that d ≤ l (true). Also, is it possible that 0 is not a mark? a1=0 always, so yes. So adding d always makes d measurable.
Thus that's safe.
Now, final answer.
But wait: There is a known problem: "CF 479D Long Jumps" indeed had a twist where you could also add a mark at position (x+y)/2 if certain condition, but that's for the case where you need to measure x and y but you also consider the possibility of measuring both via one mark that is not directly distance from existing marks? Actually, I recall that problem: You have a ruler and you need to add minimal marks to measure x and y. The typical solution includes checking three cases: both already measurable; one measurable; both missing but there exists a point such that distance x to one mark and distance y to another; and also checking if there exists a point such that you can measure both via differences from the same side? Actually that's the same.
But I recall there was a nuance: In some solutions, they also check if there is a point such that you can measure |x-y|? Wait, there is another scenario: If you have a mark at distance d such that d + x = some mark and d + y = some other mark? That's covered.
But there is also a scenario where you can measure x and y by adding one mark that is at distance |x - y| from something? Let's analyze: Suppose there exist two marks such that the distance between them is either x + y or |x - y|. Then adding a single mark at the appropriate point can measure both x and y. For example, if there is a gap of x + y between two marks a_i and a_j (a_j - a_i = x + y). Then if we add a mark at a_i + x (which equals a_j - y), we can measure x (between a_i and new) and y (between new and a_j). This is exactly the condition we have: p = a_i + x = a_j - y. That's covered by our candidate p = a_i + x and checking if p - y ∈ marks? Actually p - y = a_i + x - y, not a_j unless x - y =? Wait, if a_j - a_i = x + y, then a_i + x = a_j - y. So p = a_i + x, and we check if p - y in marks? p - y = a_i + x - y = a_i + (x - y) = a_i - (y - x). That is not a_j. But we check if p + y in marks? p + y = a_i + x + y = a_j. So the condition we need is p + y ∈ marks (or p - y ∈ marks). In this case, p + y = a_j, which is in marks. So our check of (p + y) in marks catches that. Good.
Now, what about gap = |x - y|? Suppose a_j - a_i = |x - y| (assuming x<y, so y - x). If a_j - a_i = y - x, then we could add a mark at a_i + x? Then a_i + x = a_i + x; what is the relation to a_j? a_i + x = a_i + x, while a_j = a_i + (y - x) = a_i + y - x. Then a_i + x + ? Let's see: To get y, we could use the new mark and a_j? Distance from new mark to a_j: a_j - (a_i + x) = (a_i + y - x) - (a_i + x) = y - 2x. That's not y unless x=0. So not. Alternatively, add a mark at a_i + y? Then a_i + y = a_i + y; compare to a_j: a_j = a_i + y - x; then distance between new mark and a_j: (a_i + y) - (a_i + y - x) = x. So that would give x. But then what about y? The new mark at a_i + y gives distance from a_i to new mark = y. So we would have both x and y? Actually if we add p = a_i + y, then we have p - a_i = y, and p - a_j = (a_i + y) - (a_i + y - x) = x. So both x and y measurable. So this scenario corresponds to p = a_i + y, with p - x = a_j? Wait, p = a_i + y, and a_j = a_i + (y - x) = p - x. So condition: p - x = a_j ∈ marks. That is p = a_i + y and p - x in marks. But note, in our loop we are iterating over candidates from x (p = a_i ± x). That might not catch this because we are generating p based on x. However, note that the roles of x and y are symmetric? In the case we just described, p is derived from y (a_i + y). Could there be a solution where p is not of the form a_i ± x, but of the form a_i ± y? But if p is a solution, it must be at distance x from some mark and distance y from some other. So p = a_i ± x OR p = a_j ± y. Actually if p satisfies both, then p can be expressed both ways. So if p is a solution, then there exists some mark a_i such that |p - a_i| = x. Thus p = a_i ± x. So it is indeed of the form a_i ± x. So our loop over all a_i and generating p = a_i ± x covers all possible solutions. Because if a solution exists, there is some a_i with |p - a_i| = x, so p equals a_i + x or a_i - x. So we will generate it. In the scenario described (gap = y - x), the solution p = a_i + y. But does it satisfy |p - a_i| = y, not x. But we need p to be at distance x from some mark. Let's analyze that scenario more carefully: We have marks a_i and a_j such that a_j - a_i = y - x. To achieve both x and y with one new mark p, we might consider p = a_i + y. Then distances: p - a_i = y; p - a_j = (a_i + y) - (a_i + y - x) = x. So p is at distance y from a_i and distance x from a_j. So p is at distance x from a_j. Thus there exists a mark (a_j) such that |p - a_j| = x. So p = a_j ± x? Since p > a_j? p - a_j = x, so p = a_j + x. So p is also of the form a_j + x. Indeed, from a_j, p = a_j + x. So our loop will generate p when considering a_j (the mark that is at distance x from p). So our loop, which iterates over all marks and generates p = a_i + x, will include p when i = j (the mark a_j). So it's covered.
Thus the scenario where the gap is y - x yields p = a_j + x, which is covered. Let's check: a_j = a_i + (y - x). Then p = a_j + x = a_i + y. So yes, our loop catches it when we consider a_j and generate p = a_j + x, then check if p - y in marks? Actually we need to ensure that our check for y works: For p = a_j + x, we need to verify that there exists some mark at distance y from p. The mark a_i is at distance y from p? p - a_i = (a_j + x) - a_i = (a_i + y - x + x) - a_i = y. So indeed p - a_i = y, so (p - y) = a_i ∈ marks. So our check of (p - y) in marks will capture that. So that's fine.
Thus our loop covers all possibilities.
Therefore, final solution is correct.
Now, we also need to consider the possibility that the added mark p may be equal to 0 or l? That's allowed. But if p=0, then to have |p - a_i| = x, we need a_i = x. If x is a mark, then x measurable initially, contradiction. So p cannot be 0 if both missing. So fine.
Now, final code.
But we must also consider the output format: When v=2, we should output two numbers separated by space. In our code, we print(x, y) which prints as "x y". That's fine.
Now, potential edge case: v=1 and the position we output might be 0 or l? That's possible if, for instance, both missing but p = 0? Could that happen? For p=0 to be solution, we need |0 - a_i| = x => a_i = x; and |0 - a_j| = y => a_j = y. So if both x and y are marks, then x and y measurable initially, contradiction. So p=0 not possible. Similarly p=l not possible. So fine.
Now, we should test a case where both missing but p found from a_i - x, and p = 0? That would require a_i = x. Then x would be measurable initially (0 and a_i). So not.
Thus output positions are always within (0,l) and not in marks.
Now, finalize.
But before final answer, let's think about potential performance: In the both missing loop, we do up to 2n iterations, each doing up to 2 set lookups. That's O(n). Good.
Now, we might also want to early exit once we find a solution.
Now, implement.
One more nuance: In the only one missing case, we output the missing distance. But could there be a case where both missing and we find p from our loop but also we could add that p, but also the other distance becomes measurable? That's the intention. So fine.
Now, we could also combine the loops for measurability and for both missing? But not needed.
Now, I think that's it.
Now, let's also consider if there is any scenario where the number of added marks could be 0 even if both are measurable? Already handled.
Now, maybe there is a scenario where both missing but we can add two marks but they could be placed differently than x and y to also work. But minimal is 2 anyway.
Now, final answer.
I'll produce final code as described.
But to be thorough, I will test with random small cases with brute force to verify that our solution always yields the minimal number. I could do mental testing: For n=2, a=[0,l]. Then both missing unless x or y equals l? Actually if x = l, then measurable? 0 and l give l, so x measurable. But x<y≤l, so y could be l only if x<l, but y could be l, then y measurable. So one could be measurable. If both missing, then our algorithm says v=2 unless p found. p found only if there exists a mark? With only two marks 0 and l, p = 0 + x = x, check if x + y = l? Or x - y? For p=x, we need x+y = l or x-y = some mark (which would be negative or 0). So condition is x + y = l. Similarly p = l - x? Actually from a_i = l, p = l - x = l - x, then check if p + y = l? That would be l - x + y = l => y = x, impossible; or p - y = l - x - y =? Could be 0 if x + y = l? That gives p - y = l - x - y = 0 if l = x+y. So if x + y = l, then p = l - x works? Let's test: marks {0, l}. If x+y = l, then p = l - x = y. But y is a candidate? Actually p = l - x = y. But then adding p = y? But y is not a mark? If y is a mark, then (0,y) gives y measurable, so y would be measurable initially, contradiction. So if both missing, y not in marks, so p = y is not a mark, but if x+y = l, then p = y is within [0,l]. Check p = y from a_i = l? Actually a_i = l, p = l - x = y, so candidate. Then check (p + y) = 2y, not in marks unless 2y=l? Not; (p - y) = 0, which is in marks. So p = y works! But wait, if p = y works, then adding mark at y yields both distances? Let's test: marks initially {0,l}. Add y. Then we have distances: between 0 and y = y (so y measurable); between y and l = l - y. If x+y = l, then l - y = x, so x measurable. So indeed v=1. So when l = x + y, we can add one mark at y (or at x) to measure both. Does our loop find that? With a = [0, l], both missing. Loop over a_i = 0: p = 0 + x = x. Check: (p + y) = x + y = l in marks? Yes, if l = x+y, then l is in marks. So (p + y) in marks true -> output p = x. Also for a_i = l: p = l - x = y. Check: (p - y) = 0 in marks true -> output p = y. So we will output 1. So that matches the known special case. Good.
Thus our loop correctly identifies this.
Now, what about scenario where x = y? Not allowed.
Now, test scenario where both missing and p found via (p - y) in marks? Already covered.
Now, final code.
Now, one more test: n=3, l=10, a=[0,4,10], x=3,y=7? Both missing? x=3? 0+3=3 no;4+3=7 no;10-3=7 not; so missing. y=7? 0+7=7 no;4+7=11>10;10-7=3 no; missing. Loop: ai=0: p=3 -> 3+7=10 in marks -> output 3. Indeed adding 3 gives 3-0=3, 10-3=7. Works. So v=1. Good.
Now, consider if both missing and the marks are such that multiple p exist.
Now, all good.
Thus final solution is ready.
But wait: In the only one missing case, we output the missing distance. However, could there be a scenario where both missing but we output v=1 with p, but also the missing one? Already both missing.
Now, also consider possibility that only one missing but there exists a p that could also make the other (already measurable) still measurable and maybe we could use a different p that also satisfies some condition? Not needed.
Now, finalize answer.
One more nuance: The problem states that the marks are numbered in order, and a1=0, an=l. The distances are integers. So we are good.
Now, we should consider the possibility that the added marks could be placed at positions that are already marks? The problem says "add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler." It doesn't forbid adding at existing marks. But if we output a position that is already a mark, it's effectively not adding a new mark. But our algorithm ensures we output positions not in marks (except possibly if both missing? But we argued p not in marks). However, in only one missing case, we output the missing distance d. Could d be already a mark? If d is a mark, then (0,d) gives d, so d would be measurable, contradiction. So not. So we are safe.
Thus final.
Now, let's write final solution with proper reading and printing.
We'll include a function solve().
Edge case: if n=0? Not possible, n≥2.
Now, finalize.
I'll produce final answer. ```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))
x = int(next(it))
y = int(next(it))
a = [int(next(it)) for _ in range(n)]
marks = set(a)
code
# check which distances are already measurable
has_x = any((ai + x) in marks for ai in a)
has_y = any((ai + y) in marks for ai in a)
if has_x and has_y:
print(0)
return
if has_x and not has_y:
print(1)
print(y)
return
if not has_x and has_y:
print(1)
print(x)
return
# both distances are missing
for ai in a:
# candidate p = ai + x
p = ai + x
if p <= l:
if (p + y) in marks or (p - y) in marks:
print(1)
print(p)
return
# candidate p = ai - x
p = ai - x
if p >= 0:
if (p + y) in marks or (p - y) in marks:
print(1)
print(p)
return
# no single mark can fix both
print(2)
print(x, y)