Inclusion–Exclusion Principle

Introduction

Introductory Example

Suppose there are 1010 students in a class who like mathematics, 1515 who like Chinese, and 2121 who like programming. How many students like at least one of these subjects?

Is it 10+15+21=4610+15+21=46 students? No, because some students may like both mathematics and Chinese, or Chinese and programming, and some may even like all three.

For convenience, let the sets of students who like Chinese, mathematics, and programming be A,B,CA,B,C, respectively. Then the total number of students is ∣A∪B∪C∣|A\cup B\cup C|. As noted above, directly adding the cardinalities ∣A∣,∣B∣,∣C∣|A|,|B|,|C| of these three sets counts some elements more than once, so we must subtract ∣A∩B∣,∣B∩C∣,∣C∩A∣|A\cap B|,|B\cap C|,|C\cap A|. This subtracts a small part too many times, however, so we add it back as ∣A∩B∩C∣|A\cap B\cap C|. Thus

∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣B∩C∣−∣C∩A∣+∣A∩B∩C∣|A\cup B\cup C|=|A|+|B|+|C|-|A\cap B|-|B\cap C|-|C\cap A|+|A\cap B\cap C|
Inclusion–exclusion principle — Venn diagram example
Inclusion–exclusion principle — Venn diagram example

This generalization of the problem is the familiar inclusion–exclusion principle.

Definition

Suppose the elements of U have n different properties, and call the i-th property PiP_i. The elements having property PiP_i form the set SiS_i. Then

∣⋃i=1nSi∣=∑i∣Si∣−∑i<j∣Si∩Sj∣+∑i<j<k∣Si∩Sj∩Sk∣−⋯+(−1)m−1∑ai<ai+1∣⋂i=1mSai∣+⋯+(−1)n−1∣S1∩⋯∩Sn∣\begin{aligned} \left|\bigcup_{i=1}^{n}S_i\right|=&\sum_{i}|S_i|-\sum_{i<j}|S_i\cap S_j|+\sum_{i<j<k}|S_i\cap S_j\cap S_k|-\cdots\\ &+(-1)^{m-1}\sum_{a_i<a_{i+1} }\left|\bigcap_{i=1}^{m}S_{a_i}\right|+\cdots+(-1)^{n-1}|S_1\cap\cdots\cap S_n| \end{aligned}

That is,

∣⋃i=1nSi∣=∑m=1n(−1)m−1∑ai<ai+1∣⋂i=1mSai∣\left|\bigcup_{i=1}^{n}S_i\right|=\sum_{m=1}^n(-1)^{m-1}\sum_{a_i<a_{i+1} }\left|\bigcap_{i=1}^mS_{a_i}\right|

Proof

For each element, use the binomial theorem to calculate how many times it appears. For an element x, suppose it belongs to the sets T1,T2,⋯ ,TmT_1,T_2,\cdots,T_m. Its number of appearances is

Cnt=∣{Ti}∣−∣{Ti∩Tj∣i<j}∣+⋯+(−1)k−1∣{⋂i=1kTai∣ai<ai+1}∣+⋯+(−1)m−1∣{T1∩⋯∩Tm}∣=(m1)−(m2)+⋯+(−1)m−1(mm)=(m0)−∑i=0m(−1)i(mi)=1−(1−1)m=1\begin{aligned} Cnt=&|\{T_i\}|-|\{T_i\cap T_j|i<j\}|+\cdots+(-1)^{k-1}\left|\left\{\bigcap_{i=1}^{k}T_{a_i}|a_i<a_{i+1}\right\}\right|\\ &+\cdots+(-1)^{m-1}|\{T_1\cap\cdots\cap T_m\}|\\ =&\dbinom{m}{1}-\dbinom{m}{2}+\cdots+(-1)^{m-1}\dbinom{m}{m}\\ =&\dbinom{m}{0}-\sum_{i=0}^m(-1)^i\dbinom{m}{i}\\ =&1-(1-1)^m=1 \end{aligned}

Thus each element appears exactly once, and combining all elements gives the union. QED.

Complements

For the union of sets in a universal set U, we can use inclusion–exclusion. The intersection of sets can instead be obtained by subtracting the union of the complements from the universal set:

∣⋂i=1nSi∣=∣U∣−∣⋃i=1nSi‾∣\left|\bigcap_{i=1}^{n}S_i\right|=|U|-\left|\bigcup_{i=1}^n\overline{S_i}\right|

The right-hand side can be expanded by inclusion–exclusion.

Readers who have encountered inclusion–exclusion are familiar with the material above; what matters more are its applications.

We next give three examples at different levels to demonstrate applications of the inclusion–exclusion principle.

Counting Nonnegative Integer Solutions of an Indeterminate Equation

Counting Nonnegative Integer Solutions of an Indeterminate Equation

Given the equation ∑i=1nxi=m\sum_{i=1}^nx_i=m and nn constraints xi≤bix_i\leq b_i, where m,bi∈Nm,b_i \in \mathbb{N}. Find the number of nonnegative integer solutions.

Without Constraints

If there is no constraint xi≤bix_i\leq b_i, the number of nonnegative integer solutions to the equation ∑i=1nxi=m\sum_{i=1}^nx_i=m is (m+n−1n−1)\dbinom{m+n-1}{n-1}.

A brief proof uses the stars-and-bars method.

Equivalently, distribute mm balls among nn boxes, allowing a box to be empty. This cannot be solved directly with a binomial coefficient.

Add n−1n-1 more balls. The problem becomes choosing balls from a sequence of length m+n−1m+n-1; specifically, choose n−1n-1 balls. These n−1n-1 balls divide the sequence into nn parts, which correspond one-to-one to distributions into nn boxes. Thus, the number of ways to choose from m+n−1m+n-1 balls, selecting n−1n-1, is (m+n−1n−1)\dbinom{m+n-1}{n-1}.

Inclusion–Exclusion Model

Let us abstract the model of inclusion–exclusion:

  1. Universal set U: the nonnegative integer solutions of ∑i=1nxi=m\sum_{i=1}^nx_i=m
  2. Elements: the variables xix_i.
  3. Property: the property of xix_i is the condition satisfied by xix_i, namely the condition xi≤bix_i\leq b_i.

The goal is the size of the set in which all variables satisfy their corresponding properties, namely ∣⋂i=1nSi∣|\bigcap_{i=1}^nS_i|.

This can be computed using ∣⋂i=1nSi∣=∣U∣−∣⋃i=1nSi‾∣\left|\bigcap_{i=1}^{n}S_i\right|=|U|-\left|\bigcup_{i=1}^n\overline{S_i}\right|. The quantity ∣U∣|U| can be computed with a binomial coefficient, while the latter part is naturally expanded by inclusion–exclusion.

The problem therefore becomes finding the size of an intersection of some Sai‾\overline{S_{a_i}}. Consider the meaning of Sai‾\overline{S_{a_i} }: it counts solutions satisfying xai≥bai+1x_{a_i}\geq b_{a_i}+1. Their intersection means satisfying all these conditions simultaneously. Thus, in the corresponding indeterminate equation, some variables have lower-bound constraints, while others have none.

Can these lower-bound constraints be removed? We seek nonnegative integer solutions, and some variables have a lower bound greater than 00. We can simply subtract this lower bound, making the lower bound 00, i.e. removing it. Therefore, for

∣⋂ai<ai+11≤i≤kSai∣\left|\bigcap_{a_i<a_{i+1} }^{1\leq i\leq k}S_{a_i}\right|

the indeterminate equation has the form

∑i=1nxi=m−∑i=1k(bai+1)\sum_{i=1}^nx_i=m-\sum_{i=1}^k(b_{a_i}+1)

This can also be evaluated with a binomial coefficient. The length-kk aa array is effectively enumerating subsets.

HAOI2008 Coin Shopping

HAOI2008 Coin Shopping

There are four coin denominations; the denomination of the i-th type is CiC_i. There are nn queries. Each query gives the quantity DiD_i of each coin type and a price SS, and asks for the number of payment methods.

n≤103,S≤105n\leq 10^3,S\leq 10^5.

A knapsack solution would have complexity O(4nS)O(4nS), which is too large. The most notable feature is that there are only four coin types. Abstractly, we need the number of nonnegative integer solutions to ∑i=14Cixi=S,xi≤Di\sum_{i=1}^4C_ix_i=S,x_i\leq D_i.

Using the same inclusion–exclusion method, the property of xix_i is xi≤Dix_i\leq D_i. Applying the inclusion–exclusion formula, we ultimately need to solve

∑i=14Cixi=S−∑i=1kCai(Dai+1)\sum_{i=1}^4C_ix_i=S-\sum_{i=1}^kC_{a_i}(D_{a_i}+1)

which is an unbounded knapsack problem. It can be precomputed; including the queries, the total complexity is O(4S+24n)O(4S+2^4n).

Implementation
#include <iostream>
using namespace std;
constexpr long long S = 1e5 + 5;
long long c[5], d[5], n, s;
long long f[S];

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cin >> c[1] >> c[2] >> c[3] >> c[4] >> n;
  f[0] = 1;
  for (long long j = 1; j <= 4; j++)
    for (long long i = 1; i < S; i++)
      if (i >= c[j]) f[i] += f[i - c[j]];  // f[i]: number of ways to form price i with the coins
  for (long long k = 1; k <= n; k++) {
    cin >> d[1] >> d[2] >> d[3] >> d[4] >> s;
    long long ans = 0;
    for (long long i = 1; i < 16;
         i++) {  // Inclusion-exclusion: there are four item types, so iterate from 1 to 2^4-1=15
      long long m = s, bit = 0;
      for (long long j = 1; j <= 4; j++) {
        if ((i >> (j - 1)) % 2 == 1) {
          m -= (d[j] + 1) * c[j];
          bit++;
        }
      }
      if (m >= 0) ans += (bit % 2 * 2 - 1) * f[m];
    }
    cout << f[s] - ans << '\n';
  }
  return 0;
}

Coloring a Complete-Graph Subgraph

The first three examples use inclusion–exclusion directly; this problem requires analyzing it in reverse.

Coloring a Complete-Graph Subgraph

A and B like coloring graphs (not necessarily connected), with the rule that adjacent vertices must receive the same color. Today they play a game on an nn-vertex complete graph G=(V,E)G=(V,E). They define a scoring function F(S)F(S), where S is an edge set and S⊆ES\subseteq E. The value of F(S)F(S) is the total number of ways to color G′=(V,S)G'=(V,S) with mm colors. Their other rule is that if ∣S∣|S| is odd, A's score increases by F(S)F(S); otherwise B's score increases by F(S)F(S). Find the difference between A's and B's scores.

Mathematical Form

The algorithmic direction is not obvious at first glance, so for a difficult problem we first abstract its mathematical form. The score difference is an alternating sum, so powers of -1 can serve as coefficients. We seek

Ans=∑S⊆E(−1)∣S∣−1F(S)Ans=\sum_{S\subseteq E}(-1)^{|S|-1}F(S)

Inclusion–Exclusion Model

We treat the requirement that adjacent vertices have the same color as a property. For now, ignore the coloring rule and color the graph directly with m colors. For the graph G′=(V,S)G'=(V,S), regard a coloring as an element. The property xi=xjx_i=x_j means that vertices i and j have the same color (there need not be an edge between i and j).

Define the set corresponding to property xi=xjx_i=x_j as Qi,jQ_{i,j}. It consists of all colorings of graphs G′G' satisfying that property; its size is the number of colorings satisfying the property, and its elements can be viewed as the colored graphs G′G' satisfying it.

Returning to the problem, “adjacent vertices must have the same color” can be understood as an intersection of several QQ sets. Hence

F(S)=∣⋂(i,j)∈SQi,j∣F(S)=\left|\bigcap_{(i,j)\in S}Q_{i,j}\right|

The right-hand side says that every edge (i,j)(i,j) in S satisfies xi=xjx_i=x_j; its number of colorings is therefore F(S)F(S).

Does this look like inclusion–exclusion? Since inclusion–exclusion has no native pair form, map all edges (i,j)(i,j) to T=n(n+1)2T=\frac{n(n+1)}{2} integers. Suppose (i,j)(i,j) maps to k,1≤k≤Tk,1\leq k\leq T, and Qi,jQ_{i,j} maps to QkQ_k. Then define the property xi=xjx_i=x_j as PkP_k.

Similarly, S can be represented as a set of several k values, namely S  ⟺  K={k1,k2,⋯ ,km}S\iff K=\{k_1,k_2,\cdots,k_m\}. (In other words, we establish an equivalence between edge sets and sets of integers.)

The edge set E corresponds to the set M={1,2,⋯ ,n(n+1)2}M=\left\{1,2,\cdots,\frac{n(n+1)}{2}\right\}. Therefore,

F(S)  ⟺  F({ki})=∣⋂kiQki∣F(S)\iff F(\{ {k_i}\})=\left|\bigcap_{k_i}Q_{k_i}\right|

Reverse Analysis

Expanding the desired expression gives

Ans=∑K⊆M(−1)∣K∣−1∣⋂ki∈KQki∣=∑i∣Qi∣−∑i<j∣Qi∩Qj∣+∑i<j<k∣Qi∩Qj∩Qk∣−⋯+(−1)T−1∣⋂i=1TQi∣\begin{aligned} Ans &= \sum_{K\subseteq M}(-1)^{|K|-1}\left|\bigcap_{k_i\in K}Q_{k_i}\right|\\ &= \sum_{i}|Q_i|-\sum_{i<j}|Q_i\cap Q_j|+\sum_{i<j<k}|Q_i\cap Q_j\cap Q_k|-\cdots+(-1)^{T-1}\left|\bigcap_{i=1}^TQ_i\right| \end{aligned}

This is exactly the expansion of inclusion–exclusion, so we derive the expression in reverse:

Ans=∣⋃i=1TQi∣Ans=\left|\bigcup_{i=1}^TQ_i\right|

Now consider the meaning of the right-hand side. It requires satisfying any one of the 1∼T1\sim T conditions, meaning the number of colorings in which some two vertices have the same color (not necessarily adjacent). The universal set of colorings is UU, clearly ∣U∣=mn|U|=m^n. Passing to the complement means counting colorings in which every pair has different colors, namely Amn=m!(m−n)!A_m^n=\frac{m!}{(m-n)!}. Therefore,

Ans=mn−AmnAns=m^n-A_m^n

To solve this problem, first abstract its mathematical form. Then start from the most informative condition, the definition of the function F(S)F(S), and convert it into intersections, unions, and complements of sets. Finally, transform the expression into the form of inclusion–exclusion and derive it in reverse. This problem demonstrates the reverse use of inclusion–exclusion.

Inclusion–Exclusion in Number Theory

Inclusion–exclusion can be used cleverly to solve number-theoretic problems.

Counting Pairs with GCD k

Consider the following problem:

Counting Pairs with GCD kk

Let 1≤x,y≤N1 \le x, y \le N. Let f(k)f(k) denote the number of ordered pairs with greatest common divisor kk, namely the pairs (x,y)(x, y). Find the values from f(1)f(1) to f(N)f(N).

This can be solved with the Euler totient function or Möbius inversion, but inclusion–exclusion is simpler.

By inclusion–exclusion, first find all pairs for which kk is a common divisor, then remove all pairs for which a multiple of kk is a common divisor. The remaining pairs are those for which kk is the greatest common divisor. In other words, f(k)=f(k)= is the number of pairs for which kk is a common divisor −- the number of pairs for which a multiple of kk is a common divisor.

It is also easy to see that the number of pairs for which a multiple of kk is a common divisor equals the sum of the numbers of pairs for which multiples of kk are the greatest common divisor. Hence,

f(k)=⌊(N/k)⌋2−∑i=2i∗k≤Nf(i∗k)f(k)= \lfloor (N/k) \rfloor ^2 - \sum_{i=2}^{i*k \le N} f(i*k)

When k>N/2k>N/2, we can directly calculate f(k)=⌊(N/k)⌋2f(k)= \lfloor (N/k) \rfloor ^2. Therefore, we can work backward from f(N)f(N) down to f(1)f(1). This completes the problem using inclusion–exclusion.

for (long long k = N; k >= 1; k--) {
  f[k] = (N / k) * (N / k);
  for (long long i = k + k; i <= N; i += k) f[k] -= f[i];
}

The time complexity of the method above is O(∑i=1NN/i)=O(N∑i=1N1/i)=O(Nlog⁡N)O( \sum_{i=1}^{N} N/i)=O(N \sum_{i=1}^{N} 1/i)=O(N \log N).

Here are three extra practice problems:

Deriving the Euler Totient Function by Inclusion–Exclusion

Consider the following problem:

Euler Totient Formula

Find the Euler totient φ(n)\varphi(n), where φ(n)=∣{1≤x≤n∣gcd⁡(x,n)=1}∣\varphi(n)=|\{1\leq x\leq n|\gcd(x,n)=1\}|.

Direct computation takes O(nlog⁡n)O(n\log n) time, a linear sieve takes O(n)O(n) time, and the Du sieve takes O(n23)O(n^{\frac{2}{3}}) time (although it is odd to bring up the Du sieve in an introductory number-theory problem solved by inclusion–exclusion). We now derive the Euler totient formula using inclusion–exclusion.

To determine whether two numbers are coprime, first factorize

n=∏i=1kpicin=\prod_{i=1}^k{p_i}^{c_i}

For every pip_i, we require that xx is not a multiple of pip_i, namely pi∤xp_i\nmid x. Treat this as a property, with corresponding set SiS_i. Thus,

φ(n)=∣⋂i=1kSi∣=∣U∣−∣⋃i=1kSi‾∣\varphi(n)=\left|\bigcap_{i=1}^kS_i\right|=|U|-\left|\bigcup_{i=1}^k\overline{S_i}\right|

The universal set has size ∣U∣=n|U|=n. The set Si‾\overline{S_i} consists of values satisfying pi∣xp_i\mid x, so clearly ∣Si‾∣=npi|\overline{S_i}|=\frac{n}{p_i}. Therefore,

∣⋂ai<ai+1Sai∣=n∏pai\left|\bigcap_{a_i<a_{i+1}}S_{a_i}\right|=\frac{n}{\prod p_{a_i}}

It follows that

φ(n)=n−∑inpi+∑i<jnpipj−⋯+(−1)knp1p2⋯pk=n(1−1p1)(1−1p2)⋯(1−1pk)=n∏i=1k(1−1pi)\begin{aligned} \varphi(n)&=n-\sum_{i}\frac{n}{p_i}+\sum_{i<j}\frac{n}{p_ip_j}-\cdots+(-1)^k\frac{n}{p_1p_2 \cdots p_k}\\ &=n\left(1-\frac{1}{p_1}\right)\left(1-\frac{1}{p_2}\right)\cdots\left(1-\frac{1}{p_k}\right)\\ &=n\prod_{i=1}^k\left(1-\frac{1}{p_i}\right) \end{aligned}

This is the mathematical expression for the Euler totient function.

Generalization of Inclusion–Exclusion

The inclusion–exclusion principle is often used for set-counting problems. For functions f(S),g(S)f(S),g(S) on subsets, if

f(S)=∑T⊆Sg(T)f(S)=\sum_{T\subseteq S}g(T)

then

g(S)=∑T⊆S(−1)∣S∣−∣T∣f(T)g(S)=\sum_{T\subseteq S}(-1)^{|S|-|T|}f(T)

Proof

We give a brief proof, starting from the right-hand side:

∑T⊆S(−1)∣S∣−∣T∣f(T)=∑T⊆S(−1)∣S∣−∣T∣∑Q⊆Tg(Q)=∑Qg(Q)∑Q⊆T⊆S(−1)∣S∣−∣T∣\begin{aligned} &\sum_{T\subseteq S}(-1)^{|S|-|T|}f(T)\\ =&\sum_{T\subseteq S}(-1)^{|S|-|T|}\sum_{Q\subseteq T}g(Q)\\ =&\sum_{Q}g(Q)\sum_{Q\subseteq T\subseteq S}(-1)^{|S|-|T|}\\ \end{aligned}

The latter summation is independent of QQ, so remove the latter QQ:

=∑Qg(Q)∑T⊆(S∖Q)(−1)∣S∖Q∣−∣T∣=\sum_{Q}g(Q)\sum_{T\subseteq (S\setminus Q)}(-1)^{|S\setminus Q|-|T|}

For a set PP, define the function F(P)=∑T⊆P(−1)∣P∣−∣T∣F(P)=\sum_{T\subseteq P}(-1)^{|P|-|T|} and simplify it:

F(P)=∑T⊆P(−1)∣P∣−∣T∣=∑i=0∣P∣(∣P∣i)(−1)∣P∣−i=∑i=0∣P∣(∣P∣i)1i(−1)∣P∣−i=(1−1)∣P∣=0∣P∣\begin{aligned} F(P)&=\sum_{T\subseteq P}(-1)^{|P|-|T|}\\ &=\sum_{i=0}^{|P|}\dbinom{|P|}{i}(-1)^{|P|-i}=\sum_{i=0}^{|P|}\dbinom{|P|}{i}1^i(-1)^{|P|-i}\\ &=(1-1)^{|P|}=0^{|P|} \end{aligned}

Thus the value of the original expression is

∑Qg(Q)∑T⊆(S∖Q)(−1)∣S∖Q∣−∣T∣=∑Qg(Q)F(S∖Q)=∑Qg(Q)⋅0∣S∖Q∣\sum_{Q}g(Q)\sum_{T\subseteq (S\setminus Q)}(-1)^{|S\setminus Q|-|T|}=\sum_{Q}g(Q)F(S\setminus Q)=\sum_{Q}g(Q)\cdot 0^{|S\setminus Q|}

Only when ∣S∖Q∣=0|S\setminus Q|=0 do we have 00=10^0=1; then Q=SQ=S, whose contribution to the answer is g(S)g(S). In all other cases 0∣S∖Q∣=00^{|S\setminus Q|}=0, so there is no contribution. Hence

∑Qg(Q)⋅0∣S∖Q∣=g(S)\sum_{Q}g(Q)\cdot 0^{|S\setminus Q|}=g(S)

This proves the result.

Corollary

There is also the following corollary. Under the universal set UU, for functions f(S),g(S)f(S),g(S), if

f(S)=∑S⊆Tg(T)f(S)=\sum_{S\subseteq T}g(T)

then

g(S)=∑S⊆T(−1)∣T∣−∣S∣f(T)g(S)=\sum_{S\subseteq T}(-1)^{|T|-|S|}f(T)

This corollary is the complement form; the proof is similar.

Counting DAGs

Counting DAGs

Count labeled directed acyclic graphs on nn vertices modulo 109+710^9+7. n≤5×103n\leq 5\times 10^3.

Direct DP

Consider a DP. Define f[i,j]f[i,j] as the number of DAGs on ii vertices with exactly jj vertices of indegree 00. Suppose that after removing these jj vertices, there are kk vertices of indegree 00. Before removal, each of these kk vertices must have an edge from at least one of the jj removed vertices, giving 2j−12^j-1 possibilities. The jj removed vertices may also connect arbitrarily to the remaining vertices other than the kk vertices, giving 2i−j−k2^{i-j-k} possibilities. Thus,

f[i,j]=(ij)∑k=1i−j(2j−1)k2(i−j−k)jf[i−j,k]f[i,j]=\binom{i}{j}\sum_{k=1}^{i-j}(2^j-1)^k2^{(i-j-k)j}f[i-j,k]

The complexity of computing this expression is O(n3)O(n^3).

Relaxing the Constraint

The DP above defines exactly jj vertices of indegree 00, which is too restrictive. We may instead require at least jj vertices of indegree 00. Define f[i]f[i] directly as the number of DAGs on ii vertices. Inclusion–exclusion applies directly. For the selected jj vertices, each of these jj vertices may have arbitrary edges to the remaining i−ji-j vertices, giving (2i−j)j=2(i−j)j\left(2^{i-j}\right)^j=2^{(i-j)j} possibilities:

f[i]=∑j=1i(−1)j−1(ij)2(i−j)jf[i−j]f[i]=\sum_{j=1}^i(-1)^{j-1}\binom{i}{j}2^{(i-j)j}f[i-j]

The complexity of this expression is O(n2)O(n^2).

Min–Max Inclusion–Exclusion

For a sequence {xi}\{x_i\} whose elements lie in a total order total order and support addition and subtraction, let its length be nn and let S={1,2,3,⋯ ,n}S=\{1,2,3,\cdots,n\}. Then

max⁡i∈Sxi=∑T⊆S(−1)∣T∣−1min⁡j∈Txj\max_{i\in S}{x_i}=\sum_{T\subseteq S}{(-1)^{|T|-1}\min_{j\in T}{x_j}} min⁡i∈Sxi=∑T⊆S(−1)∣T∣−1max⁡j∈Txj\min_{i\in S}{x_i}=\sum_{T\subseteq S}{(-1)^{|T|-1}\max_{j\in T}{x_j}}

Proof: Map this to the ordinary inclusion–exclusion principle. For x∈Sx\in S, suppose xx is the kk-th smallest element. Define the map f:x↦{1,2,⋯ ,k}f:x\mapsto \{1,2,\cdots,k\}. Clearly, this is a bijection.

For x,y∈Sx,y\in S, it is easy to see that f(min⁡(x,y))=f(x)∩f(y)f(\min(x,y))=f(x)\cap f(y) and f(max⁡(x,y))=f(x)∪f(y)f(\max(x,y))=f(x)\cup f(y). Therefore,

∣f(max⁡i∈Sxi)∣=∣⋃i∈Sf(xi)∣=∑T⊆S(−1)∣T∣−1∣⋂j∈Tf(xj)∣=∑T⊆S(−1)∣T∣−1∣f(min⁡j∈Txj)∣\begin{aligned} \left|f\left(\max_{i\in S}{x_i}\right)\right| &= \left| \bigcup_{i\in S} f(x_i) \right|\\ &= \sum_{T\subseteq S}(-1)^{|T|-1} \left|\bigcap_{j\in T}f(x_j)\right|\\ &= \sum_{T\subseteq S}(-1)^{|T|-1} \left|f\left(\min_{j\in T}{x_j}\right)\right|\\ \end{aligned}

Now map ∣f(max⁡i∈Sxi)∣\left|f\left(\max_{i\in S}{x_i}\right)\right| back to max⁡i∈Sxi\max_{i\in S}{x_i}; the case for min⁡\min is analogous.

QED.

You may find this formula pointless because a maximum can be computed directly. Min–max inclusion–exclusion is important because it also holds for expectations:

E(max⁡i∈Sxi)=∑T⊆S(−1)∣T∣−1E(min⁡j∈Txj)E\left(\max_{i\in S}{x_i}\right)=\sum_{T\subseteq S}{(-1)^{|T|-1}E\left(\min_{j\in T}{x_j} \right)} E(min⁡i∈Sxi)=∑T⊆S(−1)∣T∣−1E(max⁡j∈Txj)E\left(\min_{i\in S}{x_i}\right)=\sum_{T\subseteq S}{(-1)^{|T|-1}E\left(\max_{j\in T}{x_j} \right)}

Proof: Consider one way to calculate an expectation:

E(max⁡i∈Sxi)=∑yP(y=x)max⁡j∈SyjE\left(\max_{i\in S}{x_i}\right)=\sum_{y}{P(y=x)\max_{j\in S}{y_j}}

Here yy is a sequence of length nn.

Apply the previous formula to the max⁡\max that follows:

E(max⁡i∈Sxi)=∑yP(y=x)max⁡j∈Syj=∑yP(y=x)∑T⊆S(−1)∣T∣−1min⁡j∈Tyj\begin{aligned}E\left(\max_{i\in S}{x_i}\right)&=\sum_{y}{P(y=x)\max_{j\in S}{y_j}}\\ &=\sum_{y}{P(y=x)\sum_{T\subseteq S}{(-1)^{|T|-1}\min_{j\in T}{y_j}}} \end{aligned}

Swap the order of summation:

E(max⁡i∈Sxi)=∑yP(y=x)∑T⊆S(−1)∣T∣−1min⁡j∈Tyj=∑T⊆S(−1)∣T∣−1∑yP(y=x)min⁡j∈Tyj=∑T⊆S(−1)∣T∣−1E(min⁡j∈Tyj)\begin{aligned}E\left(\max_{i\in S}{x_i}\right) &=\sum_{y}{P(y=x)\sum_{T\subseteq S}{(-1)^{|T|-1}\min_{j\in T}{y_j}}}\\ &=\sum_{T\subseteq S}{(-1)^{|T|-1}\sum_y{P(y=x)\min_{j\in T}{y_j}}}\\ &=\sum_{T\subseteq S}{(-1)^{|T|-1}E\left(\min_{j\in T}{y_j}\right)} \end{aligned}

The case for min⁡\min is analogous.

QED.

There is an even stronger form:

kthmax⁡xii∈S=∑T⊆S(−1)∣T∣−k(∣T∣−1k−1)min⁡j∈Txj\underset{i\in S}{\operatorname{kthmax}{x_i}}=\sum_{T\subseteq S}{(-1)^{|T|-k}\dbinom {|T|-1}{k-1}\min_{j\in T}{x_j}} kthmin⁡xii∈S=∑T⊆S(−1)∣T∣−k(∣T∣−1k−1)max⁡j∈Txj\underset{i\in S}{\operatorname{kthmin}{x_i}}=\sum_{T\subseteq S}{(-1)^{|T|-k}\dbinom {|T|-1}{k-1}\max_{j\in T}{x_j}} E(kthmax⁡xii∈S)=∑T⊆S(−1)∣T∣−k(∣T∣−1k−1)E(min⁡j∈Txj)E\left(\underset{i\in S}{\operatorname{kthmax}{x_i}}\right)=\sum_{T\subseteq S}{(-1)^{|T|-k}\dbinom {|T|-1}{k-1}E\left(\min_{j\in T}{x_j}\right)} E(kthmin⁡xii∈S)=∑T⊆S(−1)∣T∣−k(∣T∣−1k−1)E(max⁡j∈Txj)E\left(\underset{i\in S}{\operatorname{kthmin}{x_i}}\right)=\sum_{T\subseteq S}{(-1)^{|T|-k}\dbinom {|T|-1}{k-1}E\left(\max_{j\in T}{x_j}\right)}

By convention, if n<mn< m, then (nm)=0\dbinom nm=0.

Proof: Without loss of generality, assume ∀1≤i<n,xi≤xi+1\forall 1\le i<n,x_i\le x_{i+1}. Then

∑T⊆S(−1)∣T∣−k(∣T∣−1k−1)min⁡j∈Txj=∑i∈Sxi∑T⊆S(−1)∣T∣−k(∣T∣−1k−1)[xi=min⁡j∈Txj]=∑i∈Sxi∑j=kn(n−ij−1)(j−1k−1)(−1)j−k\begin{aligned} \sum_{T\subseteq S}{(-1)^{|T|-k}\dbinom {|T|-1}{k-1}\min_{j\in T}{x_j}} &=\sum_{i\in S}{x_i\sum_{T\subseteq S}{(-1)^{|T|-k}\dbinom {|T|-1}{k-1}\left[x_i=\min_{j\in T}{x_j} \right]}}\\ &=\sum_{i\in S}{x_i\sum_{j=k}^n{\dbinom {n-i}{j-1}\dbinom {j-1}{k-1}(-1)^{j-k}}} \end{aligned}

Using the combinatorial identity (ab)(bc)=(ac)(a−cb−c)\dbinom ab\dbinom bc=\dbinom ac\dbinom {a-c}{b-c}, we obtain

∑T⊆S(−1)∣T∣−k(∣T∣−1k−1)min⁡j∈Txj=∑i∈Sxi∑j=kn(n−ij−1)(j−1k−1)(−1)j−k=∑i∈Sxi∑j=kn(n−ik−1)(n−i−k+1j−k)(−1)j−k=∑i∈S(n−ik−1)xi∑j=kn(n−i−k+1j−k)(−1)j−k=∑i∈S(n−ik−1)xi∑j=0n−i−k+1(n−i−k+1j)(−1)j\begin{aligned} \sum_{T\subseteq S}{(-1)^{|T|-k}\dbinom {|T|-1}{k-1}\min_{j\in T}{x_j}} &=\sum_{i\in S}{x_i\sum_{j=k}^n{\dbinom {n-i}{j-1}\dbinom {j-1}{k-1}(-1)^{j-k}}}\\ &=\sum_{i\in S}{x_i\sum_{j=k}^n{\dbinom {n-i}{k-1}\dbinom {n-i-k+1}{j-k}(-1)^{j-k}}}\\ &=\sum_{i\in S}{\dbinom {n-i}{k-1}x_i\sum_{j=k}^n{\dbinom {n-i-k+1}{j-k}(-1)^{j-k}}}\\ &=\sum_{i\in S}{\dbinom {n-i}{k-1}x_i\sum_{j=0}^{n-i-k+1}{\dbinom {n-i-k+1}j(-1)^{j}}} \end{aligned}

When i=n−k+1i=n-k+1,

(n−ik−1)∑j=0n−i−k+1(n−i−k+1j)(−1)j=1\dbinom {n-i}{k-1}\sum_{j=0}^{n-i-k+1}{\dbinom {n-i-k+1}j(-1)^{j}}=1

Otherwise,

(n−ik−1)∑j=0n−i−k+1(n−i−k+1j)(−1)j=0\dbinom {n-i}{k-1}\sum_{j=0}^{n-i-k+1}{\dbinom {n-i-k+1}j(-1)^{j}}=0

Therefore,

∑i∈S(n−ik−1)xi∑j=0n−i−k+1(n−i−k+1j)(−1)j=kthmax⁡i∈Sxi\sum_{i\in S}{\dbinom {n-i}{k-1}x_i\sum_{j=0}^{n-i-k+1}{\dbinom {n-i-k+1}j(-1)^{j}}}=\underset{i\in S}{\operatorname{kthmax}}{x_i}

The other three identities are analogous.

QED.

Min–max inclusion–exclusion also gives

lcm⁡i∈Sxi=∏T⊆S(gcd⁡j∈Txj)(−1)∣T∣−1\underset{i\in S}{\operatorname{lcm}}{x_i}=\prod_{T\subseteq S}{\left(\gcd_{j\in T}{x_j} \right)^{(-1)^{|T|-1}}}

Because lcm⁡,gcd⁡,a1,a−1\operatorname{lcm},\gcd,a^{1},a^{-1} correspond respectively to max⁡,min⁡,+,−\max,\min,+,-, this is exactly min–max inclusion–exclusion applied to the exponents, so the identity follows.

PKUWC2018 Random Walk

PKUWC2018 Random Walk

Given a tree with nn vertices, start from xx and at each step choose uniformly at random an edge adjacent to the current vertex and traverse it.

There are QQ queries. Each query gives a set SS and asks for the expected number of steps in a random walk starting from xx and continuing until every vertex in SS has been visited at least once.

In particular, vertex xx (the starting vertex) is considered visited once initially.

Compute the answer modulo 998244353998244353.

1≤n≤18,1≤Q≤5000,1≤∣S∣≤n1\le n\le 18,1\le Q\le 5000,1\le |S|\le n.

The number of steps in the random walk is its elapsed time. Let the random variable xix_i be the first time the walk reaches vertex ii. We seek

E(max⁡i∈Sxi)E\left(\max_{i\in S}x_i\right)

Min–max inclusion–exclusion gives

E(max⁡i∈Sxi)=E(∑T⊆S(−1)∣T∣−1min⁡i∈Txi)=∑T⊆S(−1)∣T∣−1E(min⁡i∈Txi)E\left(\max_{i\in S}x_i\right) =E\left(\sum_{T\subseteq S}(-1)^{|T|-1}\min_{i\in T}x_i\right) =\sum_{T\subseteq S}(-1)^{|T|-1}E\left(\min_{i\in T}x_i\right)

For a set T∈[n]T\in[n], consider computing F(T)=E(min⁡i∈Txi)F(T)=E(\min_{i\in T}x_i).

The meaning of E(min⁡i∈Txi)E(\min_{i\in T}x_i) is the expected time to first reach some vertex in TT. Let f(i)f(i) denote the expected time to first reach a vertex when starting from vertex ii in TT.

  • For i∈Ti\in T, f(i)=0f(i)=0.
  • For i∉Ti\notin T, f(i)=1+1deg(i)∑(i,j)∈Ef(j)f(i)=1+\frac{1}{\text{deg}(i)}\sum_{(i,j)\in E}f(j).

Gaussian elimination would take O(n3)O(n^3) time. Then, for each set TT, computing F(T)F(T) would give total complexity O(2nn3)O(2^nn^3), which is unacceptable. We use elimination on a tree.

Let the root be 11, and let the parent of vertex uu be pup_u. For a leaf ii, f(i)f(i) depends only on the parent ii (and it may even be f(i)=0f(i)=0, which is easier). Thus we can write f(i)f(i) in the form f(i)=Ai+Bif(pi)f(i)=A_i+B_if(p_i), where Ai,BiA_i,B_i can be computed quickly.

For a non-leaf vertex ii, consider its sequence of children j1,⋯ ,jkj_1,\cdots,j_k. Since f(je)=Aje+Bjef(i)f(j_e)=A_{j_e}+B_{j_e}f(i), we obtain

f(i)=1+1deg⁡(i)∑e=1k(Aje+Bjef(i))+f(pi)deg⁡(i)f(i)=1+\frac{1}{\deg(i)}\sum_{e=1}^k\left(A_{j_e}+B_{j_e}f(i)\right)+\frac{f(p_i)}{\deg(i)}

Rearranging gives

f(i)=deg⁡(i)+∑e=1kAjedeg⁡(i)−∑e=1kBje+f(pi)deg⁡(i)−∑e=1kBjef(i)=\frac{\deg(i)+\sum_{e=1}^kA_{j_e}}{\deg(i)-\sum_{e=1}^kB_{j_e}}+ \frac{f(p_i)}{\deg(i)-\sum_{e=1}^kB_{j_e}}

Thus f(i)f(i) can also be written in the form Ai+Bif(pi)A_i+B_if(p_i). We can continue this backward to the root. The root has no parent, so

f(1)=deg⁡(1)+∑e=1kAjedeg⁡(1)−∑e=1kBjef(1)=\frac{\deg(1)+\sum_{e=1}^kA_{j_e}}{\deg(1)-\sum_{e=1}^kB_{j_e}}

Solving this equation gives f(1)f(1); one downward pass then gives every vertex's f(i)f(i). Thus F(T)=f(x)F(T)=f(x), with time complexity O(n)O(n).

We can therefore, for every TT, compute F(T)F(T) in time O(2nn)O(2^nn).

Returning to inclusion–exclusion, we know that E(max⁡i∈Sxi)=∑T⊆S(−1)∣T∣−1F(T)E(\max_{i\in S}x_i)=\sum_{T\subseteq S}(-1)^{|T|-1}F(T).

Let F′(T)=(−1)∣T∣−1F(T)F'(T)=(-1)^{|T|-1}F(T). Then E(max⁡i∈Sxi)=∑T⊆SF′(T)E(\max_{i\in S}x_i)=\sum_{T\subseteq S}F'(T). Therefore, in time O(2nn)O(2^nn), for every SS we compute E(max⁡i∈Sxi)E(\max_{i\in S}x_i), and answer each query in O(1)O(1).

Exercises

References

A Brief Exploration of the Inclusion–Exclusion Principle — Wang Di, 2013 Collection of Papers by Candidates for the Chinese National Team in the Informatics Olympiad

Counting Series of Labeled DAGs — Cyhlnj

Total Order — Wikipedia

This content comes from OI Wiki: Inclusion–Exclusion Principle. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.