Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the i-th of them will start during the x_i-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes x_1, x_2, ..., x_n, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute).
Ivan can choose two properties for the alarm clock — the first minute it will ring (let's denote it as y) and the interval between two consecutive signals (let's denote it by p). After the clock is set, it will ring during minutes y, y + p, y + 2p, y + 3p and so on.
Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of p. He has to pick it among the given values p_1, p_2, ..., p_m (his phone does not support any other options for this setting).
So Ivan has to choose the first minute y when the alarm clock should start ringing and the interval between two consecutive signals p_j in such a way that it will ring during all given minutes x_1, x_2, ..., x_n (and it does not matter if his alarm clock will ring in any other minutes).
Your task is to tell the first minute y and the index j such that if Ivan sets his alarm clock with properties y and p_j it will ring during all given minutes x_1, x_2, ..., x_n or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any.
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 3 ⋅ 10^5) — the number of events and the number of possible settings for the interval between signals.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^{18}), where x_i is the minute when i-th event starts. It is guaranteed that all x_i are given in increasing order (i. e. the condition x_1 < x_2 < ... < x_n holds).
The third line of the input contains m integers p_1, p_2, ..., p_m (1 ≤ p_j ≤ 10^{18}), where p_j is the j-th option for the interval between two consecutive signals.
Output
If it's impossible to choose such values y and j so all constraints are satisfied, print "NO" in the first line.
Otherwise print "YES" in the first line. Then print two integers y (1 ≤ y ≤ 10^{18}) and j (1 ≤ j ≤ m) in the second line, where y is the first minute Ivan's alarm clock should start ringing and j is the index of the option for the interval between two consecutive signals (options are numbered from 1 to m in the order they are given input). These values should be chosen in such a way that the alarm clock will ring during all given minutes x_1, x_2, ..., x_n. If there are multiple answers, you can print any.
Examples
Input
3 5
3 12 18
2 6 5 3 3
Output
YES
3 4
Input
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given: n events at minutes x_i (strictly increasing), we need to set alarm with first ring y and interval p (p chosen from given list p_1..p_m) such that for each x_i, there exists some nonnegative integer k_i with y + k_i p = x_i. In other words, all x_i are congruent modulo p, and the common remainder r = y mod p must be such that y ≤ x_1 (since first ring is at y, and it can ring at minute y then y+p etc. Actually we need y to be the first ring. It says: "the first minute it will ring (denote y) and the interval between two consecutive signals (p). After the clock is set, it will ring during minutes y, y + p, y + 2p, ...". So y is the first ring; we don't require that y is the smallest x_i; we could set y earlier than the first event. But the condition is that each x_i must be exactly some ring minute. That means for each x_i, there is some integer k_i >= 0 such that x_i = y + k_i p. So y must be <= x_1. Also, y must be positive (1 <= y <= 1e18). And p is any of the given options. So basically, we need to find p in the list, and y such that all x_i ≡ y (mod p) and y <= x_1 (and y >= 1). Since we can choose y as any such residue, we can set y = the smallest x_i? Not necessarily: if all x_i have same residue mod p, then we can set y to the smallest x_i, or even a smaller number with that same residue that is <= x_1 (and >=1). But we have to ensure that y is the first ring, so we need y to be the smallest number in the arithmetic progression. If we set y = the residue value (maybe less than x_1), then there might be some ring before the first event? That is okay; alarm rings at y, y+p, etc. The only condition is that it rings at all events. So we can set y as the smallest positive number congruent to that residue mod p that is <= x_1? Actually the first ring is exactly y, so if we set y to something less than x_1, the alarm will ring at that minute (maybe before any event). That's allowed. But does the condition require that the alarm rings at exactly the event minutes? It says "it will ring during minutes x_1, x_2, ..., x_n". It does not say it must not ring at other minutes. So it's fine.
Thus we need: pick p from given list such that all x_i are congruent modulo p, and then choose y as any number ≡ that residue mod p with 1 ≤ y ≤ x_1 (since y must be ≤ x_1 to have some nonnegative k to reach x_1). Actually y could be > x_1? If y > x_1 then the first ring would be after the first event, so event x_1 would be missed because there is no ring at x_1. So we need y ≤ x_1. Also we must have that x_i - y is divisible by p and nonnegative. For x_1, that means x_1 - y = k_1 p ≥ 0 => y ≤ x_1. For other x_i, since x_i > x_1, if y ≤ x_1, then x_i - y > 0, but we need it to be a multiple of p. So essentially, y must be congruent to all x_i mod p, and y ≤ x_1. So the set of viable y is: y ≡ r (mod p) where r = x_1 mod p (since all have same residue) and 1 ≤ y ≤ x_1. We can choose the maximum such y? Actually we need existence; we can choose y = r if r >=1 and r ≤ x_1; but r could be 0 mod p? In modular arithmetic we consider residues in [0,p-1]. But y must be at least 1. So if r == 0, then the smallest positive number with that residue is p. But p could be > x_1? That might make it impossible. So we need to find y in [1, x_1] congruent to r mod p. That's possible iff r != 0? Actually the condition for existence of an integer y = r + t p (with t integer) such that 1 ≤ y ≤ x_1. For t can be negative? But y is the first ring; if we take t negative, then y could be less than r, but then it's still congruent (since subtracting p). However if we allow negative t, the first ring might be before r, but then the series includes numbers less than y? No, the series is y, y+p, y+2p,... so if we choose y = r - kp, with k>0, that is also congruent to r mod p and ≤ r. That could be ≤ x_1 if r - kp ≥ 1. So y can be any number ≡ r (mod p), with 1 ≤ y ≤ x_1. So the existence is equivalent to: there exists integer t such that 1 ≤ r + t p ≤ x_1. Since t can be any integer (including negative) as long as y >=1. The smallest non-negative representative is r mod p, but we can go lower by subtracting multiples of p. So the condition is that the set {r + t p | t ∈ Z} intersects [1, x_1]. This is always true unless maybe x_1 < 1? No, x_1 ≥ 1. The set is arithmetic progression infinite in both directions (except modulo). So it will always intersect any interval of length at least p? Not exactly; but given that p can be larger than x_1, we can still have intersection if r itself is within [1, x_1] (i.e., r >=1 and r ≤ x_1). If r = 0, then the residues are 0 mod p, so the numbers are multiples of p. The smallest positive multiple is p. So if p > x_1, then there is no multiple of p in [1, x_1]. If p ≤ x_1, then we can take y = p (since p is a multiple of p, and p ≥ 1 and ≤ x_1). So existence condition: if all x_i ≡ r (mod p) (with 0 ≤ r < p), then there exists y in [1, x_1] with y ≡ r mod p if and only if either r != 0 and r ≤ x_1, or r = 0 and there exists some integer k≥1 such that k p ≤ x_1 (i.e., p ≤ x_1). But note that r could be > x_1? Actually r is less than p, so r can be > x_1 only if p > x_1 and r > x_1. For example, x_1 = 5, p=7, r=6. Then r=6 > x_1=5. Could we subtract p? r-p = -1 which is <1; r+ t p for t negative: r - p = -1 (invalid), for t positive: r + p = 13 > x_1. So no y in [1,5] ≡6 mod7. So if r > x_1 and also r-p <1, then no solution. In general, the set of numbers congruent to r mod p is {..., r-p, r, r+p,...}. The smallest non-negative is r if r>0, else p. To have some number in [1,x_1], we need that the smallest positive number in this residue class (call it y0) is ≤ x_1. That y0 = r if r>0, else p. So condition: y0 ≤ x_1. Because if y0 > x_1, then all numbers in that residue class are ≥ y0 > x_1 (since subtracting p gives negative). So existence reduces to: let r = x_1 mod p (since all x_i share same mod). Then if r == 0, set y0 = p, else y0 = r. Then we need y0 ≤ x_1. That is necessary and sufficient. And then we can set y = y0 (or any other value in that class in [1,x_1], but y0 is the smallest positive one). But is it required that y be exactly the first ring? If we set y = y0, then indeed the first ring is at y0. Since y0 ≤ x_1, it's valid. So condition: All x_i have same residue mod p, and the smallest positive residue (r if r>0 else p) ≤ x_1.
But there is another nuance: Since y is the first ring, we need that no ring occurs before y? That's by definition. So if we choose y = y0, that's the first ring. So it's fine.
Thus we need to find a p from list such that:
For all i, x_i ≡ x_1 (mod p). Equivalently, p divides (x_i - x_1) for all i. That is p | gcd of differences: let D_i = x_i - x_1 for i>1. Then p must be a divisor of G = gcd_{i=2..n} (x_i - x_1). Because then all differences are multiples of p, so all x_i ≡ x_1 mod p.
Also, the condition above about y0 ≤ x_1. Since y0 is either r (x_1 mod p) if r>0 else p. But note that if p divides G, then p divides all differences. However we also need that p and x_1 condition.
But maybe we can also choose y different from the smallest positive residue? That doesn't change existence; if the smallest positive residue > x_1, then there is no other because subtracting p gives negative. So condition is exactly that the smallest positive number congruent to x_1 modulo p is ≤ x_1.
Now, x_1 mod p = x_1 - p * floor(x_1/p). That is between 0 and p-1. The smallest positive number is:
if x_1 % p != 0: (x_1 % p)
else: p.
This is exactly the remainder when dividing x_1 by p (non-zero remainder) or p if remainder 0. That number is ≤ x_1? Let's check: if remainder > 0, then remainder < p and also remainder <= x_1 because x_1 could be less than p? Actually if x_1 < p, then remainder = x_1 (since x_1 % p = x_1). That is ≤ x_1 obviously. If x_1 >= p, remainder could be any number from 0 to p-1, but if remainder >0, then remainder < p ≤ x_1? Not necessarily: p could be greater than x_1, then remainder = x_1 (since x_1 < p, remainder = x_1). So remainder >0 always and equals x_1 if p > x_1. That is ≤ x_1. So if remainder >0, it's always ≤ x_1? Let's test: x_1 = 10, p = 7, remainder = 3, which is ≤10. Yes. x_1=10, p=12, remainder =10 ≤10. So if remainder >0, then it's exactly x_1 when p > x_1, otherwise it's less than p ≤ x_1? Actually if p ≤ x_1, remainder is x_1 mod p, which is at most p-1 < p ≤ x_1, so also ≤ x_1. So when remainder >0, we always have y0 = remainder ≤ x_1. Good. So the only problematic case is when remainder = 0 (i.e., p divides x_1). Then y0 = p, and we need p ≤ x_1. That is the condition: if p divides x_1, then p must be ≤ x_1. But p divides x_1 implies p ≤ x_1 automatically because p is positive and p ≤ x_1 if x_1 is multiple of p? Not necessarily: if x_1 = 0? But x_1 ≥ 1. If x_1 is a multiple of p, then p can be larger than x_1? Actually if x_1 is a multiple of p, then x_1 = kp, with k integer ≥1. Then p = x_1/k ≤ x_1. So automatically p ≤ x_1. So if p divides x_1, then p ≤ x_1 holds because k≥1 implies p = x_1/k ≤ x_1. However there is a special case: k could be 0? No. So it seems that condition y0 ≤ x_1 is automatically satisfied for any p such that all x_i ≡ x_1 mod p? Let's check the case where p > x_1 but p does not divide x_1. Then remainder = x_1 (since x_1 < p), which is >0, and y0 = x_1 ≤ x_1, so okay. If p > x_1 and p divides x_1? That's impossible because if p > x_1 and divides x_1, then x_1 ≥ p because multiple of p must be at least p, contradiction unless x_1=0. So impossible. So indeed, for any p such that all x_i ≡ x_1 mod p, we always have that the smallest positive residue is either x_1 (if p > x_1) or the remainder (which is ≤ x_1) or p (if p ≤ x_1 and p divides x_1, then p ≤ x_1). So condition y0 ≤ x_1 is automatically satisfied! Wait, consider p = x_1 exactly. Then remainder = 0 (since x_1 mod x_1 = 0). y0 = p = x_1, which ≤ x_1. So ok. Consider p = 2, x_1 = 1. Then remainder = 1, y0=1 ≤1. So indeed always holds. But is there any scenario where y0 > x_1? That would require that the smallest positive number in the residue class be > x_1. That would happen if r > x_1 (i.e., remainder > x_1) OR if r=0 and p > x_1. But as argued, r > x_1 is impossible because r < p, and if p > x_1 then r = x_1 (since x_1 < p), so r = x_1 ≤ x_1. So r cannot be > x_1. If r = 0, then the smallest positive is p; we need p > x_1 for it to be > x_1. But if r=0 then p divides x_1, which implies p ≤ x_1. Because if p divides x_1, then x_1 = kp with k≥1 => p = x_1/k ≤ x_1. So p cannot be > x_1. So indeed, for any p such that all x_i ≡ x_1 mod p (i.e., p | differences and also maybe p | something? Actually does all x_i ≡ x_1 mod p automatically guarantee that x_1 mod p is something? No additional condition. The reasoning about r > x_1 being impossible holds regardless of the divisibility condition; it's just a property of remainder: r = x_1 mod p is in [0, p-1]. If p > x_1, then r = x_1. So r ≤ x_1. If p ≤ x_1, then r is some number less than p ≤ x_1, so r ≤ x_1. So always r ≤ x_1. And if r = 0, then p divides x_1, so p ≤ x_1. So y0 = (r if r>0 else p) is always ≤ x_1. Therefore, the condition y0 ≤ x_1 is automatically satisfied given that all x_i share the same residue mod p. That's interesting. So the only condition needed is that all x_i are congruent modulo p, i.e., p divides all differences x_i - x_1.
But wait: Could there be an issue with y needing to be ≤ x_1? Yes, we have shown that if all x_i ≡ x_1 mod p, we can always set y = (x_1 mod p) if that is non-zero, else y = p. However need to check that y is ≤ x_1 indeed. As argued, it's always ≤ x_1. But consider p > x_1, then x_1 mod p = x_1, so y = x_1. That is ≤ x_1. Good. For p ≤ x_1 and x_1 mod p = r > 0, r < p ≤ x_1, so r ≤ x_1. For p ≤ x_1 and r=0, then y = p ≤ x_1. So okay. So indeed we can always set y = (x_1 % p) if not zero, else p. But note that y must be at least 1: if p = 1? p can be 1, then r = x_1 % 1 = 0, y = 1 (since p=1) which is ≤ x_1 if x_1 ≥ 1. So fine.
Therefore, the problem reduces to: find any p from given list such that p divides G = gcd_{i=2..n} (x_i - x_1). If such p exists, we can output y = x_1 % p (if not 0) else p, and the index j of that p.
But is there any subtlety about y being the first ring? If we set y = x_1 % p, does the series contain x_1? For x_1, we have x_1 = y + kp? If y = x_1 % p, then x_1 - y is divisible by p because (x_1 - (x_1 % p)) = p * floor(x_1/p). That is nonnegative. So k = floor(x_1/p) is >= 0. So yes. For other x_i, since they are ≡ x_1 mod p, similarly x_i - y is divisible by p, and since x_i > x_1, we need to ensure that the k for x_i is nonnegative? That will be true because x_i - y >= x_i - x_1? Actually if y could be larger than x_i? But y ≤ x_1 < x_i, so it's fine. So yes.
But we need to also ensure that y is at least 1. As argued, if p = 0? Not possible. So fine.
Now consider the case where n=1? The constraints say n >= 2 (2 ≤ n ≤ 3e5). So at least two events, so G is defined.
Thus the solution appears simple: compute G = gcd of (x_i - x_1) for i=2..n. Then for each p_j, check if G % p_j == 0? Wait, condition is p_j divides G? Actually we need p_j to divide all differences, i.e., p_j | (x_i - x_1) for all i. That is equivalent to p_j | G. Since G = gcd of all differences, any divisor of G will divide each difference. But do we require exactly p_j dividing G? Yes, because if p_j divides each difference, then p_j is a divisor of G? Actually G is the greatest common divisor; any common divisor divides G. Conversely, if p_j divides G, then p_j divides each difference. So p_j divides all differences if and only if p_j | G. So we need to find any p_j such that p_j divides G.
But careful: The condition is p_j must be a divisor of G. However, is it necessary that p_j itself divides G? If G is large, p_j could be larger than G and not divide G? Could p_j still divide all differences? If p_j > G, it might still divide all differences if the differences are multiples of p_j. But then p_j would be a common divisor, so it must divide G? Actually if p_j divides each difference, then the gcd G of those differences is a multiple of p_j? No, the gcd is the greatest common divisor, meaning that every common divisor divides the gcd. Actually the gcd is the largest integer that divides all numbers, but it is also true that any common divisor must divide the gcd. Because if d divides each number, then d divides any integer combination, and gcd is such combination. So yes, d | G. So if p_j divides all differences, then p_j | G. So p_j must be ≤ G? Not necessarily; if p_j divides G, then p_j ≤ G (since divisor cannot be larger than the number, except if number is zero). But G could be zero? Differences: x_i - x_1: all positive because strictly increasing. So G > 0. So indeed p_j ≤ G. So any p_j that divides all differences must be ≤ G. So we only need to check p_j that are divisors of G.
Thus solution: Compute G = gcd(x_2 - x_1, x_3 - x_1, ..., x_n - x_1). Then iterate over p_j and find one such that G % p_j == 0. If found, output y = (x_1 % p_j) if (x_1 % p_j) != 0 else p_j. Output index j.
But wait, is there any scenario where p_j divides all differences but the condition about y fails? As argued, it's always okay. So this seems correct.
Let's test with examples:
Example 1:
n=3, m=5, x=[3,12,18], p=[2,6,5,3,3]
Differences: 12-3=9, 18-3=15 => G = gcd(9,15)=3.
Now p_j that divide 3: p_4=3 (index 4), p_5=3 (index 5). So choose p=3. Then x_1=3, p=3 => x_1%p=0, so y=p=3. Output y=3, j=4 (or 5). That matches example: YES, 3 4.
Example 2:
n=4, m=2, x=[1,5,17,19], p=[4,5]
Differences: 5-1=4, 17-1=16, 19-1=18 => gcd(4,16,18)=? gcd(4,16)=4, gcd(4,18)=2. So G=2. p_j: 4 does not divide 2; 5 does not divide 2. So no answer -> NO. Matches.
Example 3:
n=4, m=2, x=[1,5,17,19], p=[2,1]
G=2 as above. p_1=2 divides 2 -> ok. y = x_1%2 = 1%2=1, so y=1, j=1. Output YES 1 1. That matches.
So far matches examples.
But let's double-check edge cases. What if G = 1? Then the only p_j that divides 1 is p_j = 1. So if there is a p_j = 1, answer exists. Else NO. That seems fine.
What if x_1 is huge, p_j divides G but p_j > x_1? Could that happen? Since p_j divides G and G is gcd of differences. If p_j > x_1, then p_j cannot be ≤ G? Actually if p_j divides G, then p_j ≤ G. So if p_j > x_1, then G ≥ p_j > x_1. But what is G? G is the gcd of differences, each difference is at least 1, and could be larger than x_1. For instance, x_1 = 10, x_2 = 30, x_3 = 50, differences = 20, 40, G=20. So p_j could be 20, which is > x_1=10. Then p_j > x_1. But is it allowed? p_j = 20, x_1=10, remainder = 10%20 = 10, so y = 10. That's ≤ x_1? y = 10 = x_1, yes. So it's fine.
What about p_j = G itself, and G > x_1, but p_j divides x_1? Not necessarily. But as we argued, y = x_1 % p_j = x_1 (since p_j > x_1) => y = x_1, which is ≤ x_1 (equal). So okay.
Now we need to ensure y is positive: if x_1 = 0? But constraints x_i >=1, so fine.
Thus the solution is straightforward: compute G and check for any p_j dividing G.
But wait, are there any other possibilities? Could there be a scenario where p does not divide G but still all x_i are congruent modulo p? That would mean p is a common divisor of the differences, so by definition it divides each difference, hence it divides the gcd. So it must divide G. So no.
Thus the problem reduces to that.
However, need to be careful with large numbers up to 1e18, and up to 3e5 numbers. Computing G by iterating over differences and taking gcd with 64-bit integers is fine, using C++ with __int128 maybe? But 1e18 fits in 64-bit signed? 1e18 fits in 2^63-1 approx 9.22e18, so it's safe in signed 64-bit. But gcd of differences can be up to 1e18 as well, which fits. So we can use unsigned long long or __int128 for safety in multiplication? Actually gcd computation only uses subtraction/mod which are fine within 64-bit if both numbers are less than 2^63? mod operation may produce remainder less than divisor. In C++ with 64-bit signed, we can use std::gcd (C++17) which uses Euclidean algorithm with mod. However, note that the differences might be as large as 1e18 - 1, but still within 64-bit. So we can compute G using unsigned long long or long long. Be careful about overflow when computing differences? x_i up to 1e18, difference up to 1e18, still fits in signed 64-bit (max 9.22e18). So safe.
Now we need to iterate over p_j list of length up to 3e5 and check if G % p_j == 0. That's O(m) with modulo operation, fine.
But is there any nuance about p_j possibly being 0? No.
Now, but we must also output y. y = x_1 % p if that remainder != 0 else p. But what if the remainder is 0 and p = something? y = p, but we need to ensure y ≤ x_1? As argued it's true. However, what about if p is 0? No.
Now, what if p divides G but also p divides x_1? Then y = p, which is ≤ x_1 because p | x_1 implies p ≤ x_1. So fine.
Thus the solution appears trivial.
But wait, the problem statement: "Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of p. He has to pick it among the given values p_1, p_2, ..., p_m (his phone does not support any other options for this setting)." So the above reasoning is correct.
However, there might be a twist: Could there be a scenario where p_j does not divide G but still all x_i are congruent modulo p_j? Let's re-express: x_i ≡ x_1 (mod p) means p | (x_i - x_1). So indeed p divides each difference. So p is a common divisor of all differences. The gcd of differences is the largest common divisor, but any common divisor must divide the gcd. So if p divides all differences, then p divides G. So check G % p == 0 is necessary and sufficient.
But what about the possibility of p being larger than some differences? If p > some difference, say x_2 - x_1 = 5, p=10, then 10 does not divide 5, so p cannot divide that difference. So not a common divisor.
Thus indeed.
But consider if G is 0? That would happen if all differences are zero, but x_i are strictly increasing, so differences > 0, so G > 0.
Thus solution is simple.
But is there any scenario where y might need to be something else because the smallest positive residue could be less than 1? No.
Now check more complex test: n=2, x=[2,5], p list maybe [3]. Differences: 3, G=3. p=3 divides G. x_1=2, p=3 => x_1%3=2, so y=2. Check: rings at 2,5,... yes. Good.
Another: x=[4,6], p=2, G=2, x_1=4, 4%2=0 => y=2? But 2 is less than x_1=4 and 2 mod 2 =0, so rings at 2,4,6,... works. But is y=2 valid? Yes, alarm rings at 2,4,6,... so it rings at 4 and 6. Good.
What about x=[4,6], p=4? G=2, p=4 does not divide 2 -> no.
Thus correct.
Now, we need to be careful about reading input and output.
Given constraints up to 3e5, O(n + m) is fine.
Implementation details:
Read n, m.
Read list x of n numbers. (x[0..n-1])
Compute G = 0; For i from 1 to n-1: diff = x[i] - x[0]; G = gcd(G, diff). (use C++ std::gcd, but careful with negative? differences positive.)
Then read p list of m numbers.
Iterate over j from 0 to m-1: if G % p[j] == 0, then found. Record index = j+1, p_val = p[j].
Compute y: let r = x[0] % p_val; if r == 0: y = p_val; else y = r.
Output YES, y, index.
But is there any scenario where y computed this way could be 0? If x[0] % p_val == 0, then we set y = p_val, which is >=1. Good.
But what about case p_val = 0? Not possible.
Now, ensure that y is within 1..1e18: Since p_val ≤ 1e18 and x_0 % p_val is less than p_val, so y ≤ max(p_val, x_0 % p_val) ≤ max(p_val, p_val-1) = p_val ≤ 1e18, and positive.
Thus done.
But before finalizing, let's double-check the reasoning about the first minute y being exactly the first ring. Could there be a constraint that the alarm clock starts ringing exactly at minute y and then every p minutes; but if we set y = x_1 % p when that remainder is less than x_1, then there will be rings before x_1, that's fine. However, what if the remainder is less than x_1 but not necessarily the smallest positive number? Suppose we set y = x_1 % p, that is the smallest non-negative residue? Actually it's the remainder between 0 and p-1. But if remainder is 0, we set y = p, which is not the remainder (which would be 0), but we choose p as the smallest positive number congruent to 0 mod p. So that's the smallest positive. For remainder r>0, the smallest positive number congruent to r is r itself, because r is positive and less than p, so it's indeed the smallest positive in that residue class (since subtracting p gives negative). So we are effectively choosing y as the smallest positive number in the residue class.
Now, is there any requirement that y be the first ring? Yes. But if we choose y = r (where r = x_1 % p), it is indeed the smallest positive in that class? Let's test: Suppose x_1=10, p=8, then r=2. The numbers congruent to 2 mod 8 are ... -6, 2, 10, 18,... The smallest positive is 2. So yes. So that is correct. For x_1=8, p=4, r=0, smallest positive is 4, we set y=4. But note that 4 is less than x_1=8, and there is also a ring at 8 (since 4+4=8). Good.
But is it possible that y = p (when r=0) is not the only option? Could we choose y = x_1 itself? x_1 is also congruent to 0 mod p? Actually if r=0, then x_1 is multiple of p. So x_1 ≡ 0 mod p. Could we set y = x_1? That would be first ring at x_1, then next at x_1 + p, etc. That would still satisfy the events because it would ring at x_1 and x_2 (since x_2 is also multiple of p, as all differences are multiples of p, and x_1 is multiple, so x_2 = x_1 + kp, so also ring). However, is it allowed that the first ring is at x_1? Yes, because y can be any minute; but then the alarm would not ring before x_1. That's allowed. So we could also choose y = x_1. But our computed y is the smallest positive residue, which might be smaller than x_1. Both are valid. The problem allows any answer. So it's fine.
But is there a scenario where y = x_1 % p (if >0) might be > x_1? No.
Now, about the constraints: n up to 3e5, so computing gcd of up to 3e5 differences each up to 1e18. That's fine.
Potential pitfalls: The gcd of many large numbers: use iterative gcd, it's fast enough.
Now, is there any scenario where G is 0? As said, n >=2, and x_i strictly increasing => differences > 0 => G > 0. So fine.
Now consider scenario where p_j is 0? Not possible.
Thus solution seems straightforward.
But before final answer, let's think if there is any hidden nuance: The alarm rings at minutes y, y + p, y + 2p, ... The events are at minutes x_i. For each event, there must exist some k such that x_i = y + k p. So we need x_i ≡ y (mod p). For all i, they must share the same remainder y mod p. So picking y and p essentially means that all x_i have the same remainder modulo p. So we need to choose p from list such that all x_i are congruent mod p. That's the condition.
Now, why did we need to consider G? Because all x_i are congruent mod p iff p divides all pairwise differences. Since they are increasing, it's enough that p divides each difference with respect to first: x_i - x_1. So yes.
Now, we also need to output y such that y <= x_1? Actually is it necessary? Let's double-check: If we set y > x_1, then the first ring is after the first event, so we cannot have a ring at x_1 because we need x_1 = y + kp with k >=0. If y > x_1, then y + kp ≥ y > x_1 for all k≥0, so no ring at x_1. So we must have y ≤ x_1. So that's a constraint. However, if we pick p such that all x_i ≡ r (mod p) and we set y = some number congruent to r mod p, we must also ensure y ≤ x_1. As argued, if we pick the smallest positive number in the residue class, it will be ≤ x_1? We reasoned it's always ≤ x_1. But is that always true? Let's test with a contrived scenario: Suppose x_1 = 5, p = 3. Then residues: x_1 % 3 = 2. The smallest positive number congruent to 2 mod 3 is 2. 2 ≤ 5, okay.
x_1 = 1, p = 1: x_1%1=0 => smallest positive is 1, ≤1.
x_1 = 1, p = 2: x_1%2=1 => 1 ≤1.
So yes.
Now consider x_1 = 0? Not allowed.
So the condition holds.
But wait, what if p divides x_1? Then smallest positive is p. Since p divides x_1, p ≤ x_1. So okay.
Thus no issue.
Thus solution stands.
But maybe there is an alternative approach: They might want to output y = x_1 if that is congruent? But not necessary.
Now, we should be careful about integer overflow when computing differences: x_i could be up to 1e18, and x_1 could be 1, difference up to 1e18-1, safe in 64-bit signed. In C++ using long long (which is usually 64-bit signed) is okay. Use unsigned long long or __int128 for multiplication? Not needed.
Now, computing gcd: Use std::gcd in C++17 which works for signed integers? It's defined for any integer types, but careful: std::gcd typically returns absolute value. For positive numbers it's fine. In C++14 we can use own gcd: while(b) b = a % b, swap. That's fine.
Now, complexity: O(n + m) and modulo operations for m up to 3e5, okay.
But is there any trick: The input x_i up to 1e18, n up to 3e5, but x_i are in increasing order. Could there be a case where G becomes 1 quickly? But still fine.
Now, I should think about possibility of p being zero? No.
Now, is there any scenario where we need to consider that p must be positive, which it is.
Thus solution is extremely simple.
But wait, the problem might be from Codeforces and typical solution is exactly this. However, I've seen similar problem: "Alarm Clock" maybe from Codeforces Round? I recall a problem where we need to find p from list such that all x_i are congruent modulo p, and then output y. Yes, it's exactly that.
But sometimes there is nuance: The p values could be repeated? In example they have duplicate 3. That's fine.
Now, about output: "YES" or "NO". For YES, print y and j.
Now, ensure we output j as index (1-based) of p_j.
Now, implement.
But before finalize, let's test with some more cases:
Case: n=2, x=[1000000000000000000, 1000000000000000001], p=[1]. Differences = 1, G=1, p=1 divides G. y = x1 % 1 = 0? Actually x1 % 1 = 0, so set y = p = 1. Check: rings at 1,2,... includes 1e18 and 1e18+1? Since p=1, it rings every minute, so yes. That works. y=1 <= x1. So okay.
Case: n=3, x=[10, 15, 30], p=[5,6]. Differences: 5,20 => G=gcd(5,20)=5. p=5 divides 5, p=6 does not. So choose p=5. y = 10%5=0 => y=5. Check: rings at 5,10,15,20,25,30,... works. y=5 <=10.
Case: n=3, x=[2,4,10], p=[2,3,4]. Differences: 2,8 => G=2. p=2 divides, p=4? 4 does not divide 2. So answer p=2. y=2%2=0 => y=2. Check: rings at 2,4,6,8,10,... works.
Now, what about scenario where p divides G but also p divides x_1? Already considered.
Now, what about scenario where p divides G but also p > x_1? Example: x=[5, 25, 45], differences:20,40 => G=20. p=20 divides 20. p=20 > x1=5? Actually 20 >5. y = 5%20=5 => y=5. Check: rings at 5,25,45,... works (5+20=25, +20=45). So okay.
Now, maybe there is a scenario where p divides G but the computed y is > x_1? Could it happen? Suppose x_1 = 5, p=3, y = 2 (since 5%3=2) ≤5. If x_1=2, p=3, y=2 ≤2. If x_1=1, p=2, y=1 ≤1. So no.
Thus all good.
Now, is there any scenario where p divides G but we cannot pick any y because y must be at least 1? Always satisfied.
Now, consider extreme: x_1 = 1, p = 1, y=1.
Now, what if G is huge (like 1e18) and p_j is also huge, but we need to compute G % p_j. That's fine with modulo on 64-bit.
Thus solution is fine.
But wait: I recall some Codeforces problem where they required to find a p that divides the differences, but also p must be such that the remainder is the same for all x_i. But maybe there's also the possibility that we can set y to be any number with that remainder, not necessarily the smallest. But the condition that y must be ≤ x_1 is critical. However, is it possible that the smallest positive residue is > x_1? We argued no, but let's double-check mathematically:
Let p > 0, let r = x_1 mod p, with 0 ≤ r < p. The set of numbers ≡ r (mod p) is { r + kp | k ∈ Z }. The smallest positive element of this set is:
If r > 0, then r (since r>0, and r-p = r-p ≤ 0 (if p>r then negative; if p≤r? but r<p so r-p<0). So r is the smallest positive.
If r = 0, then the numbers are multiples of p. The smallest positive is p (since p>0, and next smaller would be 0 which is not positive). So y0 = (r if r>0 else p).
Now we claim y0 ≤ x_1.
Proof: If r > 0, then r = x_1 - p*floor(x_1/p) ≤ x_1 because floor(x_1/p) ≥ 0, so r = x_1 - nonnegative multiple, so r ≤ x_1. If r = 0, then x_1 is multiple of p, i.e., x_1 = kp for some k ≥ 1. Then p = x_1/k ≤ x_1. So y0 = p ≤ x_1. So always y0 ≤ x_1. Good.
Thus condition holds.
Therefore the answer is as described.
Now implement.
But note: The problem statement: "Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of p." So y can be any minute, not necessarily positive? They require 1 ≤ y ≤ 10^18. So y must be at least 1. Our y0 is at least 1 because if r>0, r≥1; if r=0, p≥1, so y0≥1. Good.
Thus all set.
Now, one more nuance: The p_j values can be up to 10^18, and G can be up to 10^18. But what if G = 0? Not possible. So fine.
Now, we should handle the case where there are multiple p_j that work. We can just pick the first one.
Now, is there any scenario where G is huge and p_j is huge and we compute G % p_j, which is okay.
Now, potential integer overflow in computing differences: x_i - x_1 could be up to 10^18, but x_i up to 10^18, x_1 at least 1, so difference up to 10^18-1, fits in 64-bit signed. In C++, long long is typically signed 64-bit, range -9.22e18 to 9.22e18. So safe.
Now, compute G: start with 0, then for each diff: G = gcd(G, diff). Use built-in __gcd or custom gcd. In C++17, std::gcd works with signed integers but might return negative if arguments negative; but ours are positive. So safe.
Now, what if G becomes 1 early, we can break? Not necessary.
Now, a potential pitfall: The gcd of many large numbers might be 0 if all diffs are 0, but they aren't.
Now, solution accepted.
But wait, there might be a more subtle twist: The p_j values are not necessarily divisors of G? We already reasoned.
But what about scenario where p_j is 0? Not possible.
Now, confirm with another example: Suppose x = [6, 10, 14], differences: 4,8 => G=4. p list: [2,4]. Both work. For p=2: y = 6%2=0 => y=2. Alarm rings at 2,4,6,8,10,12,14,... works. For p=4: y = 6%4=2 => y=2? Actually 6%4=2 => y=2, rings at 2,6,10,14,... includes 6,10,14? Yes. Wait 2,6,10,14 works: 2 is first, then 6,10,14. But does it ring at 10? 2+2*4=10, yes. So okay.
Now, another: x = [3,7,11], differences:4,8 => G=4. p=4 works: y = 3%4=3 => y=3, rings at 3,7,11,... works. p=2 also works: y=3%2=1 => y=1, rings at 1,3,5,7,9,11,... includes 3,7,11? 1+12=3, 1+32=7? Actually 1+32=7, yes. 1+52=11, yes. So fine.
Thus consistent.
Now, consider large n and m: O(n + m) works.
Thus answer: compute G, find p_j divisor.
But wait, are we missing the case where n=2? Works.
Now, double-check problem constraints: n up to 3e5, m up to 3e5, x_i up to 1e18, p_j up to 1e18.
Now, could there be a scenario where p_j is not a divisor of G but still all x_i are congruent mod p_j? As argued no.
But consider: p_j divides each difference individually, but perhaps G is not the gcd of differences? Wait, if p_j divides each difference, then p_j is a common divisor, so it must divide the gcd of the differences. So yes, p_j | G. So necessary.
Thus solution stands.
Now, I recall a Codeforces problem 1730A? No. Actually it's "Alarm Clock" maybe Codeforces Round #735 (Div. 2) problem D? Not sure. But anyway.
Now, implement in C++.
Pseudo:
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector x(n);
for (int i=0; i<n; i++) cin >> x[i];
long long G = 0;
for (int i=1; i<n; i++) {
long long diff = x[i] - x[0];
G = gcd(G, diff);
}
vector p(m);
for (int j=0; j<m; j++) cin >> p[j];
int idx = -1;
for (int j=0; j<m; j++) {
if (G % p[j] == 0) {
idx = j;
break;
}
}
if (idx == -1) {
cout << "NO\n";
} else {
long long y;
long long pval = p[idx];
long long r = x[0] % pval;
if (r == 0) y = pval;
else y = r;
cout << "YES\n";
cout << y << " " << idx+1 << "\n";
}
return 0;
}
But wait: What about case where G is 0? Actually n>=2 and strictly increasing => differences>0, so G>0. So okay.
Now, one more nuance: The alarm rings at minutes y, y+p, y+2p,... It requires that for each event x_i, there exists a nonnegative integer k_i such that x_i = y + k_i p. That's equivalent to y ≡ x_i (mod p) and y ≤ x_i. Since we already have y ≤ x_1 ≤ x_i for i>1, that is satisfied. But is there any scenario where y is greater than x_i for some i? Since we ensure y ≤ x_1, and x_i >= x_1, so y ≤ x_i for all i, so it's fine.
Thus correct.
Now, let's test with a more complex case: x = [100, 150, 200], differences: 50,100 => G=50. p list: [10, 25, 50]. p=10 works: y = 100%10=0 => y=10. Check: 10,20,... includes 100,150,200? 100=10+910, 150=10+1410, 200=10+19*10, works. p=25: y=100%25=0 => y=25. rings at 25,50,75,100,125,150,175,200,... works. p=50: y=100%50=0 => y=50, rings at 50,100,150,200,... works.
Now, what about p=20? 20 divides G? G=50, 20 does not divide 50, so no.
Thus fine.
Now, what about x = [1, 1000000000000000000], differences ~1e18-1, G = that difference (since only one difference). p list: [2] would work only if difference divisible by 2. If difference is odd, then p=2 does not work. So that's correct.
Now, edge case: n=3e5, all x_i are equally spaced? Then G = step size maybe. But fine.
Now, note: The p_j list may contain 1, which always divides any G, so answer always exists if 1 is in list. That's allowed.
Thus the problem seems too easy? But maybe there's a catch: The alarm rings during minutes x_1, x_2, ..., x_n. Does it require that the alarm rings exactly at those minutes, and not necessarily that all rings are at those minutes? But we already covered. Could there be a scenario where p divides all differences, but the computed y (the smallest positive residue) might be less than 1? No.
Now, check the condition: "it will ring during minutes y, y + p, y + 2p, ...". So it is an infinite arithmetic progression. For each event minute x_i, there must be some integer k >= 0 such that y + k p = x_i. So we need existence. That's what we used.
Thus solution is correct.
Wait, but is there any possibility that we can choose y not equal to the remainder but something else that still satisfies y ≤ x_1? For example, suppose p divides all differences, but x_1 mod p = r >0. We set y = r + p (which is also congruent). But r + p may be > x_1? If x_1 < r + p, then y > x_1, not allowed. So only y <= x_1. The set of y values congruent to r mod p and ≤ x_1 are exactly numbers of the form r + kp where k can be negative or zero? Actually if we take k negative, y = r - p. Since r < p, r-p < 0, not allowed. So the only y ≤ x_1 are r itself (if r ≤ x_1) and maybe r - p? negative. So actually for r>0, the only candidate y ≤ x_1 that is congruent is r itself (provided r ≤ x_1) because the next smaller would be r - p which is negative, and the next larger is r + p which > x_1? Not necessarily: if p is small, r+p could be ≤ x_1. For example, x_1=10, p=3, r=1. Then r+p=4 ≤10, r+2p=7 ≤10, r+3p=10 ≤10. So there are multiple y values: 1,4,7,10 all are ≤10 and congruent to 1 mod 3. So we could choose any of those. The smallest positive is 1, but we could choose 10 as well. However, does the condition "first minute it will ring" require that there is no ring before y? If we choose y=10, then the alarm rings at 10,13,... It will ring at 10 and then 13, etc. It will not ring at 1,4,7. That's allowed. So we have many possible y. But our computed y (smallest positive) is just one valid answer. So it's fine.
But is it always true that there exists some y ≤ x_1? Yes, we can take y = x_1 itself if we want. But we need to ensure that y is congruent mod p. Since all x_i are congruent mod p, x_1 itself is congruent to r. So y = x_1 works and is ≤ x_1 (equal). So for any p that divides all differences, y = x_1 is always valid (since x_1 ≡ x_1 mod p, and then for any i, x_i = x_1 + (x_i - x_1) = x_1 + k_i p, where k_i = (x_i - x_1)/p is integer and nonnegative). So indeed, if p divides all differences, we can simply set y = x_1. That's even simpler! Because then for each i, x_i = y + ((x_i - x_1)/p) * p, and (x_i - x_1)/p >=0. So y = x_1 is always a valid first ring. And x_1 is between 1 and 1e18. So we don't even need to compute the remainder logic. But wait: Is there any case where y = x_1 is not allowed because it would require that the alarm's first ring is at x_1, but then the alarm would also ring at x_1 + p, etc. That's fine. However, we must ensure that the alarm's first ring is exactly y; if we set y = x_1, then the first ring is at x_1. There is no requirement that there be a ring before the first event; it's fine. So indeed y = x_1 is always a valid choice when p divides all differences. That simplifies the output further: just output x_1 and the index j. Check with examples: Example1: x_1=3, p=3, y=3 works. Example2: no p. Example3: x_1=1, p=2, y=1 works. So indeed y = x_1 always works when p divides differences. So we can just output x_1.
But wait, is there any scenario where y = x_1 is not allowed because it might conflict with the definition of "first minute"? The alarm is set with properties y and p. It will ring at minutes y, y+p, ... So if we set y = x_1, then indeed the first ring is at x_1. That's fine. There's no requirement that there must be a ring before the first event. So it's acceptable. So we can simplify: find p_j such that p_j | G, then output "YES", then output y = x_1 and j.
But is there any scenario where p_j divides G but p_j does NOT divide x_1? That's fine; we set y = x_1. Then does it hold that x_1 ≡ something mod p_j? Actually we need x_i = y + k_i p_j for all i. With y = x_1, we have x_i = x_1 + k_i p_j, so k_i = (x_i - x_1)/p_j must be integer. Since p_j divides all differences, it is integer. So works. So y = x_1 is always a valid first ring. So indeed the problem reduces to just finding p_j dividing G. No need to compute remainder. However, need to check if y = x_1 is always allowed as first minute? Yes, because it's a minute (1 ≤ x_1 ≤ 1e18). So we can output x_1.
But the sample output for example1 is "3 4", which is y=3, p=3. That's x_1=3. So matches. For example3, output "1 1", which is x_1=1. So matches.
Thus we can always output y = x_1.
But is there any nuance: The first minute y must be such that the alarm rings at minute y, and then at y+p, etc. If we set y = x_1, then the first ring is at the first event. That's allowed. So it's fine.
But wait: Could there be a case where x_1 is not the first ring because there might be an earlier ring required? No, the definition: "the first minute it will ring (let's denote it as y)". So y is exactly the first ring. So setting y = x_1 means the first ring is at x_1. That's okay. So answer can be y = x_1.
Thus the problem is even simpler.
But is there any situation where p_j divides G but we cannot set y = x_1 because x_1 might be less than 1? No.
Thus solution: find any p_j dividing G. If found, output YES, x_1 and index j.
But is there any scenario where p_j divides G but p_j is larger than x_1? That doesn't matter because we set y = x_1. Then the ring times are x_1, x_1+p, x_1+2p,... For x_1, k=0; for x_2, x_2 = x_1 + (x_2-x_1). Since p divides (x_2-x_1), there exists integer k_2 = (x_2-x_1)/p, which is positive. So okay.
Thus the only requirement is that p_j divides all differences.
Now, is there any requirement that the alarm clock rings during minute x_i, meaning exactly at that minute, not before or after? Yes.
Thus solution stands.
Now, is there any possibility that p_j divides all differences but also we need to consider that the alarm must ring during all given minutes, and if we set y = x_1, it does. So good.
Now, consider the following: What if there are multiple events at the same minute? The problem says x_i are strictly increasing, so all distinct.
Thus final answer.
Now, we need to output index j (1-based) of p_j. Since there can be duplicates, we pick any.
Now, implementation: After reading p_j list, loop through them, check if G % p_j == 0. If yes, output.
But we must be careful about G possibly being 0? Not possible. But if n=1? Actually n >= 2, so differences exist and G>0.
Now, let's check with a random test: n=2, x=[5,7], differences=2, G=2. p=[2,3,4]. p=2 works. Output YES, 5, index of 2. Check: alarm at 5,7,9,... works.
Now, check with p=1: works.
Now, check with p=7? 7 does not divide 2, so no.
Now, what about p=0? Not allowed.
Thus solution is good.
Now, note: Could it be that the problem expects to output y as something else? Possibly they expect the smallest y? But they said any answer.
Thus we are done.
But let's double-check the logic with a slightly more complex scenario: Suppose x = [4, 6, 9]. Differences: 2,5 => G = gcd(2,5)=1. p list: [2]? 2 does not divide 1, so answer would be NO. But is it indeed impossible? Let's test manually: Can we find p from list [2] and y such that alarm rings at 4,6,9? For p=2, need all x_i ≡ y (mod 2). 4 mod2=0, 6 mod2=0, 9 mod2=1 -> not all same. So indeed impossible. So correct.
Now, what if p list includes 1? Then G=1, 1 divides 1, so answer YES, y=4. Alarm with p=1 rings every minute, so it rings at 4,6,9. So correct.
Now, edge: What if G is huge but p_j list contains a divisor that is not necessarily a factor? So we need to check divisibility.
Now, algorithm complexity: O(n + m). Works for 3e5.
Now, but careful: The differences could be up to 1e18, and we compute gcd of many numbers. The gcd algorithm's cost is logarithmic in the numbers, so fine.
Now, we should consider the potential of using __int128 for safety, but not needed.
Now, I'll produce final answer.
But wait, I recall a Codeforces problem "Ivan and Powers of Two"? No.
Given the problem is from Codeforces, sometimes they have large input and output, so we must use fast I/O.
Now, one more nuance: The problem states "the i-th of them will start during the x_i-th minute." It doesn't say "at minute x_i", but "during the x_i-th minute". That likely means exactly at that minute. So it's fine.
Now, also note: The alarm clock rings during minutes y, y+p, ... So if we set y = x_1, then for i=1, k=0, it rings exactly at that minute. For others, it's fine.
Thus solution final.
Now, I'll write solution with y = x_1.
But the sample output uses y=3 and j=4 (which is x_1). So matches.
Thus final code:
code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<ll> x(n);
for (int i = 0; i < n; i++) {
cin >> x[i];
}
ll G = 0;
for (int i = 1; i < n; i++) {
ll diff = x[i] - x[0];
G = gcd(G, diff);
}
vector<ll> p(m);
for (int j = 0; j < m; j++) {
cin >> p[j];
}
int idx = -1;
for (int j = 0; j < m; j++) {
if (G % p[j] == 0) {
idx = j;
break;
}
}
if (idx == -1) {
cout << "NO\n";
} else {
cout << "YES\n";
cout << x[0] << " " << idx+1 << "\n";
}
return 0;
}
But wait, check: Should we output x[0] or maybe need to output y such that y is the first ring? If we set y = x[0], then indeed first ring is at x[0]. However, we need to make sure that for the first event, the alarm rings exactly at that minute. That is satisfied. So fine.
But is there any scenario where setting y = x[0] fails because we need the first ring to be at minute y, but the event might require the alarm to ring at that minute, and it does. So ok.
Now, but wait: Suppose x_1 is not congruent to something? But p divides differences, so x_1 is congruent to itself mod p. So it's fine.
Thus final answer is that.
But just to be thorough, consider scenario: n=2, x=[2,5], G=3, p=[3]. y=x_1=2, then alarm rings at 2,5,8,... works.
Now, consider scenario: n=2, x=[1,1000000000000000000], G=999999999999999999, p=[999999999999999999]. y=1, then alarm rings at 1, 1+... etc. But does it ring at 1e18? 1 + k * p = 1 + 1 * 999... = 1e18? Actually 1 + 999... = 1e18, so k=1, works.
Now, everything consistent.
Thus answer is correct.
I think the problem is intended to be solved this way.
Now, I'll write final response. The solution is to compute the greatest common divisor (G) of all differences (x_i - x_1) for (i = 2,\dots,n).
A suitable interval (p_j) must divide (G); equivalently (G \bmod p_j = 0).
If such a (p_j) exists, we can simply choose the first ring (y = x_1) (which is always valid) and output its index.
Otherwise, it is impossible to satisfy the conditions.
cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<ll> x(n);
for (int i = 0; i < n; ++i) cin >> x[i];
ll G = 0;
for (int i = 1; i < n; ++i) {
ll diff = x[i] - x[0];
G = gcd(G, diff);
}
vector<ll> p(m);
for (int j = 0; j < m; ++j) cin >> p[j];
int idx = -1;
for (int j = 0; j < m; ++j) {
if (G % p[j] == 0) {
idx = j;
break;
}
}
if (idx == -1) {
cout << "NO\n";
} else {
cout << "YES\n";
cout << x[0] << " " << idx + 1 << "\n";
}
return 0;
}