Poj1286necklace of beads polyA theorem ~~

Source: Internet
Author: User
Tags cmath

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/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.