About permutations and combinations

Preface

In mathematics and combinatorics, permutation and combination are two basic concepts. Arrangement refers to the way in which a set of elements are arranged in a specific order, while combination considers the selection of elements in a way that is independent of order. Whether in daily life or in scientific research, permutation and combination play an important role. By deeply understanding the principles and applications of permutation and combination, we can solve many interesting and complex problems and expand our understanding of the world. In this article, we will explore the basic concepts, properties, and applications of permutations and combinations to help you better understand and use this mathematical tool.
Insert image description here

arrangement

Arrange using A nm A _ n ^ mAnmmeans, LaTeX \LaTeXLATEX is expressed as$A _ n ^ m$

Calculation method

A n m A_n^m Anmmeans starting from nnSelectmm from n elementsThe number of arrangements of m elements, usually recorded as A nm A_n^mAnmor P ( n , m ) P(n, m)P(n,m ) . Its calculation formula is:
A nm = n ! ( n − m ) ! A_n^m = \frac{n!}{(nm)!}Anm=(nm)!n!
where n ! n!n ! meansnnn 的阶乘,即 n ! = n ⋅ ( n − 1 ) ⋅ ( n − 2 ) ⋅ … ⋅ 2 ⋅ 1 n! = n \cdot (n-1) \cdot (n-2) \cdot \ldots \cdot 2 \cdot 1 n!=n(n1)(n2)21

In other words, A nm A_n^mAnmmeans starting from nnFrom n different elements, select mmin orderThe number of ways m elements can be arranged.

C++ representation

// 计算阶乘
int factorial(int n) {
    
    
    if (n <= 1) {
    
    
        return 1;
    }
    return n * factorial(n - 1);
}

// 计算排列数
int calculatePermutation(int n, int m) {
    
    
    if (n < m) {
    
    
        return 0;  // 错误情况,返回0或抛出异常
    }
    return factorial(n) / factorial(n - m);
}

combination

Arrange using C nm C _ n ^ mCnmmeans, LaTeX \LaTeXLATEX is expressed as$C _ n ^ m$

Calculation method

C n m C_n^m Cnmmeans starting from nnSelectmm from n elementsThe number of combinations of m elements, usually recorded as C nm C_n^mCnmor C ( n , m ) C(n, m)C(n,m ) . Its calculation formula is:
C nm = n ! m ! ( n − m ) ! C_n^m = \frac{n!}{m!(nm)!}Cnm=m!(nm)!n!
where n ! n!n ! meansnnn 的阶乘,即 n ! = n ⋅ ( n − 1 ) ⋅ ( n − 2 ) ⋅ … ⋅ 2 ⋅ 1 n! = n \cdot (n-1) \cdot (n-2) \cdot \ldots \cdot 2 \cdot 1 n!=n(n1)(n2)21

In other words, C nm C_n^mCnmmeans starting from nnSelect mm from n different elements in an unordered mannerThe number of ways m elements can be combined. It is also known as the binomial coefficient, combinatorial number or permutation combinatorial number.


C n m = A n m n ! C_n^m = \frac{A_n^m}{n!} Cnm=n!Anm

The explanation of this formula is that from nnSelect mm from n elements in orderThe number of ways m elements can be arrangedA nm A_n^mAnm, divided by n! n!n ! is to eliminate double counting. Because combinations do not take into account the order of the elements, divide the number of permutations byn ! n!n ! , get the final number of combinations.

C++ representation

// 计算组合数
int calculateCombination(int n, int m) {
    
    
    // 基本情况
    if (m == 0 || n == m) {
    
    
        return 1;
    }
    
    // 递归调用
    return calculateCombination(n-1, m-1) + calculateCombination(n-1, m);
}

Guess you like

Origin blog.csdn.net/Python_enjoy/article/details/132700540