We need to solve the problem: Given N, A, B (positive integers, A<B<N), find sum of all positive integers less than N that are divisible by either A or B.
This is classic inclusion-exclusion: sum of multiples of A below N plus sum of multiples of B below N minus sum of multiples of LCM(A,B) below N.
Let kA = floor((N-1)/A), sumA = A * (kA*(kA+1)/2). Similarly for B. Let L = lcm(A,B). But careful: if we compute multiples of both, we need to subtract sum of multiples of L. However, if A and B are not necessarily coprime, LCM works.
Since N up to 50000, we could also iterate but better O(1). But need to compute LCM: L = A*B / gcd(A,B). Since N is small, could compute with int.
We need to output the sum. Ensure it fits? N up to 50000, max sum for multiples of 2: sum of numbers up to 49999 approx 1.25e9, within 32-bit signed? Actually sum up to 50000 is about 1.25e9, so sum of two such may be about 2.5e9, still within 32-bit signed max (2.147e9) might overflow? Wait 2.5e9 > 2.147e9, but is it possible? Let's estimate worst-case: A=2, B=3, N=50000. Multiples of 2: sum = 2+4+...+49998. Number of terms = floor(49999/2)=24999. Sum = 2 * (2499925000/2)=2 * (2499912500)=? Actually better: sum of multiples of 2 = 2 * n*(n+1)/2 where n= floor((N-1)/A)= floor(49999/2)=24999. So sum = A * n*(n+1)/2 = 2 * 2499925000/2 = 2 * 2499912500? Actually 2 * (n*(n+1)/2) = n*(n+1). That's if A=1? Wait formula: sum_{i=1}^{k} (Ai) = A * k(k+1)/2. So for A=2, k=24999: sum = 2 * 2499925000/2 = 2 * (2499912500) = 2 * 312487500 = 624975000? Let's compute: 2499912500 = 2499912500 = (25000-1)12500 = 312500000 - 12500 = 312487500. Times 2 = 624975000. For B=3: k = floor(49999/3)=16666 (since 316666=49998). sum = 3 * 1666616667/2. Compute 1666616667 ≈? 1666616667 = 16666(16666+1)=16666^2+16666. 16666^2 = (1666616666) =? 16666^2 = (16667-1)^2 = 16667^2 - 216667 +1 = (277777? actually compute precisely later). But approximate: 1666616667 ~ 277,777? Actually 1666616666=277,775,556? That seems off. Let's compute: 16000^2=256,000,000; 666^2=443,556; cross 216000666=21,312,000; sum=277,755,556? hmm. Actually 16666^2 = (16666)(16666) = maybe 277,755,556? Let's compute: 16666 * 16667 = 1666616666 + 16666 = 277,777,? Let's do simpler: 16666 * 16667 = (1666616666)+16666. 1666616666 = (16667-1)(16667-1) = 16667^2 -216667 +1. 16667^2 = 277,777,? Actually 16667^2 = (1666716667) =? 16667 * 16667 = (1666716000)=266,672,000; plus 16667667=16667600=10,000,200; 1666767=1,116,? Let's do systematic: 16667^2 = (16667)(16667). Could compute 1666716667 = (1666716667) using (1666716667) = (1666710000)=166,670,000; 6000=100,002,000; 600=10,000,200; 60=1,000,020; 7=116,669; sum = 166,670,000+100,002,000=266,672,000; +10,000,200=276,672,200? Actually I'm messing. Better compute using formula: (16667)^2 = (16667^2). Actually we can compute later. But sum of multiples of 3 approx: average around (316666)/2? Not necessary. Let's approximate: The sum of numbers from 1 to M is M(M+1)/2. Multiples of 3: 3(1+2+...+k)=3k(k+1)/2, where k=16666. So sum = 31666616667/2. 1666616667 ≈ 277,777? Actually 1666616667 = 16666*(16666+1)=16666^2+16666. 16666^2 = (16666)(16666) = maybe 277,755,556? Let's compute: 1666616666 =? 16666 * 10000 = 166,660,000; 6000=99,996,000; 600=9,999,600; 60=999,960; 6=99,996; Sum = 166,660,000+99,996,000=266,656,000; +9,999,600=276,655,600; +999,960=277,655,560; +99,996=277,755,556. Yes 16666^2 = 277,755,556. Then add 16666 gives 277,772,222? Actually 277,755,556+16,666=277,772,222. So product = 277,772,222. Then multiply by 3 gives 833,316,666; divide by 2 gives 416,658,333. So sumB ≈ 416,658,333. SumA = 624,975,000. Sum of both = ~1,041,633,333. LCM of 2 and 3 is 6. Multiples of 6: k6 = floor(49999/6)=8333 (since 68333=49998). Sum6 = 683338334/2 = 683334167 =? 83334167 ~? 83334000=33,332,000; 8333167=1,391,? 8333100=833,300; 833360=499,980; 83337=58,331; sum = 833,300+499,980=1,333,280; +58,331=1,391,611; total =33,332,000+1,391,611=34,723,611. Times 6 = 208,341,666? Actually sum6 = 6 * (83338334/2) = 683334167 = 634,723,? we computed 83334167 =? Let's compute accurately: 83334167 = 8333*(4000+167)=83334000=33,332,000; 8333167 = 8333100=833,300; 833360=499,980; 8333*7=58,331; sum=833,300+499,980=1,333,280; +58,331=1,391,611; so total = 33,332,000+1,391,611 = 34,723,611. Multiply by 6 = 208,341,666. So final sum = sumA+sumB - sum6 = 624,975,000 + 416,658,333 - 208,341,666 = 833,291,667? Actually 624,975,000+416,658,333=1,041,633,333; minus 208,341,666 = 833,291,667. That's well below 2^31-1 (2,147,483,647). So 32-bit int is fine. Even if we take A=1? But constraint A>=2, so sum cannot exceed 1e9. So 32-bit int safe. However languages like C++ might use long long for safety.
Implementation: Read N, A, B. Compute kA = (N-1)/A, kB = (N-1)/B, lcm = A / gcd(A,B) * B (be careful with overflow? A and B up to < N <= 50000, product up to 2.5e9, fits 32-bit signed int? 5000050000=2.5e9 > 2.147e9, but since A and B < N <= 50000, AB could be up to 4999949998 ≈ 2.5e9, which might exceed 32-bit signed int (max 2,147,483,647). But we are dividing by gcd, so product/gcd could be smaller? Actually worst-case LCM might be large when gcd is 1, then lcm = AB. For A=30000, B=40000, product = 1.2e9, still <2.147e9? 3000040000 = 1,200,000,000 (1.2e9), fine. But 4000045000 = 1,800,000,000 (1.8e9) < 2.147e9; 4999949998 = 2,499,?? Compute: 5000050000=2.5e9, minus 50000-? Actually 4999949998 = (50000-1)(50000-2)=5000050000 -500002 -150000 +2 = 2,500,000,000 -100,000 -50,000 +2 = 2,499,850,002? That's 2.5e9 minus ~150k, so 2,499,850,002, which is > 2,147,483,647. So product can exceed 2^31-1. So if using 32-bit signed int, multiplication might overflow. But we can compute lcm using 64-bit (long long) or use formula: lcm = A / gcd(A,B) * B; but intermediate multiplication (A/gcd)B could still be up to about 2.5e9? Actually (A/gcd) * B: since A and B <= 49999, and gcd >= 1, dividing reduces one factor. The worst-case for lcm when gcd=1, lcm = AB which may exceed 2^31-1 as shown. However we only need lcm to compute kL = (N-1)/lcm, and then sumL = lcm * kL(kL+1)/2. Since N <= 50000, lcm could be > N, in which case kL = 0, so we don't actually need lcm value if it's > N-1? Actually to compute kL we need to compare lcm to N; if lcm >= N, then kL = 0. But we need lcm to compute kL = floor((N-1)/lcm). If lcm > N-1, then floor = 0. So we can avoid overflow by checking: if A/gcd > (N-1)/B? Because lcm = (A/gcd)*B. We can check if (A/gcd) * B > N-1, then kL = 0. Since N-1 <= 49999, we can compute safely using 64-bit or check overflow conditions.
Simpler: use 64-bit integers (long long) in C++ for safety. In Python, int is arbitrary. So we can just compute lcm = A * B // gcd(A,B) as Python int.
Thus solution is straightforward.
Edge Cases: A and B could have common factors, L = lcm. Also note we consider numbers less than N (i.e., up to N-1). So floor((N-1)/x). Also note positive integers, so start from multiples of A: A,2A,...; similarly B.
Make sure sum formula uses integer arithmetic.
Implementation steps:
- Read input line, parse into integers N, A, B.
- Compute function sum_multiples(x):
- k = (N-1)//x
- return x * k * (k+1) // 2
- Compute g = gcd(A, B)
- lcm = A // g * B (to avoid overflow in Python fine; but use integer division)
- sumL = sum_multiples(lcm) if lcm < N else 0
- total = sumA + sumB - sumL
- Print total.
Test with sample: N=20,A=4,B=7. sumA: multiples of 4: 4,8,12,16 => sum=40. kA=(19)//4=4 => 445/2=410=40. sumB: multiples of 7: 7,14 => sum=21. kB=19//7=2 => 723/2=73=21. lcm=28 (since gcd=1? Actually 4 and7 gcd=1, lcm=28). sumL: multiples of 28 below20? None, kL=19//28=0 =>0. total=61. Works.
Complexities O(log(min(A,B))) for gcd.
Now writing final answer: Provide explanation and code.
Possible pitfalls: When computing lcm, if lcm may exceed N but we still compute sum_multiples, k will be 0 so sum 0, fine. But if lcm is very large, multiplication might overflow in languages with fixed-width ints. So best to use long long.
Now, as per requirement: output the required sum.
Now produce final response with explanation and code (likely in Python or C++). Since not specified language, I can provide both? Usually they expect code. Let's provide Python code as it's simple and fits.
We'll also include in solution description.
Thus final answer. ```python
import sys
import math
def sum_of_multiples(limit, x):
"""Return sum of all positive multiples of x less than limit."""
k = (limit - 1) // x
return x * k * (k + 1) // 2
def main():
data = sys.stdin.read().strip().split()
if not data:
return
N, A, B = map(int, data)
total = sum_of_multiples(N, A) + sum_of_multiples(N, B)
g = math.gcd(A, B)
lcm = A // g * B
if lcm < N:
total -= sum_of_multiples(N, lcm)
print(total)
if name == "main":
main()