Python study notes: permutation and combination

1. Permutation and combination formula

1. Arrangement formula

P n m = n ( n − 1 ) ( n − 2 ) . . . ( n − m + 1 ) = n ! ( n − m ) ! P_n^m=n(n-1)(n-2)...(n-m+1)=\displaystyle\frac{n!}{(n-m)!} Pnm=n(n1)(n2)...(nm+1)=(nm)!n!
P n n = n ! ( 0 ! = 1 ) P_n^n=n! (0!=1) Pnn=n!(0!=1)

2. Combination formula

C n m = n ( n − 1 ) ( n − 2 ) . . . ( n − m + 1 ) m ! = n ! ( n − m ) ! m ! C_n^m=\displaystyle\frac{n(n-1)(n-2)...(n-m+1)}{m!}=\frac{n!}{(n-m)!m!} Cnm=m!n(n1)(n2)...(nm+1)=(nm)!m!n!

Two, calculate permutation and combination

1. The task of placing books

  • Ms. Jones wants to put 10 10. 1 0 book is placed on the shelf, which has444 math books,3 33 chemistry books,2 22 history books and1 11 language document. Now Ms. Jones wants to organize her books. If the same books must be put together, how many ways are there?
  • If you put the math book first, then the chemistry book, then the history book, and finally the language book, then there is P 4 4 × P 3 3 × P 2 2 × P 1 1 = 4! × 3! × 2! × 1! P_4^4\times P_3^3\times P_2^2\times P_1^1=4!\times3!\times2!\times1!P44×P33×P22×P11=4!×3!×2!×1 ! Ways, and these4 4The order of the 4 disciplines isP 4 4 = 4! P_4^4 = 4!P44=4 ! Types, so the answer is4! × 4! × 3! × 2! × 1! = 6912 4!\times4!\times3!\times2!\times1!=69124!×4!×3!×2!×1!=6912
  • Use the perm() function in the scipy.special module to calculate
    Insert picture description here

2. Committee composition

  • A group has 12 121 2 people, of which5 55 ladies,7 77 men, now select2 22 ladies and3 3Three men form a committee. How many different committees are there?
  • Because there is C 5 2 C_5^2C52There are two ways to select women, there are C 7 3 C_7^3C73There are two ways to select men, so according to the basic counting rule, one is C 5 2 × C 7 3 = 5 × 4 2 × 1 × 7 × 6 × 5 3 × 2 × 1 = 350 C_5^2 \times C_7^3= \displaystyle\frac{5\times4}{2\times1}\times\frac{7\times6\times5}{3\times2\times1}=350C52×C73=2×15×4×3×2×17×6×5=3 5 0 possible Commission.
  • Use the comb() function in the scipy.special module to calculate
    Insert picture description here

Three, display permutation and combination

1. Show full arrangement

  • 1, 2, 3 all arrangements

Insert picture description here

2. Display arrangement

  • Arrangement of 3 of 1, 2, 3, 4, and 5
    Insert picture description here

3. Display combination

  • Take 3 of 1, 2, 3, 4, and 5 to form a group. How many possible groups are there in total?
    Insert picture description here

Guess you like

Origin blog.csdn.net/howard2005/article/details/109283877
Recommended