Chapter II: Fundamentals of Mathematics
Basic Counting Method:
In this chapter, the basic principle of counting, the principle of tolerance, and the two-term coefficient identity are no longer reflected, this column mainly through specific topics to enhance the application of theoretical knowledge.
Let's take a look at two basic counting questions:
EX1: A repeatable selection of combinations: There are n different elements, each element can be selected multiple times, the total number of K elements to choose how many methods?
Analysis: The number of elements selected in the first element is XI, then we can convert the problem to a x1+x2+...xn = k non-negative integer solution (x1,x2,...., xn). In fact, it is easy to think of the method of inserting a partition, we give this equation a combination of meaning, that is, like K 1 formed k-1 interval inserted n-1 partition, the number of cases of this action is the number of n-tuple solution, but we list the counting formula found to be C (k-1,n-1), although in the "specific mathematics" The meaning of this combinatorial number is given in the book, but it is not well understood here.
We use a roundabout strategy to establish a one by one mapping relationship: Yi = xi+1, then the original equation is equivalent to y1 + Y2...yn = k + N of N-ary non-negative integer solution number, so we still use the above insert partition principle, you can get the counting formula C (n+k-1,n-1) = C (n+ K-1,K).
EX2: Monochrome triangle: N (n≤1000) points in a given space, where there are no three-point collinear lines. Each of the two points is connected by a red or black segment (the graph can be given in the form of a matrix). Find the number of triangles with 3 sides of the same color.
Analysis: The violence law is obviously theoretically solvable, but the time complexity is O (n^3), which we need to optimize. From the negative consideration, to seek non-monochromatic triangles, we are easy to see, non-monochrome triangles must have two points, the two points connected to the two different color sides, then we from the point of connection side of the case to consider, for the first point VI, we assume that there is AI a red edge, apparently VI connected to the N-1-ai black Edge, Then we can construct an AI (N-1-ai) non-monochromatic triangle, combined with the results we have just observed, we will find that this calculation mode will calculate each non-monochromatic triangle two times, that is, the number of final non-monochromatic triangles is 1/2∑ai (N-1-ai).
Let's take a look at some of the combination counts in the contest.
Uva11538:
Given the n x M chessboard, put in two queens, how many situations can be formed so that the two queens can attack each other.
Analysis: Since this problem does not tell you that the square board can rotate the restrictions, then we do not consider using the Polya principle to weight.
First of all, based on the Queen's Walk in chess, it took the horizontal line, vertical lines, slashes, so here in order to enable two Queens to attack each other, we have analyzed from these three cases, we remember that these three cases are a (n,m), B (n,m), C (n,m). Suppose N is a row and M is a column.
(1) Horizontal line: First put a queen, there are NM methods, then another queen has M-1 method. i.e. A (n,m) = MN (m-1)
(2) Vertical line: First put in a queen, there are NM methods, then another queen has n-1 method. i.e. B (n,m) = NM (n-1)
(3) Slash: We first assume a size relationship--m>n, because this will limit the number of slashes we get, then based on this hypothesis, we will get the following diagonal length sequence:
1,2,3,4...n-2,n-1,n,n,n...n,n,n (m-n+1 N) ... n-1,n-2,4,3,2,1, also take into account that there are two groups of such diagonal.
So we are easy to know, C (n,m) = 2[2∑i (i-1) + (m-n+1) n (n-1)].
And for the beginning of our assumption of the m>n, we do this, will pass in the two parameters of the large assigned to M, so that a formula can be completed.
Then the simplification can be programmed to achieve.
The simple reference code is as follows.
#include <cstdio>#include<algorithm>using namespacestd;intMain () {Long LongN, M; while(SCANF ("%lld%lld", &n,&m)! =EOF) { if(n = =0&& m = =0) Break; if(M < n)//Guaranteed N≤mswap (N,M); printf ("%lld\n", m*n* (m+n-2) +2*n* (n1)*(3*m-n-1)/3); }}
"Training Guide"--6.6