Question type: Combined mathematics
Required knowledge: polyA or Burnside
A string of N beads painted in C colors and asked how many different necklaces can be formed.
Restriction: the same type is obtained by rotation, and the same type is obtained by flip.
There are multiple algorithms in this question. The following two algorithms are easy to think of and understand.
Algorithm 2 is recommended because it is easier to process than algorithm 1 in case of high precision, and its complexity is much lower than algorithm 1. All core knowledge points are polyA theorem.
Solution Process:
Algorithm 1:
I. Calculate the number of rotation replacement and flip replacement:
1. Rotating replacement
N beads, the necklace is treated as a circle, each rotation angle is I * 360/N (I (1, N), a total of n different rotating.
2. Flip replacement
⊙ Consider the axis of symmetry:
N is an odd number. There is only one axis of symmetry, that is, the Axis passes through a vertex. In this way, there are n replications.
N is an even number and has two types of flip:
Each side of the axis has n/2 points. Such replacement has n/2 points;
The shaft is divided into two points, with n/2 points on each side. Such replacement also has n/2;
As a result, there are still n replicas, that is, if n is an odd even number, the number of replicas is the same.
To sum up, there are 2 * n Different replacements, that is, | G | = 2n.
Ii. Number of cyclic nodes for each replacement
1. Rotating replacement
Replacement result of the I-th rotation:
For (j = 0; j <n; j ++) <br/>{< br/> G [J] = (I + J) % N; <br/>}< br/> the number of cyclic nodes count is obtained in the following loop: <br/> for (j = 0, Count = 0; j <n; j ++) <br/> {// calculate the number of cyclic nodes for each rotation replacement, which is similar to finding connected subgraphs. <br/> If (flag [J] = 0) <br/> {// If the J node has never been accessed, it indicates a new loop, Count ++. <br/> count ++; <br/> for (k = J; flag [k] = 0; k = G [k]) <br/> flag [k] = 1; <br/>}< br/>}
2. Flip replacement
Replacement result of the I-th flip:
For (j = 1; (j + J) <n; j ++) <br/> {// The result of each flip replacement. The positions are exchanged along the symmetric point of the flip axis. <br/> // because each rotation replacement in the program is performed before the flip replacement, you only need to use the same flip loop for processing each time without further discussion. <br/> K = G [J]; <br/> G [J] = G [n-J]; <br/> G [n-J] = K; <br/>}< br/> the number of cyclic nodes is calculated in the following cycle: <br/> for (j = 0, Count = 0; j <n; j ++) <br/> {// calculate the number of cyclic nodes for each flip replacement, similar to finding connected subgraphs. <br/> If (flag [J] = 0) <br/> {// If the J node has never been accessed, it indicates a new loop, Count ++. <br/> count ++; <br/> for (k = J; flag [k] = 0; k = G [k]) <br/> flag [k] = 1; <br/>}< br/>}
Algorithm 2:
This question can also quickly calculate the number of cycles (or rotations) for each replacement to achieve the goal of fast solution:
Analysis (1)
1. rotate.
Consider clockwise rotation of the I-cell replacement:
The number of loops is gcd (n, I)
The length of each loop is L = N/gcd (n, I)
2. Flip
Symmetry Axis
* ** N is an odd number. there is only one axis of symmetry, that is, the Axis passes through a point. there are [n/2] cycles with a length of 2, and a cycle with a length of 1 (point to be passed), V = C ([n/2], [m/2]).
* ** N is an even number and has two types of flip
Each side of the axis has n/2 points. Such replacement has n/2 points.
The shaft is worn at two points, and each side has n/2 points. Such replacement also has n/2 points.
Algorithm complexity analysis: it is easy to calculate the time complexity of this algorithm as O (n ).
Analysis (2 ):
PolyA theorem: Set g to a group of n objects, and dye these n objects in M colors, the number of dyeing schemes L = 1/| G | * [m ^ P (A1) + m ^ P (A2) + .... + m ^ P (an)]. where p (AI) is the number of cycles of a replacement.
1. Rotating replacement.
We assume that the rotation is 1 ~ clockwise ~ N, then the number of loops = gcd (I, n );
2. Flip replacement
When N is an even number, there are two cases. One is that the center axis is on two symmetric objects, and the number of loops is n/2 + 1, the other is that the symmetric axis has n/2 objects on both sides, and the number of loops is n/2;
When N is an odd number, the symmetric axis can only be on one object, and the number of cycles is n/2 + 1;
Void polyA (int p, int N) <br/>{< br/> int I; <br/> ans = 0; <br/> for (I = 0; I <n; I ++) // The number of cyclic nodes in the I-th rotation is gcd (n, I). <br/> ans + = POW (p * 1.0, gcd (n, I); <br/> If (N % 2) // n is an odd number <br/>{< br/> for (I = 0; I <n; I ++) // a total of N cycles are all replaced by N/2 + 1; <br/> ans + = POW (p * 1.0, n/2 + 1); <br/>}< br/> else // n is an even number <br/>{< br/> for (I = 0; I <n/2; I ++) // as described above, there are two kinds of replacements. The first cycle number is n/2, <br/> ans + = POW (p * 1.0, n/2) + POW (p * 1.0, n/2 + 1 ); // The number of cycles in the second type is n/2 + 1; <br/>}< br/>}