05 Counting Methods

Updated 4 Oct 2026

Basic Principles

Multiplication Principle

  • If an activity can be constructed in tt successive steps and
    • step 1 can be done in n1n_1 ways,
    • step 2 can be done in n2n_2 ways,
    • …\dotso
    • step tt can be done in ntn_t ways,
  • then the number of different possible activities is n1⋅n2⋅…⋅ntn_1\cdot n_2\cdot \dotso \cdot n_t

Sometimes, Multiplication Principle, followed by “subtraction” x ทั้งหมด ลบ ตรงข้าม?

Addition Principle

  • Used when the arrangements under consideration can be divided into “disjoint cases”.
  • หากการทำงานให้เสร็จมีหลายแบบ ให้แยกคิดแต่ละวิธีด้วยการคูณ (ทำงานให้เสร็จซะก่อน)
    แล้วจึงนำมาบวก กัน เป็นวิธีทั้งหมดที่ทำได้
    • The number of all arrangement: n1+n2+…+ntn_1+n_2+\dotso+n_t

ระวังโจทย์ที่เป็นมันสามารถมีบางตัวซ้ำกันได้ อย่าลืมลบออกด้วย อย่านับซ้ำ!

n(A∪B∪C)=n(A)+n(B)+n(C)−n(A∩B)−n(A∩C)−n(B∩C)+n(A∩B∩C)\boxed{n(A\cup B\cup C)=n(A)+n(B)+n(C)-n(A\cap B)-n(A\cap C)-n(B\cap C)+n(A\cap B\cap C)}

  • เหมือนโจทย์ปัญหา จำนวนสมาชิกใน Set แบบนั้นเลย!

Permutation and Combination

Permutation

  • Definition: A permutation of nn distinct elements x1,x2,… ,xnx_1,x_2,\dotso,x_n is an ordering of the nn elements x1,x2,… ,xnx_1,x_2,\dotso,x_n

Linear

Theorem: There are n!n! permutations of nn elements.

  • จริง ๆ นิยามของ n!n! ก็มาจาก กฎการคูณนั่นแหละ!

Circular

  • “Fixed ไว้ 1 จุด” แล้วที่เหลือคิดเหมือนเส้นตรง
  • 2D
    • ถ้ามีของแตกต่างกัน nn สิ่ง นำมาจัดเรียงสับเปลี่ยนแบบวงกลม ได้ (n−1)!(n-1)!

    In general: There are (n−1)!(n-1)! ways that nn persons can be seated around a circular table.

rr-Permutation

(ไม่ได้ใช้บ่อยขนาดนั้น)

  • การหาจำนวนวิธีเรียงสับเปลี่ยนของ nn สิ่ง (แตกต่างกันทั้งหมด) โดยการนำมาจัดครั้งละ rr สิ่ง
  • ลำดับมีความสำคัญ
  • C(n,r)⋅r!=P(n,r)C(n,r)\cdot r!=P(n,r) เลือกมาก่อนแล้วจัดเรียงสลับ ๆ
P(n,r)=n!(n−r)!\boxed{P(n,r)=\frac{n!}{(n-r)!}}

rr-Combination

  • เลือก
  • ลำดับไม่มีความสำคัญ
C(n,r)=n!(n−r)!r!=P(n,r)r!\boxed{C(n,r)=\frac{n!}{(n-r)!r!}=\frac{P(n,r)}{r!}}

Generalized Permutations and Combinations

  • In this section we consider
    • Orderings of sequences containing repetitions
    • Unordered selections in which repetitions are allowed

Generalized Permutation

  • Suppose that a sequence SS of nn items has n1n_1 identical objects of type 1, n2n_2 identical objects of type 2, …\dotso , and ntn_t identical objects of type tt. Then the number of orderings of SS is
    • เจอของซ้ำ
    n!n1!⋅n2!…nt!\boxed{\frac{n!}{n_1!\cdot n_2!\dotso n_{t}!}}

Generalized Combination

C(k+t−1,t−1)=C(k+t−1,k)\boxed{C(k + t − 1, t − 1) = C(k + t − 1, k)}
No RepetitionsRepetitions Allowed
Ordered Selections (Fixed Order)n!n!n!n1!⋅n2!…nt!\frac{n!}{n_1!\cdot n_2!\dotso n_{t}!}
Unordered SelectionsCn,rC_{n,r}Stars & Bar

Stars and Bars

Distribute identical objects among a number of different recipients

  • เวลาที่จะ Solve คำถามแนว x1+x2+x3=15x_1+x_2+x_3=15 แบบนี้นะ
    • กรณีไม่มี Constraint อะไร
      • ให้ใช้ Stars & Bars ปกติ
    • แต่ถ้ามี Constraint แค่ช่วงล่าง เช่น
      • Constraint >,≥0>,\ge 0 มีความหมายเหมือนกัน (แต่ถ้าเลขอื่นต้องระวังนะ)
      • หากมี Constraint เช่น
        • Constraint x1≥1x_1\ge1ให้เปลี่ยนสมการให้ดูง่ายกว่าเดิม y1=x1−1y_1=x_1-1 แบบนี้
        • Constraint x1>1x_1>1 ถ้าเป็นแบบนี้เปลี่ยนเป็น y1=x1−2y_1=x_1-2
    • ถ้ามี Constraint ช่วงบนด้วย
      • จัดการ ช่วงล่าง ให้เรียบร้อยก่อน แล้วค่อยหาคำตอบสมการ— แล้วค่อยเอามาลบกับช่วงที่มีช่วงบนร่วมอยู่ด้วย
      • การจะเปลี่ยนสมการช่วงบนจะดูยากนิดนึงนะ แต่มองได้แน่นอน 3≤x2≤63\le x_2 \le 6
        • ถ้าเป็นแบบนี้ก็ต้องเอาช่วงบนเป็น z2=y2−4z_2=y_2-4 ก็คือเราไม่เอา 7 ขึ้นไปถูกมั้ย (มองให้ออก ไม่รู้จะเขียนไง555)

วิธีนี้เหมือนจะไม่ถูกต้อง 100% นะ! ทำตามวิธีครูจะแม่นกว่า!

Stars & Bars for Nested Loop

	\begin{algorithm}
	\caption{How many times is the statement $x := x + 1$ executed?}
	\begin{algorithmic}
	\For{$i := 1$ \to $5$}
		\For{$j := 1$ \to $i$}
			\State $x:= x+1$
		\EndFor
	\EndFor
	\end{algorithmic}
	\end{algorithm}	

  • หลังกำแพงเวลาคิดก็จะได้ C6,2C_{6,2} เลยไง! ก็ถือว่าเป็นคำตอบของข้อนี้โลดดด
	\begin{algorithm}
	\caption{How many times is the statement $x := x + 1$ executed?}
	\begin{algorithmic}
	\For{$i := 1$ \to $10$}
		\For{$j := 1$ \to $i$}
			\For{$k := 1$ \to $j$}
				\State $x:= x+1$
			\EndFor
		\EndFor
	\EndFor
	\end{algorithmic}
	\end{algorithm}	

  • หลังกำแพงจะได้ C12,3C_{12,3} ก็เป็นคำตอบของข้อนี้เลยทันที~~~
	\begin{algorithm}
	\caption{How many times is the statement $x := x + 1$ executed?}
	\begin{algorithmic}
	\For{$i := 3$ \to $10$}
		\For{$j := 3$ \to $i$}
			\For{$k := 3$ \to $j$}
				\State $x:= x+1$
			\EndFor
		\EndFor
	\EndFor
	\end{algorithmic}
	\end{algorithm}