POJ 1286 Necklace of beads

Source: Internet
Author: User
Tags cmath

Necklace of Beads

Test instructions: In three colors to the length of N (n < 24) ring bracelet coloring, if can be rotated or flipped to be expressed as the same type, ask how many different coloring scheme?

Idea: Pure equivalence class calculation problem;

Emphasis: Conversion of rotation and rollover to displacement operations;

Rotation: The length of the interval is enumerated, that is 0 <= i < n, so that the cyclic section is N/GCD (i,n), directly into the power of 3 to sum;

Flip: The odd even, and then find the number of symmetric axis and the number of cycles in each case can be;

The above-calculated a+b is just the sum of the number of fixed points, the final divided by the total number of permutations that is 2n;

#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1typedef __int64 Ll;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &B) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void  out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');} ll f[ -],ans[ -];template<typename t>intGCD (T a,t b) {returnB?GCD (b,a%b): A;}intMain () {f[0] =1; REP1 (i,1, -) F[i] = f[i-1]*3; REP1 (i,1, -) {ll a=0, B =0; Rep0 (J,0, i) A + =F[GCD (i,j)]; if(I &1) b = i*f[(i+1)/2]; Else{b= i/2* (f[i/2] + f[(i+2)/2]); } Ans[i]= (A + b)/2/i; }    intm;  while(Read1 (m), M >=0){         out(Ans[m]);p UTS (""); }    return 0;}
View Code

The same type of title: Bracelet operation, just enter the number of colors after the first number of beads;

#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1typedef __int64 Ll;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void  out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');} ll f[ +];template<typename t>intGCD (T a,t b) {returnB?GCD (b,a%b): A;}intMain () {intS,c;  while(Read2 (c,s), S +c) {f[0] =1; REP1 (i,1, s) f[i] = f[i-1]*C; ll a=0, B =0; Rep0 (J,0, s) A + =F[GCD (s,j)]; if(S &1) b = s*f[(s+1)/2]; Elseb= s/2* (f[s/2] + f[(s+2)/2]);  out((A + B)/2/s); Puts (""); }    return 0;}
View Code

POJ 1286 Necklace of beads

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.