Question: There are three colors and N beads, so you can find the total number of beads in a circle that are rotated, flipped, or essentially different.
Analysis: This is almost the same as poj2409, but there are only three colors. The solution is the same... If you don't understand it, take a look at the site's poj2409 !!
Solution 1:
# Include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> double ans; <br/> int N; <br/> int gcd (int A, int B) <br/>{< br/> If (B = 0) <br/> return; <br/> return gcd (B, A % B); <br/>}< br/> int main () <br/>{< br/> int I, j, k; <br/> while (scanf ("% d", & N) & n! =-1) <br/>{< br/> If (n = 0) <br/>{< br/> printf ("0/N "); <br/> continue; <br/>}< br/> ans = 0.0; <br/> for (I = 0; I <n; I ++) <br/> ans + = POW (3.0, gcd (n, I); // there are only three colors, almost the same as poj2409. , <Br/> If (N & 1) <br/>{< br/> for (I = 0; I <n; I ++) <br/> ans + = POW (3*1.0, (n + 1)/2 ); <br/>}< br/> else <br/> {<br/> for (I = 0; I <n/2; I ++) <br/> {<br/> ans + = POW (3*1.0, (n + 2)/2); <br/> ans + = POW (3*1.0, n/2); <br/>}< br/> printf ("%. 0lf/N ", ANS/(2 * n); <br/>}< br/> return 0; <br/>}
Solution 2: it is a method to optimize the processing, but it is best to use the first solution to improve the processing accuracy.
# Include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> # define m 25 <br/> int G [m], flag [m]; <br/> int P, ANS, N; <br/> double res = 0.0; <br/> int main () <br/>{< br/> int I, j, k; <br/> while (scanf ("% d", & n )) <br/>{< br/> If (n =-1) <br/> break; <br/> If (n = 0) <br/>{< br/> printf ("0/N"); <br/> continue; <br/>}< br/> res = 0.0; <br/> memset (G, 0, sizeof (g); <br/> for (I = 0; I <n; I ++) // This cycle contains N rotation replicas and N flip replicas. <Br/>{< br/> memset (flag, 0, sizeof (FLAG); <br/> for (j = 0; j <n; j ++) // result after each rotation and replacement, <br/> G [J] = (I + J) % N; <br/> // ans = 0; <br/> for (j = 0, ANS = 0; j <n; j ++) // calculate the cyclic section of each rotation replacement, similar to the graph for finding connected sub-accounts <br/>. <Br/> {<br/> If (flag [J] = 0) // If the J node has never been accessed, it indicates a new loop, <br/> ans ++; <br/> {<br/> ans ++; <br/> for (k = J; flag [k] = 0; k = G [k]) <br/> flag [k] = 1; <br/>}< br/> res + = POW (3*1.0, ANS); // calculates the result after rotation replacement; </P> <p> for (j = 1; (j + J) <n; j ++) // result of each flip replacement, position of symmetric point switching along the turning axis <br/> {// each rotation and replacement in the program is performed before the turning position, therefore, the same translation is used each time. <br/> the conversion process does not need to be discussed in different situations. <br/> K = G [J]; <br/> G [J] = G [n-J]; <br/> G [n-J] = K; <br/>}< br/> memset (flag, 0, sizeof (FLAG); </P> <p> for (j = 0, ANS = 0; j <n; j ++) // calculates the number of cyclic nodes for each flip replacement, which is similar to the connected sub-graph <br/>; <br/> {<br/> If (flag [J] = 0) // If the J node has not been accessed, it indicates a new loop, <br/> ans ++; <br/> {<br/> ans ++; <br/> for (k = J; flag [k] = 0; k = G [k]) <br/> flag [k] = 1; <br/>}< br/> res + = POW (3*1.0, ANS); // The result of flip replacement is added; <br/>}< br/> printf ("%. 0lf/N ", Res/(2 * n); <br/>}< br/> return 0; <br/>}< br/>