Principle of combining mathematics
In composite mathematics, content rejection is often used. We use content rejection to solve some composite numbers with conditions.
Refresh principle: the number of elements with Properties A and B is equivalent to the number of elements with Properties A and B and the number of elements with both Properties A and B.
The mathematical formula is represented as | A ∪ B | = | A | + | B |-| A ∩ B |.
Graphical Representation
The yellow area is what we want.
Similarly, for the three properties, the mathematical formula is | A ∪ B ∪ C | = | A | + | B | + | C |-| A ∩ B |-| A ∩ C |-| B Branch C | + | A branch B Branch C |
Why should we add the last one? This is because one more is subtracted during the subtraction process.
For the principle of rejection, recursive and binary enumeration are commonly used. The biggest advantage of binary enumeration is to enumerate the subsets of all elements. Assume that there are m elements in a set, m is 1 or 0 for m-long binary numbers, for each 1
-Corresponds to an element. The entire binary enumeration is all subsets, from 0 to 2 ^ m.
Recursive Algorithms use the concept of dfs to search and retrieve each scheme for rejection. Because each question has a different search method, the same template is not used, so no code is required.
- In this case, all the odd and even properties are subtracted. If the requested properties are the opposite, the total number is subtracted.
- The principle of rejection is simple, and the actual situation is very complicated. Each question is rejected by a different nature, but the final idea remains unchanged, if you do more questions, you will gradually accumulate experience and finally get a good idea.