We need to find the minimum v such that from numbers 1..v we can select cnt1 numbers that are not divisible by x, and cnt2 numbers that are not divisible by y, with all numbers distinct, and no number is used for both. That means we need to partition a subset of {1..v} into two disjoint sets: set A (for friend1) of size cnt1, where each element not divisible by x; set B (for friend2) of size cnt2, where each element not divisible by y. The remaining numbers can be unused.
We need to know if such selection is possible for a given v, and find the smallest v.
Constraints: cnt1, cnt2 up to 1e9, x,y primes, x<y. The sum is up to 1e9, so we need efficient check, perhaps binary search on v.
Given v, we need to see if we can assign numbers to meet requirements. Let's analyze the constraints.
Let total numbers = v.
Let numbers that are multiples of x: Mx = floor(v/x). Similarly My = floor(v/y). Also numbers that are multiples of both x and y, i.e., multiples of lcm(x,y). Since x and y are primes (distinct, as they are different primes, because x<y and both prime), they are coprime? Actually x and y are primes but not necessarily distinct? x<y so they are distinct primes, thus gcd(x,y)=1, so lcm = xy. So Mxy = floor(v/(xy)).
Now, friend1 cannot get numbers divisible by x. So A must be subset of numbers not divisible by x. Similarly B must be subset of numbers not divisible by y.
We need to choose two disjoint subsets of sizes cnt1 and cnt2 from the set of numbers 1..v, respecting these divisibility constraints.
We need to check feasibility.
Define categories:
Let U = {1..v}.
We can partition U into four groups based on divisibility by x and y:
- Divisible by both x and y (i.e., multiples of x*y): size d_both = Mxy.
- Divisible by x but not y: size d_x_only = Mx - Mxy.
- Divisible by y but not x: size d_y_only = My - Mxy.
- Divisible by neither: size d_none = v - Mx - My + Mxy.
Now, friend1 (A) cannot use numbers divisible by x. So A can only take from groups: d_y_only (since divisible by y only, not x) and d_none (neither). Also note that d_both is divisible by x, so cannot be used by friend1. Similarly, friend2 (B) cannot use numbers divisible by y, so B can only take from groups: d_x_only and d_none. Also they cannot share numbers.
We need to assign cnt1 numbers to A from available in groups 3 (d_y_only) and 4 (d_none). And cnt2 numbers to B from groups 2 (d_x_only) and 4 (d_none). And groups 2,3,4 have limited supplies, and group 4 is shared.
Thus it's a resource allocation problem: we have shared resource (group4) that can go to either friend, but each friend also has exclusive resources: friend1 gets exclusive group3; friend2 gets exclusive group2. Also groups1 (both) cannot be used by anyone.
So we need to check if we can satisfy:
Let a4 be the number of numbers from group4 assigned to A, and b4 from group4 assigned to B. Then:
a4 + (from group3) = cnt1, but from group3 we can assign at most d_y_only. So we need to assign some a3 ≤ d_y_only to A; but group3 can only be used by A (since B cannot use numbers divisible by y; group3 divisible by y, so B cannot use them). So all of group3 are exclusively available for A. So we can set a3 = min(cnt1, d_y_only). However we might also need to use group4 if a3 < cnt1. So we need:
a3 = min(d_y_only, cnt1) but we can also decide to not use all of group3 if we want? Actually we want to minimize usage of shared resource, so we should use as many exclusive as possible. So the feasibility condition: we must have at least cnt1 numbers from the union of group3 and group4. That is:
cnt1 ≤ d_y_only + d_none? Not exactly because group4 is also used for B. So we need to allocate group4 between A and B. So we need to see if there exists a4, b4 such that:
a4 + b4 ≤ d_none,
a4 + d_y_only >= cnt1? Actually a4 + (use all d_y_only) >= cnt1? But we can also not use all d_y_only if we want, but it's optimal to use as many exclusive as possible, so we would use all d_y_only for A as long as needed. So we need:
Let a3 = min(cnt1, d_y_only). Then remaining for A from group4: need cnt1 - a3. So a4_needed = max(0, cnt1 - d_y_only). Similarly for B: let b2 = min(cnt2, d_x_only). Then b4_needed = max(0, cnt2 - d_x_only).
We need a4_needed + b4_needed ≤ d_none. Additionally, we must ensure that we are not forced to assign numbers from group3 to B (they cannot) or group2 to A (they cannot). So the condition above is necessary and sufficient, because we can allocate exactly needed from group4, and all exclusive numbers are used for respective friend, and the leftover group4 can be left unused. However, we must also ensure that the needed numbers from group4 do not exceed d_none. That is:
Let needA = max(0, cnt1 - d_y_only)
Let needB = max(0, cnt2 - d_x_only)
If needA + needB ≤ d_none, then feasible.
But wait: there is also possibility of using some numbers from group4 for A even if A already satisfied by group3, but that wouldn't be needed. So condition is correct.
But is there any other subtlety? For example, could we use some numbers from group2 for A? No, because group2 divisible by x, so A cannot take them. Could we use some numbers from group3 for B? No, because divisible by y. So exclusive groups are forced to be used only by the corresponding friend. So yes.
But what about numbers that are divisible by both? They cannot be used by either friend, so they are wasted.
Thus the feasibility condition is: needA + needB ≤ v - Mx - My + Mxy.
Now note: d_y_only = My - Mxy, d_x_only = Mx - Mxy, d_none = v - Mx - My + Mxy.
Alternatively, we could also think in terms of counting numbers that are forbidden for each friend etc. But this condition seems straightforward.
But wait, check example 1: cnt1=3, cnt2=1, x=2,y=3, v=5.
Compute Mx = floor(5/2)=2, My=floor(5/3)=1, Mxy= floor(5/6)=0.
d_y_only = My - Mxy =1 (the number 3)
d_x_only = Mx - Mxy =2 (numbers 2,4)
d_none = v - Mx - My + Mxy =5 -2-1+0=2 (numbers 1,5)
needA = max(0,3-1)=2
needB = max(0,1-2)=0
needA+needB =2 ≤ d_none=2 => feasible. Yes.
Example 2: cnt1=1,cnt2=3,x=2,y=3,v=4
Mx=floor(4/2)=2, My=floor(4/3)=1, Mxy=0
d_y_only=1, d_x_only=2, d_none=4-2-1=1 (number 1? Actually numbers: 1,2,3,4: 2,4 are multiples of 2; 3 is multiple of 3; 1 is none. So d_none=1)
needA = max(0,1-1)=0
needB = max(0,3-2)=1
needA+needB =1 ≤ d_none=1 => feasible. Yes.
So condition works.
But is it sufficient? Yes, because we can always assign: give A all d_y_only (up to cnt1), give B all d_x_only (up to cnt2), and then assign needed numbers from d_none to each. Since d_none is large enough, we can assign distinct numbers. So it's sufficient.
Now we need to find the smallest v such that this condition holds.
Because cnt1 and cnt2 up to 1e9, v can be large, maybe up to something like 2e9? But we can binary search. The condition involves floor division, we can compute in O(1). So binary search on v.
What is upper bound for v? Worst case: we might need v up to something like (cnt1 + cnt2) * something? Since each number can be used at most once, and we need total numbers cnt1+cnt2, but some numbers are forbidden to each friend, so we may need more numbers to avoid forbidden overlaps. However, since we are required to select distinct numbers, the total numbers needed is at least cnt1+cnt2. But we also may have to skip some numbers that are forbidden for both or forced to be excluded because they are only usable by one but we need both? Actually the condition ensures we have enough usable numbers. The worst-case scenario is when x and y are small primes, many numbers are divisible by them, reducing usable numbers. So we might need v significantly larger than cnt1+cnt2. Could v be as high as 2e9? Since cnt1+cnt2 ≤ 1e9, but each number can be used at most once. In worst case, suppose x=2, y=3. The density of numbers not divisible by 2 or 3 is about (1 - 1/2 - 1/3 + 1/6) = 1/3. So to get N numbers not divisible by either (for the shared part) plus numbers divisible by only y or only x? But we need cnt1 numbers not divisible by x and cnt2 numbers not divisible by y. The worst-case v might be about 3*(cnt1+cnt2) maybe? But could be larger due to distribution: needA and needB come from d_none. The worst-case is when both needA and needB are large, i.e., when d_y_only is small compared to cnt1 and d_x_only small compared to cnt2. That occurs when My and Mx are small relative to cnt1, cnt2. My is floor(v/y). So for v not too large, My is small. For large cnt1, needA is nearly cnt1, so we need d_none to be about cnt1+cnt2. So v must be such that d_none >= cnt1+cnt2. Since d_none ≈ v*(1 - 1/x - 1/y + 1/(xy)). For x=2,y=3, factor = 1 - 0.5 - 0.333... + 0.166... = 0.333..., so v must be at least about 3*(cnt1+cnt2). That's at most 3e9, which exceeds 32-bit signed int but fits in 64-bit. But constraints: cnt1+cnt2 ≤ 1e9, so 3e9 is okay. However, maybe even larger if x and y are both small? For x=2, y=3 is the smallest possible, since x ≥2, y ≥3 (since primes, distinct). So the worst factor is when x=2,y=3 gives smallest d_none proportion. For other primes, factor is larger, so v needed is less. So the maximum v needed would be when x=2,y=3 and cnt1 and cnt2 are both large (e.g., 5e8 each). Then we need d_none ≈ cnt1+cnt2? Actually needA+needB could be as high as cnt1+cnt2 if exclusive groups are insufficient. But d_none is roughly v/3, so v ≈ 3*(cnt1+cnt2) ≈ 3e9, which is within 64-bit. So we can set high bound to something like 2*(cnt1+cnt2)? Maybe 4e9 to be safe. But note cnt1+cnt2 ≤ 1e9, so 4e9 is safe. Alternatively, we can use high = 2e18? But we need careful with binary search overflow. Let's set high = 2e18? Actually we can compute safely with 64-bit. Since cnt1, cnt2 up to 1e9, v up to maybe 2e9? Actually need to consider that needA = max(0, cnt1 - (My - Mxy)). My is at most v/3 for y=3, so My ≈ v/3. So needA ≈ cnt1 - v/3 + something. For v around 3(cnt1+cnt2), needA+needB might be around something. But we can derive an upper bound: the condition needA+needB ≤ d_none. Let's try to find worst-case v in terms of cnt1+cnt2. Since needA = max(0, cnt1 - (My - Mxy)). But My - Mxy = floor(v/y) - floor(v/(x(1/y - 1/(x( (x-1)/(x(1/ (2y) = (1)/(2(1/x - 1/(x( (y-1)/(x( (3-1)/(6) ) = v*(2/6)=v/3. d_none ≈ v*(1 - 1/x - 1/y + 1/(xy)) = v*( (xy - y - x +1)/(xy) ) = v*( ( (x-1)(y-1) )/(xy) ). For x=2,y=3: (1*2)/(6)=2/6=1/3.
Now, needA = max(0, cnt1 - v/6). needB = max(0, cnt2 - v/3). Actually careful: needA uses d_y_only = My - Mxy ≈ v*(1/y - 1/(xy)). For x=2,y=3: 1/3 - 1/6 = 1/6. So needA approx max(0, cnt1 - v/6). needB uses d_x_only = Mx - Mxy ≈ v*(1/x - 1/(xy)). For x=2: 1/2 - 1/6 = 1/3. So needB approx max(0, cnt2 - v/3). And d_none ≈ v/3.
Condition needA+needB ≤ d_none ≈ v/3.
If both cnt1 and cnt2 are large, we need v such that (cnt1 - v/6) + (cnt2 - v/3) ≤ v/3, i.e., cnt1+cnt2 ≤ v/6 + v/3 + v/3 = v/6 + 2v/3 = (v/6 + 4v/6) = 5v/6? Wait compute: needA = cnt1 - v/6 (if >0), needB = cnt2 - v/3 (if >0). Then condition: cnt1 - v/6 + cnt2 - v/3 ≤ v/3 => cnt1+cnt2 ≤ v/6 + v/3 + v/3 = v/6 + 2v/3 = v/6 + 4v/6 = 5v/6 => v ≥ (6/5)(cnt1+cnt2) = 1.2*(cnt1+cnt2). That's lower than 3*(cnt1+cnt2). But this is approximate and depends on which need is positive. Actually, if v is small, both needs may be positive. For v increasing, needA and needB become zero eventually. But the worst-case v might be when cnt1 is huge relative to cnt2? Let's test extremes: Suppose cnt1 is very large, cnt2 is 0? But cnt2 ≥1 (given cnt1, cnt2 >=1? Actually input says: 1 ≤ cnt1, cnt2 < 10^9, yes both at least 1). So both positive. However, needA uses d_y_only which is relatively small (since y > x, 1/y smaller). So needA could be large if cnt1 large. needB uses d_x_only which is larger (since 1/x larger). So perhaps worst-case is when cnt1 is large and cnt2 moderate, because needA requires many from d_none. But also d_none is shared.
Let's derive exact feasibility condition.
Let v be given.
Define:
A_excl = max(0, cnt1 - d_y_only) // needed from shared
B_excl = max(0, cnt2 - d_x_only)
Need = A_excl + B_excl
If Need ≤ d_none, feasible.
We can also think in terms of total numbers that are usable for each friend, but condition is correct.
Now to binary search, we need lower bound low and high.
Lower bound: obviously must be at least cnt1+cnt2 because we need that many distinct numbers. But also might need more because of forbidden numbers. So low = cnt1+cnt2? Actually could be lower? No, we need at least cnt1+cnt2 numbers total, and numbers are distinct. So v must be at least cnt1+cnt2. But maybe if cnt1+cnt2 is larger than v, impossible. So low can be set to 0 or 1 but we can set low = 1. But better low = cnt1+cnt2? But could it be that v = cnt1+cnt2 - 1 works? Not possible because we need that many distinct numbers from 1..v. So low = cnt1+cnt2.
Upper bound: need to find a v where condition definitely holds. Could be as high as 2e18? But we can compute a safe upper bound. Since cnt1, cnt2 up to 1e9, we can try to find a v that is definitely enough: For instance, v = 2 * (cnt1 + cnt2) * something? Let's check worst-case scenario: need to ensure condition holds. For any v, d_y_only = floor(v/y) - floor(v/(xy)). This is at least floor(v/y) - v/(xy). Actually floor differences could be less, but asymptotically v*(1/y - 1/(xy)). Similarly d_x_only = floor(v/x) - floor(v/(xy)). d_none = v - floor(v/x) - floor(v/y) + floor(v/(xy)). For large v, these are about v * (1 - 1/x - 1/y + 1/(xy)). Since x≥2,y≥3, the smallest proportion for d_none is when x=2,y=3: about 1/3. So if we take v = 2 * (cnt1 + cnt2) * 3? Actually we want Need ≤ d_none. Need at most cnt1 + cnt2 (since A_excl ≤ cnt1, B_excl ≤ cnt2). So a sufficient condition is cnt1+cnt2 ≤ d_none. Since d_none ≈ v * factor, we can set v such that factor * v ≥ cnt1+cnt2. With factor min = 1/3, v ≥ 3(cnt1+cnt2). But careful: Need might be less than cnt1+cnt2 if some of the cnt's are covered by exclusive groups. But worst-case Need = cnt1+cnt2 (when both exclusive groups are zero). This could happen if v is so small that d_y_only = 0 and d_x_only = 0. For very small v, exclusive groups might be zero. So worst-case Need = cnt1+cnt2. So condition Need ≤ d_none implies we need d_none ≥ cnt1+cnt2 at least. But if v is large enough, exclusive groups become positive and reduce Need. However, to guarantee feasibility we can simply set v = 3 * (cnt1+cnt2) works? Let's test with x=2,y=3: v=3N, then d_none ≈ N, but exclusive groups also take some numbers, but Need may be less. But is it always feasible for v = 3*(cnt1+cnt2)? Let's test extreme: cnt1=1e9, cnt2=1e9? Actually cnt1+cnt2 ≤ 1e9, so N ≤ 1e9. v=3e9. Compute Mx = floor(3e9/2)=1.5e9, My= floor(3e9/3)=1e9, Mxy= floor(3e9/6)=5e8. Then d_y_only = 1e9 -5e8 =5e8, d_x_only=1.5e9-5e8=1e9, d_none=3e9 -1.5e9 -1e9 +5e8 = 1e9? Actually 3e9 -2.5e9 +5e8 = 1e9? 3e9 - 1.5e9 = 1.5e9, minus 1e9 = 5e8, plus 5e8 = 1e9. So d_none = 1e9. needA = max(0, cnt1 - 5e8) = 5e8, needB = max(0, cnt2 - 1e9) = 0, so need=5e8 ≤ d_none=1e9. Feasible. If cnt1=1e9, cnt2=1 (N≈1e9+1). needA ≈ 5e8, needB = max(0, 1 - 1e9) =0, need=5e8 ≤ 1e9. So it's fine.
But is there any case where v=3*(cnt1+cnt2) might not be enough? Let's test with x=2,y=3, cnt1 = 1e9, cnt2 = 1e9? Actually cnt1+cnt2 ≤ 1e9, so both cannot be 1e9 sum would be 2e9. So maximum sum is 1e9. So if cnt1 = 1e9-1, cnt2 =1, sum=1e9. v=3e9 gives d_none=1e9. needA = cnt1 - d_y_only (d_y_only=5e8) = (1e9-1)-5e8 = 5e8 -1? Actually 1e9-1 -5e8 = 5e8 -1 = 499999999? That's less than 1e9, feasible. So v=3e9 works. But could there be a case where need > d_none? With v=3N, d_none ~ N, but need could be as high as N if both exclusive groups are zero, but at v=3N, exclusive groups are not zero: d_y_only approx v/6 = N/2? Actually v/6 = (3N)/6 = N/2. So needA at most cnt1 - N/2; needB at most cnt2 - v/3 = cnt2 - N; since cnt2 ≤ N, needB <= 0. So needA <= cnt1 - N/2. Since cnt1 ≤ N, needA <= N - N/2 = N/2. So need <= N/2 ≤ d_none (≈ N). So it's fine.
But could we have a scenario where d_x_only is very small? For x large, 1/x is small. For x=2 it's largest. For x >2, d_x_only is even smaller (since 1/x smaller). But also d_none factor becomes larger because 1 - 1/x - 1/y + 1/(xy) increases as x increases. For x=3,y=5, factor = 1 - 1/3 - 1/5 + 1/15 = 1 - 0.3333 - 0.2 + 0.0667 = 0.5334, > 1/3. So v needed would be less. So the worst factor is indeed with smallest primes, x=2,y=3. So v=3*(cnt1+cnt2) is a safe upper bound. But to be safe, since we have floor operations, maybe we need a bit more. For v=3*(cnt1+cnt2), all approximations hold; but due to rounding down, d_none might be slightly less than the ideal proportion. However, we can increase the bound a little: maybe 4*(cnt1+cnt2) or even 2e9? Since cnt1+cnt2 ≤ 1e9, 4e9 is fine. Or we could compute a high bound as (cnt1+cnt2) * 2 * max(x,y)? That could be larger but still manageable. But to be safe, we could set high = 2e18? But that would cause binary search to loop many times (60 iterations) which is fine, but we need to avoid overflow in multiplication. We could just set high = 2e18? But then condition calculations with floor(v/x) etc. would involve dividing 2e18 by up to 3e4, which is fine in 64-bit. However, we must be careful that needA = max(0, cnt1 - (My - Mxy)) might be huge if cnt1 is up to 1e9 and My-Mxy might be negative? Actually My-Mxy is non-negative because My >= Mxy. So it's fine. But binary search on up to 2e18 would take about 60 steps, which is acceptable. But we can choose a more reasonable high to reduce iterations, but 60 is fine.
But note: Since cnt1,cnt2 up to 1e9, and v might need to be up to about 2e9? Actually with factor 1/3, to have d_none >= cnt1+cnt2, we need v >= 3*(cnt1+cnt2). Since cnt1+cnt2 max 1e9, v max 3e9. That's > 2^31-1 but < 2^32. So we can set high = 4e9, or 5e9 to be safe. But 4e9 fits within 64-bit. 2^31 is about 2.147e9, 2^32 is about 4.295e9. So we can set high = 2e9? Actually 2e9 might be insufficient if cnt1+cnt2=1e9, v may need 3e9. So we need high > 3e9. So high = 4e9 is safe. But we must consider that cnt1+cnt2 could be 1e9 and x=2,y=3, v needed maybe slightly more than 3e9? Let's test: Suppose cnt1 = 1e9, cnt2=0? But cnt2 ≥1, so sum at most 1e9. Actually cnt1 and cnt2 each at least 1, so sum at least 2. But max sum is 1e9. For sum = 1e9, what is minimum v? Could it be > 3e9? Let's try to find v that exactly satisfies condition. For x=2,y=3, d_none = v - floor(v/2) - floor(v/3) + floor(v/6). Need = max(0, cnt1 - (floor(v/3)-floor(v/6))) + max(0, cnt2 - (floor(v/2)-floor(v/6))). For cnt1 = 1e9, cnt2 = 1? Let's compute approximate v needed. needA = cnt1 - v/6, needB = max(0, 1 - v/3). For v around 2.4e9, v/6 = 0.4e9, needA ~ 0.6e9; d_none ~ v/3 = 0.8e9, so needA <= d_none. So v could be around 2.4e9. Actually let's solve: needA = cnt1 - (v/3 - v/6) = cnt1 - v/6. Condition needA <= v/3 => cnt1 <= v/6 + v/3 = v/2 => v >= 2cnt1. Since cnt1 up to 1e9-1, v >= ~2e9. That suggests v needed maybe around 2e9 for large cnt1, not 3e9. Let's derive: For x=2,y=3, if cnt2 is small, needB =0. Condition: cnt1 - v/6 <= v/3 => cnt1 <= v/2 => v >= 2cnt1. Since cnt1 could be up to 1e9 -1, v>=2e9 approx. If cnt2 also large, we had v >= 1.2*(cnt1+cnt2). So worst-case v may be around 2e9 (when cnt1 large, cnt2 small). So high = 4e9 is safe. But we should also consider that floor operations might reduce effective numbers, so we may need a bit more. Let's test extreme: cnt1 = 1e9, cnt2 = 1e9? That sum 2e9 but sum <=1e9. So not possible. So maximum v needed is less than maybe 2.5e9? But to be absolutely safe, high = 2e18 is overkill but safe; binary search with 60 steps is fine.
But we need to be careful: in the condition we compute floor(v/x) and floor(v/y), which for v up to 2e18 is fine with 64-bit integer (unsigned or signed 64-bit). However, we must be careful with multiplication v * something? No multiplication. Only division. So we can use 64-bit.
Thus we can binary search from low=1 to high=2e18, or set high as something like 2*(cnt1+cnt2) * max(x,y)? But easier: high = 2e18 (or 1e18?) But cnt1, cnt2 up to 1e9, v won't exceed about 2e9*? Actually if x and y are large (like near 3e4), factor close to 1, v about cnt1+cnt2, but that's small. So v never exceeds about 2e9? Wait, if x=2,y=3, v may need to be about 2cnt1 if cnt1 is near 1e9, that's 2e9. So upper bound 4e9 is safe. But to avoid thinking, we can set high = 2000000000LL * 2? Actually 2000000000 * 2 = 4e9, but we can also set high = 2000000000LL * 3 = 6e9. Since cnt1+cnt2 ≤ 1e9, v won't exceed 2e9? Let's test worst-case: we derived v >= 2cnt1 when cnt2 is negligible. Since cnt1 can be up to 1e9-1, v up to 2e9. But also if cnt2 is also large, v may be up to 1.2*(cnt1+cnt2) which is <= 1.2e9. So v max ~2e9. However, note that due to discrete nature, maybe we need a little more, but definitely less than 3e9? Let's test an extreme: cnt1 = 1e9 - 1, cnt2 = 1 (since sum <= 1e9, cnt2 at least 1). Then v needed? Let's try to find the minimal v using condition. We'll later test with code, but we can set high = 2e9 + some margin, maybe 4e9. But to be absolutely safe, high = 2e18 is fine. It only adds a few more iterations (log2(2e18) ≈ 61). So we can just set high = 1e18. However, we need to ensure that the check function doesn't overflow when computing needA+needB, which are up to cnt1,cnt2 (1e9), and d_none up to v (2e18) so 64-bit is fine.
But careful: In binary search, we will use while low < high, mid = (low+high)//2, if feasible(mid) then high=mid else low=mid+1. Start low=1, high= some big number like 2e18. That's fine.
But let's check for overflow when computing Mx = v / x; v could be up to 2e18, x at least 2, so division yields up to 1e18, fits in 64-bit (unsigned long long). But we store in 64-bit signed? In C++ we use long long (signed, up to 9e18). So fine.
Thus binary search with check function:
bool possible(LL v) {
LL Mx = v / x;
LL My = v / y;
LL Mxy = v / (xy); // since x,y primes, gcd=1, lcm = xy, but also careful: xy might overflow 64-bit? x and y up to 3e4, product up to 9e8, which fits in 64-bit easily. Actually xy <= 9e8, well within 64-bit. So safe.
// But note: if x*y may exceed v? That's fine, floor division yields 0.
LL d_y_only = My - Mxy;
LL d_x_only = Mx - Mxy;
LL d_none = v - Mx - My + Mxy;
// needA = max(0, cnt1 - d_y_only)
LL needA = cnt1 - d_y_only; if (needA < 0) needA = 0;
LL needB = cnt2 - d_x_only; if (needB < 0) needB = 0;
return needA + needB <= d_none;
}
But wait: is there any possibility of double counting? For example, d_none = v - Mx - My + Mxy. That's correct.
Now, check examples: earlier works.
But need to ensure we are using the right categories: Friend1 cannot have numbers divisible by x. d_y_only numbers are divisible by y, not by x. So friend1 can take them because they are not divisible by x. But are there any restrictions? Friend1 only dislikes numbers divisible by x. So yes, divisible by y is okay. Similarly, friend2 can take d_x_only. So correct.
Edge cases: when x and y are not coprime? They are both primes, distinct, so coprime. So lcm = x*y.
Now, consider if cnt1 or cnt2 is 0? But problem says 1 ≤ cnt1, cnt2, so both at least 1.
Now, binary search boundaries: low could be 1, but we know answer at least cnt1+cnt2. So low = cnt1+cnt2 might speed up but not required.
But careful: It's possible that answer might be less than cnt1+cnt2? No, because we need cnt1+cnt2 distinct numbers. So answer >= cnt1+cnt2. So we can set low = cnt1+cnt2. That's safe.
Now, high: we need to ensure that for high, possible(high) returns true. For high = 2e18, definitely true? Let's verify. As v → ∞, d_y_only ≈ v * (1/y - 1/(xy)), which grows linearly; d_x_only similarly; d_none ≈ v * factor. So needA becomes max(0, cnt1 - huge) = 0, needB = 0, so condition holds. So yes, for sufficiently large v it's feasible. So high = 2e18 works.
But we might want to avoid huge binary search ranges if we use 64-bit but it's fine.
Now we need to ensure the check function uses correct types: long long for v, cnt1, cnt2, x, y. cnt1, cnt2 up to 1e9, x,y up to 3e4, product up to 9e8, fits in int, but we can use long long to avoid overflow in v/x etc.
Potential issue: In computing Mxy = v / (xy), we should compute xy as (long long) x * y to avoid int overflow if we use int, but x*y fits in int? x and y up to 30000, product up to 900 million, which fits in 32-bit signed int (max 2e9). So safe, but still better to use LL.
Now implement binary search:
ll low = cnt1 + cnt2, high = 2e18; // maybe 2e18
while (low < high) {
ll mid = low + (high - low) / 2;
if (possible(mid)) high = mid;
else low = mid + 1;
}
cout << low << endl;
Check with examples.
But we must confirm that the condition is indeed necessary and sufficient. Let's think about any hidden constraints: numbers from 1..v, we need to assign exactly cnt1 numbers to A and cnt2 numbers to B. We can choose any numbers. The condition derived assumes we give A as many from its exclusive group (d_y_only) as possible, and B as many from its exclusive group (d_x_only) as possible, then assign the rest from d_none. This yields a feasible assignment if and only if the shared d_none is enough to cover the shortages. Is it always optimal? Yes, because exclusive groups can only be used by one friend, and using them for that friend reduces the need for shared group. So maximizing their usage minimizes need for shared group. So if it's possible, this allocation works. Conversely, if needA+needB > d_none, then even after using all exclusives, we still need more numbers from d_none than available, so impossible.
But careful: Could there be a scenario where it's beneficial not to use some exclusive numbers for a friend, to free up something? Since exclusive numbers cannot be used by the other friend anyway, if we don't use an exclusive number for its only possible friend, we would have to use a shared number for that friend instead, which increases demand on shared group. So that would only worsen the condition. So using all exclusive numbers (up to the friend's demand) is optimal.
Also, note that exclusive groups are mutually exclusive: d_x_only cannot be used by A, d_y_only cannot be used by B. So using them for the respective friend doesn't affect the other's allocation. So condition is correct.
Thus binary search works.
Now, let's test with some custom cases.
Case: cnt1=1, cnt2=1, x=2, y=3. Minimal v? We need at least 2 numbers. Check v=2: numbers 1,2. Mx=1, My=0, Mxy=0. d_y_only=0, d_x_only=1, d_none=1. needA = max(0,1-0)=1, needB = max(0,1-1)=0, need=1 <=1 => feasible? But is it feasible? We need A: numbers not divisible by 2 (so can be {1} or {??}) and B: numbers not divisible by 3 (so can be {2}). But note B cannot use numbers divisible by 3, but 2 is okay. So A gets 1, B gets 2. Works. So v=2 is enough. Our condition says feasible. Answer should be 2.
Check v=1: Mx=0,My=0,Mxy=0, d_y_only=0,d_x_only=0,d_none=1. needA = 1-0=1, needB=1-0=1, need=2 > d_none=1, not feasible. So answer 2. Good.
Case: cnt1=2,cnt2=2,x=2,y=3. Minimum v? Let's try v=4: numbers 1,2,3,4. Mx=2,My=1,Mxy=0. d_y_only=1,d_x_only=2,d_none=1. needA = max(0,2-1)=1, needB = max(0,2-2)=0, need=1 <=1 => feasible. Let's assign: A can take numbers not divisible by 2: {1,3,?} but 3 is divisible by 3? Actually A only dislikes 2; 3 is fine. So exclusive for A: d_y_only is numbers divisible by y (3) but not x, i.e., {3}. So a3=1, needA=1 from shared (d_none={1}). So A gets {3,1}. B can take numbers not divisible by 3: exclusive d_x_only numbers divisible by 2 but not 3: {2,4} both ok (not divisible by 3). So B gets {2,4}. Works. So v=4 works.
Check v=3: numbers 1,2,3. Mx=1,My=1,Mxy=0. d_y_only=1,d_x_only=1,d_none=1. needA = max(0,2-1)=1, needB = max(0,2-1)=1, need=2 > d_none=1 => not feasible. So answer 4.
Now case where x and y are larger: e.g., cnt1=5,cnt2=5,x=5,y=7. Compute minimal v? Could binary search.
Edge case: x and y are not necessarily small, but product may be large but still ≤ 9e8.
Now we need to ensure the binary search works for large numbers. Since low may be up to 2e9, high up to 2e18, range is huge, but log2(2e18) ~ 61, which is fine.
But we should also consider that the answer might exceed 2e18? No, because v will not need to be larger than something like 2cnt1 if cnt2 small. Since cnt1 ≤ 1e9, v ≤ 2e9, so 2e18 is more than enough. So we could set high = 2e9 * 2 maybe. But to be safe, we can compute a more precise upper bound: perhaps 2cnt1 + 2cnt2? Actually worst-case v could be when x=2,y=3 and cnt1 is large, cnt2 small. We derived v >= 2cnt1 if cnt2 negligible. Since cnt1 ≤ 1e9, v ≤ 2e9. But also for x=2,y=3, we can get more precise bound: needA = cnt1 - floor(v/3) + floor(v/6) maybe? Actually d_y_only = floor(v/3) - floor(v/6). So needA = max(0, cnt1 - floor(v/3) + floor(v/6)). This could be positive for v up to about 2cnt1. So v is at most 2cnt1. Since cnt1 <= 1e9, v <= 2e9. So high = 2e9 is enough? But what about cnt2 also large? In that case v may be less than cnt1+cnt2? Actually if both large, v might be around 1.2*(cnt1+cnt2). Since sum ≤ 1e9, v ≤ 1.2e9. So 2e9 is safe. But due to floor, maybe need a bit more, but 2e9+some margin should be enough. However, if cnt1=1e9, cnt2=1, v needed might be around 2e9+? Let's test exact minimal v for such scenario. We'll trust 2e9 is enough. But we could set high = 2e9+10, but binary search will still work if high is exactly the answer or higher. But we need guarantee that high is indeed feasible. If high = 2e9, is it always feasible? Suppose cnt1=1e9, cnt2=1, x=2,y=3. For v=2e9, compute: Mx = 2e9/2 = 1e9, My = floor(2e9/3) = 666,666,666? Actually 2e9/3 ≈ 666,666,666.666, floor = 666,666,666. Mxy = floor(2e9/6) = 333,333,333. So d_y_only = My - Mxy = 666,666,666 - 333,333,333 = 333,333,333. d_x_only = Mx - Mxy = 1e9 - 333,333,333 = 666,666,667? Actually 1e9 - 333,333,333 = 666,666,667 (since 1e9 is 1,000,000,000). d_none = v - Mx - My + Mxy = 2e9 - 1e9 - 666,666,666 + 333,333,333 = (2e9 - 1e9) = 1e9; 1e9 - 666,666,666 = 333,333,334; +333,333,333 = 666,666,667. needA = max(0, 1e9 - 333,333,333) = 666,666,667. needB = max(0, 1 - 666,666,667) = 0. Need = 666,666,667. d_none = 666,666,667. So needA+needB = 666,666,667 ≤ d_none = 666,666,667, feasible. So v=2e9 is feasible. Could there be a case where v=2e9 is not feasible? Possibly if cnt1 = 1e9, cnt2 = something moderate? But check needA = cnt1 - d_y_only. d_y_only = floor(v/3)-floor(v/6). For v=2e9, d_y_only = floor(2e9/3) - floor(2e9/6) = 666,666,666 - 333,333,333 = 333,333,333. So needA = 1e9 - 333,333,333 = 666,666,667. d_none = v - floor(v/2)-floor(v/3)+floor(v/6) = 2e9 - 1e9 - 666,666,666 + 333,333,333 = 666,666,667. So needA = d_none. So feasible. If cnt1 were 1e9 exactly, v=2e9 works. If cnt1 were 1e9, cnt2 = maybe 1? then needB =0, okay. If cnt2 is larger, needB could be positive but needA would be less because cnt1 is at maximum? Actually cnt1 + cnt2 ≤ 1e9, so if cnt1=1e9, cnt2 must be 0 (contradiction cnt2≥1). So maximum cnt1 is 1e9-1. So for cnt1 = 999,999,999, needA = 999,999,999 - 333,333,333 = 666,666,666. Then needA <= d_none = 666,666,667. So still feasible. So v=2e9 is feasible for all? But consider cnt1=1e9-1, cnt2=1, v=2e9, needA = 666,666,666, needB =0, total need = 666,666,666 ≤ d_none=666,666,667 feasible. So v=2e9 works. But what about x=2,y=3 and cnt1=500 million, cnt2=500 million? v=2e9? Compute: needA = max(0, 500M - 333,333,333) = 166,666,667, needB = max(0, 500M - 666,666,667) = 0, total need=166,666,667 <= d_none=666,666,667 feasible. So v=2e9 works. So high=2e9 seems safe for x=2,y=3. But what if x=2,y=3 and cnt1=1, cnt2=1e9-1? Then needA = max(0, 1 - 333,333,333)=0, needB = max(0, 999,999,999 - 666,666,667)=333,333,332, total need=333,333,332 <= d_none=666,666,667 feasible. So yes.
But is there any scenario where v must be >2e9? Let's try with x=2,y=3 and cnt1 and cnt2 both around 5e8? Actually sum <= 1e9. So worst-case v required may be when both needA and needB are large, we derived v >= 1.2*(cnt1+cnt2) which is ≤ 1.2e9. So actually v never needs to exceed about 2e9 for the extreme case where cnt1 is near 1e9 and cnt2 small. So 2e9 is safe. But consider x=2,y=3, and cnt1=1e9-1, cnt2=1, v=2e9 is feasible as we saw. Could the minimal v be greater than 2e9? Suppose cnt1=1e9-1, cnt2=1. The condition is needA = cnt1 - d_y_only, needB = max(0, cnt2 - d_x_only). For v slightly less than 2e9, needA could be > d_none. Let's test v=1,999,999,999. Compute floors. But approximate: v=2e9 gave needA = 666,666,667 (if cnt1=1e9). For cnt1=1e9-1, needA=666,666,666. d_none= v - floor(v/2)-floor(v/3)+floor(v/6). For v just below 2e9, these floors might reduce d_none slightly, and d_y_only might also reduce slightly, possibly making needA larger relative to d_none? Let's check worst-case: we need to ensure that for v=2e9, condition holds. So answer cannot exceed 2e9. So high=2e9 is safe.
But what about x=2,y=3 and cnt1 = 1e9, cnt2 = something? cnt1 cannot be 1e9 because cnt1 < 1e9 and cnt2 >=1 so sum <=1e9, so max cnt1 = 1e9-1. So v=2e9 is safe.
Thus we can set high = 2e9? But is there any scenario with other primes where v needed >2e9? For x=2,y=3 is worst. For x=2,y=3, the factor for d_none is ~1/3, and needA ~ cnt1 - v/6, needB ~ cnt2 - v/3. The worst v needed is when cnt1 is very large relative to cnt2, leading to v >= 2*cnt1. Since cnt1 ≤ 1e9-1, v <= 2e9 - 2. So safe. So high = 2e9 is enough. But to be extra safe with rounding, we can set high = 2e9 + 10 or 4e9. But I'd rather set high = 2e18 for simplicity and safety; binary search will take 60 steps, which is fine.
But careful: With high = 2e18, we need to be careful that Mx = v/x can be computed as v / x, which for v up to 2e18 yields up to 1e18, which fits in 64-bit signed. Good.
Now implement possible(v) with long long.
But we must also consider the possibility that x*y could overflow if we used int, but we'll cast to long long.
Now implement solve().
Edge Cases:
-
x and y are primes, but x < y. They are distinct primes, so gcd=1.
-
cnt1 and cnt2 can be up to 1e9, but we use long long.
Now let's test more examples.
Example 1: 3 1 2 3 => we got answer 5.
Binary search: low = 4? Actually cnt1+cnt2=4, low=4. Check v=4? We earlier computed for v=4: Mx=2,My=1,Mxy=0. d_y_only=1,d_x_only=2,d_none=1. needA = max(0,3-1)=2, needB = max(0,1-2)=0, need=2 > d_none=1, not feasible. v=5 feasible. So answer 5.
Example 2: 1 3 2 3 => low=4, v=4 feasible? For v=4: Mx=2,My=1,Mxy=0; d_y_only=1,d_x_only=2,d_none=1. needA = max(0,1-1)=0, needB = max(0,3-2)=1, need=1 <=1, feasible. So answer 4.
Now consider some edge: cnt1=1, cnt2=1, x=2, y=3 => answer 2. low=2, check v=2 feasible? Mx=1,My=0,Mxy=0: d_y_only=0,d_x_only=1,d_none=1; needA=1, needB=0, need=1 <=1, feasible. So answer 2.
Now consider x=2,y=3, cnt1=10^9-1, cnt2=1. We can compute approximate answer. Let's brute by binary search maybe but not needed.
Now we should double-check condition for scenario where cnt1 is less than d_y_only, then needA=0. Similarly for cnt2. That's fine.
But is there any scenario where using more from exclusive than needed could cause conflict? No.
Now let's think about if there is any possibility that some numbers from d_y_only or d_x_only might be used to cover the other friend's shortage if we don't use them? But they cannot be used for the other friend because of divisibility restrictions. So no.
Thus solution is clear.
Now, before coding, we must also consider that x and y are prime but not necessarily distinct? They are given as distinct primes? The problem states: x < y, and 2 ≤ x < y ≤ 3·10^4, both prime. So they are distinct.
Now, we need to compute Mxy = v / (xy). However, xy may be greater than v, then Mxy = 0, which is fine.
Now, we should also consider potential pitfalls: needA and needB may be large, but we are using long long.
Now, confirm that the condition is sufficient: we can construct actual sets? Not needed.
Now we also need to consider that we are to print minimum v. So binary search lower bound.
One more nuance: Are there numbers that are divisible by both x and y? They are not usable by either friend. So they are wasted. In our allocation, we didn't use them. That's fine.
Now, is there any possibility that we could use some numbers from d_both for one of the friends? No, because they are divisible by x (bad for friend1) and by y (bad for friend2). So not usable.
Thus everything is consistent.
Now we can implement.
But wait: The problem says: "You want to present each of them several positive integers." It doesn't require that we use exactly numbers 1..v? It says "find such minimum number v, that