poj_2409 Let It Bead (Polya theorem)

Source: Internet
Author: User

Describe

Let it bead located upstairs at the Cannery Row in Monterey, CA. As can deduce from the company name, their business is beads. Their PR department found out that customers is interested in buying colored bracelets. However, over-percent of the target audience insists that the bracelets is unique. (Just Imagine what happened if-women showed up on the same party wearing identical bracelets!) It's a good thing that bracelets can has different lengths and need not being made of beads of one color. Help the boss estimating maximum profit by calculating what many different bracelets can be produced. 

A Bracel ET is a ring-like sequence of s beads each of the which can have one of the C distinct colors. The ring is closed, i.e. have no beginning or end, and has no direction. Assume an unlimited supply of beads of each color. For different values of S and C, calculate the number of different bracelets so can be made.

Input

Every line of the input file defines a test case and contains-integers:the number of available colors C followed by t He length of the bracelets s. Input is terminated by c=s=0. Otherwise, both is positive, and, due to technical difficulties in the Bracelet-fabrication-machine, cs<=32, i.e. thei R product does not exceed 32.

Output

For each test, the output on a, the number of the unique bracelets. The figure below shows the 8 different bracelets The can is made with 2 colors and 5 beads.

Sample input

1 12 12 25 12 52 66 20 0

Sample output

123581321

The code is as follows:

1#include <cstdio>2#include <cmath>3 4 intgcdintXinty) {5     returnY? GCD (y, x%y): x;6 }7 intMain ()8 {9     intc,s;Ten      while(SCANF ("%d%d", &c,&s) && (c!=0|| s!=0)){ One         intans=0; A          for(intI=1; i<=s;i++) {//Rotate -             intXHJ=GCD (S,i);//rotation of the cycle section -ans+= (int) Pow (c*1.0, xhj*1.0); the         } -         if(s%2==1){//when flipping s is odd -ans+= (int) (S*pow (c*1.0, (s+1)/2*1.0));//all S are -}Else{//when flipping s is even +ans+= (int) ((s/2) *pow (c*1.0, (s/2+1)*1.0));//the number of cyclic nodes of the axis of symmetry is s/2+1 -ans+= (int) ((s/2) *pow (c*1.0, s/2*1.0));//axis of symmetry However, the number of vertex cycle nodes are S/2 +         } AAns/= (2*s); atprintf"%d\n", ans);  -     } -      -     return 0; -}

Polya theorem:

Polya theorem is a very important theorem in combinatorial mathematics. A rough introduction to a simple rough example ~

Eg: use 2 colors to dye the 6 pieces in a circle. If only one is calculated by rotation, ask how many color states.

Displacement: The transformation of a vertex expressed in matrix form.

example, a pawn is 1~6 from a point clockwise, and the displacement of all pieces clockwise to one position can be expressed as:

There are 6 permutations in the example:

C1 (1) (2) (3) (4) (5) (6) C2 (1 6 5 4 3 2 1)

C3 (1 5 3 1) (2 6 4) C4 (1 4) (2 5) (3 6)

C5 (1 3 5) (2 4 6) C6 (1 2 3 4 5 6)

That is c1=6 (6 loops), c2=1 (1 loops), c3=2 (2 loops), c4=3 (3 loops), c5=2 (2 loops), c6=1 (1 loops).

Round robin (self-understanding): Look for 1 ( record ) from the second line. Then find the first line of the 1 corresponding to the second row of numbers ( records ). The number of the second row that corresponds to the first row of 1 is the number of the first row, and the number ( record ) of the second row is found. And so on

The main useful data above is that there are several loops respectively.

Use the following very important and important formula ~

These n points are stained with M color, and the number of different staining schemes is:

s= (mc1+mc2+...+mc| g|) /| G|

answer to the example after the formula is brought in:

s= (26+21+22+23+22+21)/6=14.

The application of Polya theorem in algorithm competition (ruthless and fiercely important):

number of cycles for common permutations

Ø Calculating the number of permutations is the bottleneck of this algorithm. If the number of cycles of each permutation can be calculated quickly, the running efficiency of the program will be greatly improved.

Ø Rotation: N points clockwise (or counterclockwise) to rotate the displacement of position I, the number of cycles is gcd (n,i)

Ø Flip:

When the 1.N is even,

axis of symmetry but vertex: the number of loops is N/2

Axis of symmetry over vertex: number of loops is n/2+1

When 2.N is odd, the number of cycles is (n+1)/2

Thinking of solving problems

Main topic:

Input: Each line of the file defines a test case and contains two integers: the number of available color C, followed by the length of the bracelet s. The input is terminated by C = s= 0. Otherwise, both are positive and, because of the technical difficulties in the bracelet making machine, cs<= 32, that is, their products no more than 32.

output: A unique number of bracelets for each test case output for a single line. Shows 8 different bracelets that can be made with 2 colors and 5 beads.  

.. He meow wrote here I found, as if the above is a problem-solving ideas. The subject directly sets Polya theorem to settle ~.

poj_2409 Let It Bead (Polya theorem)

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.