KonkurStudy
همه کارت‌ها

Inclusion-Exclusion and Coprime Counting

Size of a union of nn sets is the alternating sum over all intersections; sieving by the prime divisors of mm counts integers coprime to mm.

کارت فرمول کنکور ارشد علوم کامپیوتر

اهمیت
۰/۵

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∣|A\cup B| = |A|+|B|-|A\cap 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∣.|A\cup B\cup C| = |A|+|B|+|C| - |A\cap B| - |A\cap C| - |B\cap C| + |A\cap B\cap C|.

In general, for nn sets A1,…,AnA_1,\dots,A_n: ∣⋃i=1nAi∣=∑∅≠S⊆{1,…,n}(−1)∣S∣+1∣⋂i∈SAi∣.\left|\bigcup_{i=1}^{n} A_i\right| = \sum_{\varnothing \ne S \subseteq \{1,\dots,n\}} (-1)^{|S|+1}\left|\bigcap_{i\in S} A_i\right|.

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∣−⋯ ,\left|\overline{A_1}\cap\cdots\cap\overline{A_n}\right| = N - \sum |A_i| + \sum |A_i\cap A_j| - \cdots, where NN 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 mm

To count integers in {1,2,…,N}\{1,2,\dots,N\} that are coprime to mm, factor mm into its distinct primes p1,…,pkp_1,\dots,p_k. An integer is coprime to mm iff it is divisible by none of these primes, so let AiA_i be the multiples of pip_i. The count of multiples of a product is ∣Ai∩Aj∣=⌊N/(pipj)⌋|A_i\cap A_j| = \lfloor N/(p_i p_j)\rfloor, and inclusion-exclusion gives the complement directly: #{k≤N:gcd⁡(k,m)=1}=N−∑i⌊Npi⌋+∑i<j⌊Npipj⌋−⋯ .\#\{k\le N : \gcd(k,m)=1\} = N - \sum_i \Big\lfloor\tfrac{N}{p_i}\Big\rfloor + \sum_{i<j} \Big\lfloor\tfrac{N}{p_i p_j}\Big\rfloor - \cdots.

When N=mN=m itself the floors are exact and this collapses to Euler's totient: φ(m)=m∏p∣m(1−1p).\varphi(m) = m\prod_{p\mid m}\left(1-\tfrac{1}{p}\right).

Quantity Formula When to use
Coprime to mm in [1,N][1,N] sieve with floors ⌊N/d⌋\lfloor N/d\rfloor N≠mN \ne m (general range)
Coprime to mm in [1,m][1,m] φ(m)=m∏p∣m(1−1p)\varphi(m)=m\prod_{p\mid m}(1-\tfrac1p) range is exactly [1,m][1,m]

Mini-example

How many of {1,…,100}\{1,\dots,100\} are coprime to 4545? Since 45=32⋅545 = 3^2\cdot 5, only the primes 33 and 55 matter (the exponent is irrelevant for coprimality). Count multiples of 33 or 55 and subtract from 100100: 100−(⌊1003⌋+⌊1005⌋−⌊10015⌋)=100−(33+20−6)=100−47=53.100 - \left(\Big\lfloor\tfrac{100}{3}\Big\rfloor + \Big\lfloor\tfrac{100}{5}\Big\rfloor - \Big\lfloor\tfrac{100}{15}\Big\rfloor\right) = 100 - (33+20-6) = 100 - 47 = 53.

The same alternating-sum idea also counts strings avoiding patterns: for 1010-digit strings over {1,2,3}\{1,2,3\} where the left 55 avoid a 11 or the right 55 avoid a 22, take ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣=2⋅2535−210=26(243−16)=26⋅227|A\cup B| = |A|+|B|-|A\cap B| = 2\cdot 2^5 3^5 - 2^{10} = 2^6(243-16) = 2^6\cdot 227.

Exam trick: For coprimality only the distinct prime factors of mm matter — drop the exponents (so 32⋅53^2\cdot 5 behaves like 3⋅53\cdot 5). And remember the sign rule: add odd-sized intersections, subtract even-sized ones. Forgetting the final +∣A∩B∣+|A\cap B| correction is the classic trap that leaves the overlap double-subtracted or double-counted.

فرمول‌های کلیدی

Union of n sets
∣⋃i=1nAi∣=∑∅≠S⊆{1,…,n}(−1)∣S∣+1∣⋂i∈SAi∣\left|\bigcup_{i=1}^{n} A_i\right| = \sum_{\varnothing \ne S \subseteq \{1,\dots,n\}} (-1)^{|S|+1}\left|\bigcap_{i\in S} A_i\right|

Alternating sum over all non-empty intersections.

Coprime count by sieve
#{k≤N:gcd⁡(k,m)=1}=N−∑i⌊Npi⌋+∑i<j⌊Npipj⌋−⋯\#\{k\le N : \gcd(k,m)=1\} = N - \sum_i \left\lfloor\tfrac{N}{p_i}\right\rfloor + \sum_{i<j} \left\lfloor\tfrac{N}{p_i p_j}\right\rfloor - \cdots

Sieve over the distinct primes p_i of m.

Euler's totient
φ(m)=m∏p∣m(1−1p)\varphi(m) = m\prod_{p\mid m}\left(1-\tfrac{1}{p}\right)

Coprime count over the full range [1,m].

#inclusion-exclusion#coprime#euler-totient#counting#number-theory#combinatorics#divisibility

سوالات تمرینی