Enumerate overlapping conditions
Use subset parity and LCM to count a union of divisibility conditions.
Understand the idea
General inclusion-exclusion adds intersections of odd numbers of sets and subtracts intersections of even numbers. A nonempty mask selects one intersection. For numbers divisible by every selected divisor, use their LCM; the intersection count in [1, N] is floor(N/LCM).
Trace a small example
Up to 20, multiples of 2 number 10 and multiples of 3 number 6. Their overlap is multiples of 6, numbering 3. The union therefore has 10 + 6 − 3 = 13. Divisors 2 and 4 have intersection multiples of 4, not multiples of 8.
Conditions and cost
LCM, not an unproved product, handles shared prime factors. Cap the LCM at N before multiplying; if it exceeds N, that intersection contributes zero. With k conditions, the straightforward method visits 2^k − 1 masks and needs small k.
Numbers divisible by both 4 and 6 are exactly the multiples of which value?
Turn the idea into code
Count numbers from 1 through 30 divisible by 3 or 5.
Give me a hint
Subtract the overlap at multiples of 15.
Show the explained solution
floor(30/3) + floor(30/5) − floor(30/15) = 10 + 6 − 2 = 14.
Your notes
Mark complete when you can explain it yourself.