Given three positive integers N, A and B (A < B < N), find the sum of all positive…
- User
Given three positive integers N, A and B (A < B < N), find the sum of all positive integers less than N, which are divisible by either A or B.
For example, when N = 20, A = 4 and B = 7, the possible values are 4, 7, 8, 12, 14, and 16. Their sum is 61. Input Format The only line of the input file contains three space separated integers N, A and B.
Output Format Output the required sum.
Constraints 10 ≤ N ≤ 50000 2 ≤ A < B < N
SAMPLE INPUT 20 4 7
SAMPLE OUTPUT 61
Assistant
pythonimport math # Read input values N, A, B = map(int, input().split()) # Function to calculate the sum of multiples of a number up to N-1 def sum_multiples(num, limit): count = (limit - 1) // num return num * count * (count + 1) // 2 sum_a = sum_multiples(A, N) sum_b = sum_multiples(B, N) # Calculate LCM of A and B gcd = math.gcd(A, B) lcm = A * B // gcd sum_lcm = sum_multiples(lcm, N) # Calculate the final result result = sum_a + sum_b - sum_lcm print(result)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.