We need to count positive integers X (1 ≤ X ≤ M) such that for each a_k (even positive), X = a_k * (p + 0.5) for some non-negative integer p. Equivalently, X = a_k/2 * (2p + 1), i.e., X is an odd multiple of a_k/2. Let b_k = a_k/2, which is an integer (since a_k is even). Then the condition becomes: X is a multiple of b_k, and X / b_k is odd. Because X = b_k * (2p+1). So X must be divisible by b_k, and the quotient must be odd.
Thus for each k, X must be a multiple of b_k and X / b_k ≡ 1 mod 2 (odd).
We need X that satisfies all such conditions simultaneously. Let’s denote the set of b_k.
We want X such that for all k, b_k | X and (X / b_k) is odd.
Note that if X is a common multiple of all b_k (i.e., X is a multiple of lcm(b_1,...,b_N)), does that guarantee the odd condition? Not necessarily; we also need that for each b_k, X/b_k is odd. However, if X is a multiple of all b_k, then X = L * t where L = lcm(b_i) and t integer. Then X/b_k = (L/b_k) * t. For X/b_k to be odd, we need t to be such that (L/b_k)*t is odd. Since L/b_k is an integer; denote d_k = L / b_k. Then condition: d_k * t is odd. This must hold for all k.
Alternatively, we can think in terms of the prime factorization. Condition: for each prime, maybe we need parity constraints.
Better approach: The condition X = b_k * (odd) can be expressed as: X must be divisible by b_k, and X must not be divisible by 2b_k? Actually, if X = b_k * odd, then X is not divisible by 2b_k because that would make quotient even? Wait: if X = b_k * odd, then X / b_k is odd, so it is not divisible by 2. So X is not divisible by 2b_k? Actually, if X were also divisible by 2b_k, then X/(2b_k) would be integer, but X/b_k would be even? Not necessarily: X could be b_k * odd, then 2b_k divides X only if that odd is even, which is impossible. So indeed X is not divisible by 2b_k. So condition: b_k | X, but 2b_k ∤ X.
But careful: Could X be divisible by 2b_k and still satisfy X/b_k odd? No, because if 2b_k divides X, then X = 2*b_k * m => X/b_k = 2m, which is even. So indeed condition equivalent: b_k divides X, and the highest power of 2 dividing X/b_k is 0 (i.e., v2(X/b_k)=0). But we can combine these constraints.
Let’s define v2(n) as exponent of 2 in n.
We need for each k: v2(X) = v2(b_k) (since X/b_k odd means v2(X) = v2(b_k)). Because if X = b_k * odd, then v2(X) = v2(b_k) + 0 = v2(b_k). So all b_k must have the same 2-adic valuation! Otherwise no X can satisfy both simultaneously. Because if there exist i,j such that v2(b_i) ≠ v2(b_j), then we would need v2(X) = v2(b_i) and v2(X) = v2(b_j) simultaneously, impossible. Thus a necessary condition is that all b_k have the same exponent of 2. If not, answer is 0.
Assuming they all have same v2, let v = v2(b_k) for all k. Then condition becomes: v2(X) = v. And also b_k must divide X. Since all b_k share same v2, we can consider the odd parts. Write b_k = 2^v * c_k, where c_k is odd. Then condition v2(X)=v means X = 2^v * y, where y is odd. Also b_k | X => 2^v * c_k divides 2^v * y => c_k | y (since 2^v cancels). So we need y to be an odd integer divisible by all c_k, i.e., y is a multiple of L_odd = lcm(c_1,...,c_N). Moreover, y must be odd (which is automatically satisfied if L_odd is odd, but careful: L_odd is lcm of odd numbers, so it's odd. And any multiple of an odd number could be even? Actually if y = L_odd * t, y could be even if t is even. But we require y odd. So we need t such that L_odd * t is odd => t must be odd as well because L_odd is odd, product is odd iff t is odd. So t must be odd.
Thus X = 2^v * y, where y = L_odd * t, t odd positive integer.
So the set of all semi-common multiples is X = 2^v * L_odd * u, where u is odd positive integer.
But wait: Are there any further constraints from the condition that b_k divides X? Already covered because L_odd is lcm of c_k, so any multiple of L_odd is divisible by each c_k. And since v2(X)=v, it's fine.
But double-check: Suppose X = 2^v * L_odd * u with u odd. Then for each k: b_k = 2^v * c_k. Since c_k | L_odd, we have L_odd = c_k * d_k for some integer d_k. Then X / b_k = (2^v * L_odd * u) / (2^v * c_k) = d_k * u. Since d_k is integer, and u is odd, d_k * u is odd only if d_k is odd? Wait, need X/b_k odd. If u is odd, then d_k * u is odd iff d_k is odd. So we need d_k to be odd. But is d_k = L_odd / c_k odd? L_odd is lcm of odd numbers, but L_odd/c_k may not be odd. Let's test with an example: b_k = 6 and 10. Then a_k=6,10 -> b_k=3,5? Actually a_k even: 6,10 -> b_k = 3,5. v2: both b_k are odd, v=0. c_k = 3,5. L_odd = lcm(3,5)=15. Then d_1 = 15/3 = 5 (odd), d_2 = 15/5 = 3 (odd). So ok. Another example: b_k = 2 and 6? But a_k even: a_k=4 and 12 -> b_k=2 and 6? But v2(2)=1, v2(6)=1? 2=21, 6=23 => v=1, c_k=1 and 3. L_odd = lcm(1,3)=3. Then d_1 = 3/1=3 odd, d_2=3/3=1 odd. So ok. But is it always the case that L_odd/c_k is odd? Since L_odd is lcm of odd numbers, and each c_k is odd, the ratio L_odd/c_k may have factors of primes that appear in other c_j but not in c_k. For odd primes, the ratio could be any integer, but it's odd because all primes are odd. Since L_odd and c_k are both odd, their ratio is odd (division of odd numbers yields odd if integer). Indeed, odd/odd is odd? Check: 9/3=3 odd; 15/5=3 odd; 21/7=3 odd; 45/9=5 odd. Actually, an odd number divided by an odd number yields an odd number only if the quotient is integer? But integer quotient of two odd numbers may be even? Let's test: 15/3=5 odd, 21/3=7 odd, 45/15=3 odd, 75/15=5 odd. Is there a case where odd/odd = even? Suppose odd1 = odd2 * even? But even times odd2 would be even, not odd. If both are odd, then product odd2 * even is even, so cannot be odd. So if quotient is integer, it must be odd. Because if quotient q = (odd1)/(odd2) is integer, then odd1 = odd2 * q. Mod 2: odd1 ≡ odd2 * q (mod 2). Since odd1 and odd2 are 1 mod2, we have 1 ≡ 1 * q (mod2) => q ≡ 1 mod2. So q is odd. Yes! So L_odd/c_k is odd. Therefore d_k is always odd. So the condition X/b_k odd reduces to requiring that u is odd (since d_k odd). Good. So our derived form is correct.
Thus semi-common multiples X are exactly numbers of the form:
X = L * u, where L = 2^v * L_odd, and u is any odd positive integer.
But careful: L = lcm of b_k? Let's compute L = lcm(b_1,...,b_N). Because b_k = 2^v * c_k, and lcm of b_k = 2^v * lcm(c_k) (since all have same v, the maximum exponent for 2 is v). Indeed lcm(b_i) = 2^{max_i v2(b_i)} * lcm(c_i) = 2^v * L_odd, because all v2 equal v. So L = lcm(b_i). So the condition becomes: X must be a multiple of L, and X/L must be odd. Because X = L * u, with u odd.
Check: If X = L * u with u odd, then for each b_k, b_k divides L, so L = b_k * d_k, with d_k = L/b_k. Since v2(L)=v, and b_k have v2=v, L/b_k is odd as argued. Then X/b_k = (L * u)/b_k = d_k * u, product of two odds => odd. So condition satisfied. Conversely, if X satisfies condition for all k, we argued that v2(X)=v, and X must be multiple of L_odd? Actually from earlier we deduced X = 2^v * y, with y multiple of L_odd and y odd, so X = 2^v * L_odd * t, t odd. That's exactly X = L * t with t odd. So equivalence holds.
Thus the problem reduces to: Count integers X in [1, M] such that X is a multiple of L = lcm(b_1,...,b_N), and X/L is odd.
But wait: Must also satisfy that L itself (and thus X) is positive. L may be huge, up to lcm of numbers up to 10^9, could exceed 10^18 easily. But M ≤ 10^9, so we only care about L ≤ M perhaps? Actually if L > M, then no multiple of L within [1, M] except possibly if L itself > M then none. But also need X/L odd. So if L > M, answer = 0. If L ≤ M, we need to count multiples of L up to M such that the quotient is odd.
So answer = number of integers k such that 1 ≤ k * L ≤ M and k is odd.
Thus answer = floor( (number of odd numbers ≤ floor(M/L) )? Actually we need count of odd k with k*L ≤ M. So let Q = floor(M / L). Then the number of odd k in [1, Q] = ceil(Q/2) or (Q+1)//2? Since among numbers 1..Q, half are odd if Q even: Q/2 odd; if Q odd: (Q+1)/2 odd. So = (Q+1)//2 integer division? Actually floor((Q+1)/2) = (Q+1)//2. Equivalent to (Q+1)//2.
But careful: k must be positive integer. So answer = (Q // 2) if Q even? Let's compute: number of odds = ⌈Q/2⌉. Because for Q=1, odds=1; Q=2, odds=1; Q=3, odds=2; Q=4, odds=2; So indeed = (Q+1)//2.
Thus answer = ( floor(M/L) + 1 ) // 2, provided that all b_i have same v2; else 0.
But we must also consider the possibility that L might be 0? No.
Now we must compute L = lcm(b_1,...,b_N) where b_i = a_i/2. But a_i up to 1e9, N up to 1e5, so lcm could be huge, far exceed 64-bit. But we only need to know if L <= M (1e9) and compute floor(M/L). Since M is at most 1e9, we can cap L at something > M because if L > M, answer is 0. So we can compute lcm incrementally, but whenever it exceeds M, we can break and output 0. Since intermediate lcm values may exceed 64-bit, we need to handle overflow. Use 128-bit or careful multiplication with check: compute l = lcm so far. For next b, compute l = l / gcd(l, b) * b. But this product may exceed M. We can check if l > M, then we can early exit because further lcm will only increase. But we must also check the v2 condition before. So algorithm:
- Read N, M.
- Read array a.
- For each a_i, compute b_i = a_i / 2.
- Check that all v2(b_i) are equal. Compute v2 for first b, then compare with others. If any differ, output 0.
- Compute L = lcm of all b_i, but capped at > M. Since we only care up to M. Compute L_cur = 1. For each b:
- Compute g = gcd(L_cur, b).
- Compute L_cur = L_cur / g * b. But need to ensure no overflow and check > M. Since M <= 1e9, we can use 64-bit (since product L_cur/g * b could be up to about 1e18? Actually L_cur might be up to 1e9 before multiplication, and b up to 5e8 (since a up to 1e9, b up to 5e8). So product can be up to ~5e17, which fits in 64-bit (max ~9e18). 64-bit signed can hold up to 9e18, so safe if we use unsigned long long (up to 1.8e19). But Python int is arbitrary precision anyway. However we can also early check: if L_cur > M, we can break (since further lcm will be >= L_cur, so >M). Actually if L_cur already > M, we can set a flag and break, answer 0.
- However we must be careful: L_cur could become > M even if intermediate product overflows in languages with fixed-size ints. But in Python, fine.
- But also, if during multiplication L_cur exceeds M, we can set L_cur = M+1 (or just break) because we only need to know if L <= M. So we can compute L_cur but if it exceeds M, we can immediately output 0.
- After processing all b, if L_cur > M, output 0. Else Q = M // L_cur, answer = (Q + 1)//2.
Edge Cases: L_cur might be 0? No.
Check examples:
Example1: N=2 M=50, a=[6,10] => b=[3,5]. v2(3)=0, v2(5)=0 same. lcm(3,5)=15. L=15 <=50. Q = floor(50/15)=3. odds: 1,3 => (3+1)//2=2. Output 2. Correct.
Example2: N=3 M=100 a=[14,22,40] => b=[7,11,20]. v2: 7 (0), 11 (0), 20 = 45? Actually 20=45, but 20 is even => v2(20)=2 because 20=2^2*5. So v2 differ: first two 0, third 2. So output 0. matches.
Example3: N=5 M=1000000000 a=[6,6,2,6,2] => b=[3,3,1,3,1]. v2: 3 (0),3 (0),1 (0),3 (0),1 (0) all same. lcm of b: lcm(3,3,1,3,1)=3. L=3. Q= floor(1e9/3)=333333333. odds count = (333333333+1)//2 = 166666667. matches.
Thus solution seems straightforward.
But wait: Are we sure that the condition "X is a multiple of L and X/L is odd" is sufficient and necessary given v2 equal? Let's double-check with some edge cases.
Suppose b_i = [2, 6]. b_i: v2(2)=1, v2(6)=1? 6=2*3 => v2=1. So v=1. c_i: 1,3. L_odd = lcm(1,3)=3. L = 2^1 * 3 = 6. So L=6. According to condition, X should be multiples of 6 with odd quotient. So X = 6, 18, 30, ... Check: For b1=2: X=6 => 6/2=3 odd -> ok. X=18 => 18/2=9 odd -> ok. For b2=6: X=6 => 6/6=1 odd ok. X=18 => 18/6=3 odd ok. What about X=12? 12 is multiple of 6 but quotient 2 even. For b1: 12/2=6 even, fails. So indeed only odd multiples.
But is there any X that satisfies the original condition but is not of this form? Let's test with more numbers: b = [4,12]. a = 8,24 => b=4,12. v2(4)=2, v2(12)=2 (since 12=43). So v=2. c: 1,3. L=43? Actually 2^2=4, L_odd=3 => L=12. So X must be multiples of 12 with odd quotient: 12,36,60,... Check: For b=4: X=12 => 12/4=3 odd ok. X=36 => 36/4=9 odd ok. For b=12: X=12 => 12/12=1 odd ok. What about X=24? 24/4=6 even fails. So fine.
Now consider case where b_i share same v2 but L might have higher v2? Actually L = lcm(b_i) will have v2 = max_i v2(b_i) = v because all equal. So L's v2 = v. So X/L odd ensures v2(X)=v as well? If X = L * u, u odd, then v2(X)=v2(L)=v. So condition holds.
Now consider if b_i are all equal? works.
Thus solution is correct.
Now we must handle N up to 1e5, M up to 1e9, a_i up to 1e9. Computing gcd and lcm for up to 1e5 numbers is okay. The lcm may become huge but we cap at M+1. However, even if we cap, we need to avoid overflow in languages like C++ but Python is fine. But in Python, we can just compute directly but we must be careful about performance: 1e5 gcd operations are fine. However, if we early break when L_cur > M, we can save some but not crucial.
One nuance: The condition that all v2(b_i) are equal is necessary. However, what if some b_i = 0? But a_i >= 2 even, so b_i >=1. So fine.
Now confirm with more tests:
Edge: N=1. Then semi-common multiples are X such that X = a1*(p+0.5) => X = b1*(odd). So count = number of odd multiples of b1 ≤ M. That is floor(M / b1) number of multiples, and half of them (ceil) are odd? Actually multiples of b1: k*b1, need k odd. So answer = ( floor(M/b1) + 1 ) // 2. This matches our formula: L = b1, v2(b1) = something, condition holds trivially, answer = (M//b1 +1)//2. Good.
Edge: if b_i all have same v2 but L > M -> answer 0.
Now consider when M is small.
Now, is there any hidden condition about p being non-negative integer? p+0.5 > 0 always, so X positive, that's fine.
Thus solution appears simple.
But before coding, let's think about potential pitfalls: The transformation X = a_k * (p+0.5) seems to assume that a_k is even. Indeed a_k even ensures that a_k/2 is integer. If a_k were odd, then a_k*(p+0.5) would be half-integer not integer? Actually p integer, so (p+0.5) is half-integer, product with odd yields half-integer? But a_k is even, so it's fine. The constraint says a_i even, so no issue.
Now confirm with another test: a = [4,8] => b=[2,4]. v2(2)=1, v2(4)=2 differ -> answer 0. Check manually: Need X such that X = 4*(p+0.5)=4p+2 => X ≡ 2 mod4; and X = 8*(q+0.5)=8q+4 => X ≡ 4 mod8. Simultaneous? X ≡2 mod4 and ≡4 mod8. Solve: X mod8: possibilities: from first, X could be 2,6 mod8; but need 4 mod8; no solution. So 0. Good.
Another test: a = [12,20] => b=[6,10]; v2(6)=1, v2(10)=1? 6=23 v2=1; 10=25 v2=1 -> ok. L = lcm(6,10)=30. Then X multiples of 30 with odd quotient: 30,90,150,... Check: For 12: 30/12=2.5 not integer? Actually condition: X = 12*(p+0.5) => X/12 = p+0.5 => X must be such that X/12 is half-integer. X=30 => 30/12=2.5 => p=2? 2.5 = 2+0.5 so p=2 works. For 20: 30/20=1.5 => p=1 works. So yes. 60? 60/12=5 -> integer, not half-integer; fails. So matches.
Now consider case where L = 0? not.
Now algorithm steps in detail:
- Input N, M.
- Read list a.
- Compute list b = [x//2 for x in a].
- Compute v2 for first b: while b0%2==0: count; set v0.
- For each b_i, compute its v2, compare to v0. If any differ: print 0 and exit.
- Compute L = 1.
- For each b_i:
- g = gcd(L, b_i)
- L = L // g * b_i
- If L > M: print 0 and exit (since L > M no X can satisfy)
- After loop, Q = M // L
- ans = (Q + 1) // 2
- Print ans.
But careful: In the process of computing L, we might have L become > M even before processing all b. In that case, we can immediately output 0. Indeed if at any point L exceeds M, the final lcm will be >= L, so definitely > M, so answer 0.
But what if during multiplication L // g * b_i exceeds M but we haven't overflowed? Fine.
Now ensure we handle large N efficiently. gcd is O(log min(L,b_i)). L may become large but capped; but we may early break.
Edge case: L could be 0? No.
Now test with maximum values: N=1e5, all b_i = 2 (i.e., a_i=4). Then v2 all 1, L = lcm of many 2's = 2. L=2 ≤ 1e9. Q = 5e8. ans = (5e8+1)//2 = 250000000? Actually M=1e9, M//2=500000000, ans = 250000000? Wait (500000000+1)//2 = 250000000.5? Actually integer division: (500000000+1)//2 = 500000001//2 = 250000000 (since 500000001/2=250000000.5 floor = 250000000). But careful: number of odd k from 1 to 500000000: exactly 250000000 because half of 500M are odd. So ans=250000000. That's plausible.
Now if all b_i=10^9/2? Actually a_i up to 1e9, b_i up to 5e8. If all b_i are distinct primes near 5e8, L would be huge product, quickly > M, answer 0.
Now consider also that the condition of equal v2 may be violated often.
Thus solution should pass.
Double-check derivation with some tricky cases:
Case: b_i = [6,10,15]? Actually a_i even => b_i integer. But need to ensure v2 same. Suppose a_i=12,20,30 => b_i=6,10,15. v2(6)=1, v2(10)=1, v2(15)=0 -> differ, answer 0. Manually: X must be odd multiple of 6, odd multiple of 10, odd multiple of 15. But odd multiple of 6 means X=6*(odd) => X even? 6odd = even because 6 even. Actually 6odd is even. Similarly 10odd even. 15odd odd. So X must be even and odd simultaneously -> impossible. So 0.
Now case: b_i = [2,6,18]? a_i=4,12,36 => v2(2)=1, v2(6)=1, v2(18)=1? 18=29 => v2=1. So v=1. c_i: 1,3,9. L_odd = lcm(1,3,9)=9. L=29=18. So X = 18 * odd. Check: X=18 => 18/2=9 odd, 18/6=3 odd, 18/18=1 odd. works. X=54 => 54/2=27 odd, 54/6=9 odd, 54/18=3 odd. works. So formula holds.
Now consider if L might be even but we require quotient odd; but L itself could be even. That's fine.
Now final answer computation: ans = (M//L + 1)//2.
But what about X = 0? Not allowed, positive integer. So start from k=1.
Now test with M < L: answer 0.
Now implement.
But wait: There is subtlety: The condition "There exists a non-negative integer p such that X = a_k * (p+0.5)" means X is exactly a_k*(p+0.5). For a given X, for each k we need existence of some p. Our derivation X = b_k * (2p+1) yields that X must be divisible by b_k and quotient odd. This is correct.
Now consider if b_k are large and L > M, answer 0.
One more nuance: The condition X = a_k * (p+0.5) can also be written as X = (a_k/2)*(2p+1). Since a_k even, a_k/2 integer. So exactly as we did.
Now, the problem might also be interpreted differently: Could p be different for each k? Yes, and we accounted for that.
Thus solution is correct.
Now complexity: O(N log max(a)). Use math.gcd.
Edge Cases to test:
-
N=1, a=2 (b=1). v2(1)=0. L=1. M=10. Q=10, ans = (10+1)//2 = 5. Check manually: X such that X = 2*(p+0.5) => X = 2p+1, odd numbers. Odd numbers ≤10 are 5. Yes.
-
N=1, a=4 (b=2). v2(2)=1. L=2. M=10. Q=5, ans=(5+1)//2=3. X = 4*(p+0.5) => X=4p+2 => numbers ≡2 mod4: 2,6,10. That's 3 numbers. Works.
-
N=2, a=[4,8] => b=[2,4] v2 differ =>0.
-
N=2, a=[4,12] => b=[2,6] v2 both1, L= lcm(2,6)=6, M=20 => Q=3, ans=2. X=6,18? Check: 6 => 6/4=1.5 ok, 6/12=0.5? 6/12=0.5 not integer? Wait condition: For a_k=12, we need X = 12*(p+0.5) => X must be 6,18,30,... Actually 12*(0+0.5)=6, so 6 works; 12*(1+0.5)=18 works. For a_k=4, 6 works? 4*(1+0.5)=6, yes. 18 works? 4*(4+0.5)=18, yes. So indeed 2 numbers: 6,18. 30 >20. So ans=2 matches.
-
N=2, a=[12,20] => b=[6,10] L=30, M=100 => Q=3, ans=2. X=30,90? Check: 30 works, 90 works? 90/12=7.5, 90/20=4.5, yes. 150>100. So ans=2.
Now consider scenario where L is odd? v=0, L is odd. Then X = L * odd => product odd*odd = odd, so all X are odd. But if L is odd and we require X/L odd, then X odd. So okay.
Now code.
But be careful: In Python, division of large numbers is fine.
Now produce final answer.
Now potential pitfalls:
-
We computed b = a_i // 2. But a_i up to 1e9, so b_i up to 5e8. That's fine.
-
For v2 computation, we can use bit_length or while loop dividing by 2. But a_i up to 1e9, at most 30 divisions, fine. Or use (b & -b) trick, but not needed.
-
Compute gcd using math.gcd.
-
During lcm accumulation, we must check overflow only for early break if L > M. In Python, big ints are fine but we can still break to save time.
Now I'll produce code accordingly.
But before final, let's double-check the derivation once more thoroughly with algebra:
Original condition: For each k, ∃ integer p_k ≥ 0 such that X = a_k (p_k + 0.5). Since a_k even, write a_k = 2 b_k. Then X = 2 b_k (p_k + 0.5) = b_k (2 p_k + 1). So X = b_k * odd integer.
Thus X must be a multiple of b_k, and the multiplier (X / b_k) must be odd.
Let’s denote t_k = X / b_k. For each k, t_k must be odd.
Now if we let L = lcm(b_1,...,b_N), then write X = L * q. For each k, we have b_k | L, so L = b_k * d_k. Then t_k = X / b_k = (L * q) / b_k = d_k * q. So t_k odd ⇔ d_k * q odd.
Since d_k = L / b_k, both L and b_k are integers. We want d_k * q odd.
Observe that d_k = L / b_k, and note that v2(L) = max_i v2(b_i). If all b_i have same v2 = v, then each b_k = 2^v * c_k with c_k odd. Then L = 2^v * lcm(c_k). Since each c_k odd, lcm(c_k) odd. So L = 2^v * odd. Then b_k = 2^v * c_k. So d_k = (2^v * lcm(c_k)) / (2^v * c_k) = lcm(c_k)/c_k, which is odd as argued. So d_k odd. Then condition t_k odd reduces to q odd. So indeed X = L * q with q odd.
If there exist b_i, b_j with different v2, then let v_min < v_max. Suppose we try to have X satisfy both. v2(X) must equal v2(b_i) for each, impossible. So no solution.
Thus necessary and sufficient: all v2(b_i) equal, and then X = L * q, q odd.
Thus answer as derived.
Now code:
import sys
import math
def v2(x):
cnt = 0
while x % 2 == 0:
x //= 2
cnt += 1
return cnt
def solve():
input_data = sys.stdin.read().split()
it = iter(input_data)
N = int(next(it))
M = int(next(it))
a = [int(next(it)) for _ in range(N)]
b = [x // 2 for x in a] # since a even
# check v2 equality
v = v2(b[0])
for val in b[1:]:
if v2(val) != v:
print(0)
return
L = 1
for val in b:
g = math.gcd(L, val)
# compute L = L // g * val
# if L exceeds M, we can early exit
# but careful: L // g might be large; multiply by val
# In Python, big ints fine; but we can check early to avoid huge numbers
# However, M <= 1e9, so we can cap L at M+1
temp = L // g * val
if temp > M:
print(0)
return
L = temp
# Now L <= M
Q = M // L
ans = (Q + 1) // 2
print(ans)
if __name__ == "__main__":
solve()
But there is a subtlety: The v2 check for the first element b[0] may have v = something. But if N=0? N≥1 per constraints (1 ≤ N). So fine.
Now consider if L becomes 0? No.
Now test with provided examples:
Example1: Input: 2 50\n6 10 -> b=[3,5]; v2 both0; L: gcd(1,3)=1 => L=3; next gcd(3,5)=1 => L=15 <=50; Q=3; ans=2. OK.
Example2: 3 100\n14 22 40 -> b=[7,11,20]; v2(7)=0, v2(11)=0, v2(20)=2 -> mismatch, output 0. OK.
Example3: 5 1000000000\n6 6 2 6 2 -> b=[3,3,1,3,1]; v2 all0; L: start1->3, then lcm(3,3)=3, then lcm(3,1)=3, then lcm(3,3)=3, then lcm(3,1)=3; L=3; Q=333333333; ans=166666667. OK.
Now edge tests:
-
N=1, M=1, a=[2] -> b=1; v2=0; L=1; Q=1; ans=1. Check: X=2*(p+0.5) => X=1? Actually 2*(0+0.5)=1, yes. So 1 is in [1,1], answer 1. Our formula gives (1+1)//2=1. Good.
-
N=1, M=1, a=[4] -> b=2; v2=1; L=2; Q=0; ans=(0+1)//2=0? Actually (0+1)//2=0? integer division: (0+1)//2 = 0 (since 1//2=0). So answer 0. Check: X=4*(p+0.5) => X=2,6,10,... minimal 2 >1, so answer 0. Good.
-
N=2, M=10, a=[4,12] -> b=[2,6]; v2 both1; L= lcm(2,6)=6; Q=1 (10//6=1); ans=(1+1)//2=1. X=6 only. Check: 6 works, next 18>10. So 1. Good.
-
N=2, M=5, a=[4,12] -> L=6>5 => 0. Good.
Now let's test with random small cases via brute force to be confident.
We'll produce brute for small N, small M, small a (even). Compare formula.
We can test manually: Write a small script mentally? Let's reason a few.
Case: N=2, a=[2,4] -> b=[1,2]. v2(1)=0, v2(2)=1 differ -> 0. Brute: Need X such that X=2*(p+0.5)=2p+1 odd; X=4*(q+0.5)=4q+2 even. So X must be odd and even -> impossible ->0.
Case: N=2, a=[2,6] -> b=[1,3]; v2 both0; L=lcm(1,3)=3; Q=M//3; ans=(Q+1)//2. Check M=10: Q=3, ans=2. Let's list: X must be odd multiple of 1 (so any odd) and odd multiple of 3 (so 3,9,15,...). Intersection: odd numbers that are multiples of 3: 3,9,15,... within 10: 3,9 =>2. Yes.
Case: N=2, a=[6,10] we did.
Case: N=2, a=[4,6] -> b=[2,3]; v2: 2->1, 3->0 differ -> 0. Check: X must be 4p+2 (≡2 mod4) and 6q+3 (≡3 mod6? Actually 6q+3 ≡ 3 mod6, also odd). Can there be X? Let's try small: X=2? 2=40+2 ok; 2=6? 60+3=3, no. X=6? 6=41+2 yes? 41+2=6 yes; 6=60.5? Actually 6=6*(0+0.5)=3? no; condition for a=6: 6*(q+0.5)=6q+3, so 6q+3=6 => q=0.5 no. X=10? 10=42+2 ok; 10=61+4? Actually 61+3=9, 61.5? no. X=14? 43+2=14; 6? 62+3=15; 6? no. X=18? 44+2=18; 62+3=15; 6*2.5? no. So no solution. So 0.
Case: N=2, a=[8,12] -> b=[4,6]; v2: 4->2, 6->1 differ -> 0.
Thus correct.
Now another subtlety: The condition that p is non-negative integer implies that X ≥ a_k/2. But does our condition X = b_k * odd enforce that? For p≥0, we have X = a_k*(p+0.5) ≥ a_k/2. Since b_k = a_k/2, X = b_k * odd ≥ b_k * 1. So X ≥ b_k. That's automatically satisfied if X is positive odd multiple; the smallest odd multiple is b_k*1, which is exactly a_k/2. So no extra constraint beyond positivity. But could there be a case where X is a multiple of b_k with odd quotient but X < b_k? Since odd quotient must be at least 1, X ≥ b_k. So fine.
Now ensure we haven't missed anything like X must be integer, we handled.
Now complexity: O(N log max(a)), fine.
Now we should be careful about the lcm computation when L becomes > M: we break and output 0. However, what if L becomes > M but due to intermediate step, we might get L exactly > M, but the final L might be larger; but anyway answer 0.
But consider scenario: Suppose L computed as product/gcd might overflow Python's big int? Python can handle arbitrarily large ints, but performance might degrade if numbers become huge. But since we break when L > M (which is at most 1e9), we cap the size to around 1e9, so numbers remain small. However, it's possible that L becomes > M in the middle of loop, we break and exit. So we avoid huge numbers. So efficient.
One nuance: When we compute L = L // g * val, if L // g is large but still product might exceed M. But since we check after multiplication, we might compute product as big integer if val is large and L // g is large; but if L is already > M we would have broken earlier. So the only time we compute a product that could be large is when L <= M and val might be large but product may jump from <=M to > M; but that product might be as big as L * val (since dividing by g reduces but not too much). The worst-case product could be as high as M * (max b) maybe up to 1e9 * 5e8 = 5e17. That's still okay for Python. But if we continue iterating many times, L may become huge after many numbers, but we break early. So overall fine.
Now also note: There's possibility that v2 values are same, but L may be computed as something that exceeds 64-bit but still <= M? M <= 1e9, so L <= 1e9. So L will be at most 1e9. So it's small. So we won't have huge numbers anyway. Actually careful: L can be up to M, which is at most 1e9. So indeed L will never exceed 1e9 in successful cases. So numbers remain within 32-bit. So no issue.
Thus the algorithm is efficient and safe.
Now I'll produce final answer.
But wait: There's an alternative approach using the condition that X = a_k/2 * (odd) which leads to X must be multiple of L = lcm(a_i/2) and quotient odd, provided all a_i/2 have same v2. So we've got it.
Now I think the solution is complete.
But I'll also check if there's any off-by-one: For Q = floor(M / L), number of odd k from 1 to Q inclusive is (Q+1)//2. This counts k=1,3,5,... up to Q if Q odd, or up to Q-1 if Q even. That's correct.
Now test with Q=0: (0+1)//2 = 0. So answer 0.
Now test with M=5, L=6 -> Q=0 -> ans=0. good.
Now consider M=6, L=6 -> Q=1 -> ans=1. X=6 works if conditions satisfied.
Now consider M=7, L=6 -> Q=1 -> ans=1 (X=6). So not counting 12 because >7.
Now all good.
Now I'll produce final code in Python.
But before finalize, ensure we import math.
One more nuance: In the v2 check, we compute v2(b[0]) using while loop. But if N is large (1e5) and many b_i are large, the while loop for each b_i could be up to 30 iterations, total 3e6 operations, fine.
Alternatively, we could compute v2 using bit operations: (b & -b) to get lowest set bit, but not needed.
Potential optimization: Compute v2 for first b, then for others check if (val >> v) is odd? Not exactly. Could check if val % (2^v) == 0 and val // (2^v) odd? Actually condition for equal v2: v2(val) == v. So we can compute v2 of each val; we can early exit if different.
Implement:
def v2(x):
return (x & -x).bit_length() - 1 # or use x.bit_length etc.
But I'll keep simple while loop.
Now, code.
One more thought: The problem statement says "semi-common multiple" defined as X that satisfies for every k, there exists non-negative integer p such that X = a_k * (p + 0.5). That is equivalent to X / a_k = p + 0.5. So X must be a half-integer multiple of each a_k. The analysis above correct.
Now, consider if any a_k = 0? No, positive even.
Now final answer.
Now I'll write final solution with explanation.
But I need to produce the answer as code.
One more thing: The input format: first line N M, second line a_1 ... a_N. So read accordingly.
Now final.
But before final output, I'll add docstring maybe.
Ok.
Now produce final answer.
However, there is a twist: The problem is from AtCoder ABC (maybe). I recall a problem "Semi Common Multiple" from AtCoder ABC 150 D? Actually there is a problem named "Semi Common Multiple" in AtCoder, and solution is as above. I think I've seen it. So it's fine.
One more nuance: The condition "non-negative integer p" means p can be 0, so X can be a_k/2. That's allowed. So our formula includes that as k=1 case (odd quotient 1). Good.
Now all set.
Now finalize. The problem requires counting positive integers (X \le M) such that for each even number (a_k) there exists a non‑negative integer (p) with (X = a_k(p+0.5)).
Let (b_k = a_k/2) (integer because (a_k) is even). The condition becomes (X = b_k \cdot (2p+1)), i.e. (X) is a multiple of (b_k) and the quotient (X/b_k) is odd.
Thus for all (k) we need:
- (b_k \mid X),
- (X/b_k) is odd.
Write each (b_k = 2^{v} \cdot c_k) where (c_k) is odd. For the oddness condition to be simultaneously possible, all (v_2(b_k)) must be equal; otherwise no (X) can satisfy both. If they are equal, let (v) be that common exponent.
Then the condition reduces to:
- (X = 2^{v} \cdot y) with (y) odd,
- (c_k \mid y) for every (k), i.e. (y) is a multiple of (L_{\text{odd}} = \operatorname{lcm}(c_1,\dots,c_N)).
Hence (X = 2^{v} \cdot L_{\text{odd}} \cdot t) with (t) odd. But (2^{v} \cdot L_{\text{odd}}) is exactly the least common multiple (L = \operatorname{lcm}(b_1,\dots,b_N)). Therefore the semi‑common multiples are precisely the numbers (X = L \cdot q) where (q) is an odd positive integer.
Consequently, the answer is:
- (0) if the (v_2(b_k)) are not all equal, or if (L > M);
- otherwise, let (Q = \lfloor M/L \rfloor); the number of odd integers (q) with (1 \le q \le Q) is (\lceil Q/2 \rceil = (Q+1)//2).
The algorithm follows directly: compute (b_i = a_i/2), check equal (v_2), compute (L) while capping at (M), and finally compute the count. The time complexity is (O(N \log \max a_i)).
import sys
import math
def v2(x: int) -> int:
"""Exponent of 2 in x."""
cnt = 0
while x % 2 == 0:
x //= 2
cnt += 1
return cnt
def solve() -> None:
it = iter(sys.stdin.read().split())
N = int(next(it))
M = int(next(it))
a = [int(next(it)) for _ in range(N)]
b = [x // 2 for x in a] # all a_i are even
# all b_i must have the same exponent of 2
v = v2(b[0])
for val in b[1:]:
if v2(val) != v:
print(0)
return
# compute L = lcm(b_i), but stop if it exceeds M
L = 1
for val in b:
g = math.gcd(L, val)
L = L // g * val
if L > M:
print(0)
return
Q = M // L
ans = (Q + 1) // 2
print(ans)
if __name__ == "__main__":
solve()