Size of a union of n sets is the alternating sum over all intersections; sieving by the prime divisors of m counts integers coprime to m.
کارت فرمول کنکور ارشد علوم کامپیوتر
اهمیت
۰/۵
The general principle
To count a union you add the sizes of the sets, then correct for over-counting by alternately subtracting and adding the intersections. The two-set case ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣ (see the related card) is just the first step of a pattern that continues for any number of sets.
For three sets:
∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣.
In general, for n sets A1,…,An:
∣⋃i=1nAi∣=∑∅=S⊆{1,…,n}(−1)∣S∣+1⋂i∈SAi.
The sign alternates with the size of the intersection: single sets are added, pairwise overlaps subtracted, triple overlaps added, and so on. Equivalently, the count of elements in none of the sets is
A1∩⋯∩An=N−∑∣Ai∣+∑∣Ai∩Aj∣−⋯,
where N is the size of the universe. This complement form is the workhorse for counting things that avoid a list of forbidden properties.
Application: counting integers coprime to m
To count integers in {1,2,…,N} that are coprime to m, factor m into its distinct primesp1,…,pk. An integer is coprime to m iff it is divisible by none of these primes, so let Ai be the multiples of pi. The count of multiples of a product is ∣Ai∩Aj∣=⌊N/(pipj)⌋, and inclusion-exclusion gives the complement directly:
#{k≤N:gcd(k,m)=1}=N−∑i⌊piN⌋+∑i<j⌊pipjN⌋−⋯.
When N=m itself the floors are exact and this collapses to Euler's totient:
φ(m)=m∏p∣m(1−p1).
Quantity
Formula
When to use
Coprime to m in [1,N]
sieve with floors ⌊N/d⌋
N=m (general range)
Coprime to m in [1,m]
φ(m)=m∏p∣m(1−p1)
range is exactly [1,m]
Mini-example
How many of {1,…,100} are coprime to 45? Since 45=32⋅5, only the primes3 and 5 matter (the exponent is irrelevant for coprimality). Count multiples of 3 or 5 and subtract from 100:
100−(⌊3100⌋+⌊5100⌋−⌊15100⌋)=100−(33+20−6)=100−47=53.
The same alternating-sum idea also counts strings avoiding patterns: for 10-digit strings over {1,2,3} where the left 5 avoid a 1or the right 5 avoid a 2, take ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣=2⋅2535−210=26(243−16)=26⋅227.
Exam trick: For coprimality only the distinct prime factors of m matter — drop the exponents (so 32⋅5 behaves like 3⋅5). And remember the sign rule: add odd-sized intersections, subtract even-sized ones. Forgetting the final +∣A∩B∣ correction is the classic trap that leaves the overlap double-subtracted or double-counted.