Topic Links:
http://poj.org/problem?id=1286
Main topic:
Given 3 colors of beads, the number of each color beads are unlimited, the beads are made of the length of the necklace of N.
Ask how many kinds of non-repeating necklaces can be made, and the final result will not exceed the range of int type data. and two
The necklace is the same, when and only if the two necklaces by rotating or flipping can be coincident together, and the corresponding bead
Same color.
Problem Solving Ideas:
The problem is the same as the POJ2409, except that the title specifies the number of colors.
Application of Polya theorem. First look at the Polya theorem.
Polya theorem: Set G = {A1,a2,...,ag} is a permutation group of N objects, with M color given to these n
object is shaded, the number of different shading schemes is:
| g|^ ( -1) * {m^c (A1) + m^c (A2) + ... + m^c (AG)}.
where C (AI) is the number of cyclic sections that displace AI, (i = 1,2,...,g).
To solve this problem, the Polya theorem is used to find out all the permutations, and the number of cyclic sections is calculated. And then
The sum of 3^c (AI) is calculated by the above formula, divided by the number of permutation groups.
There are two ways to replace the problem:
1. Rotate the displacement. Rotate I beads clockwise, the length of the cycle is LCM (n,i)/I, the number of links is
N/(LCM (n,i)/i), i.e. GCD (n,i).
2. Flip the permutation. It is discussed according to the parity of N.
When n is odd:
with the first bead as the vertex and center flip, after flipping, the first I beads remain unchanged, the remaining beads 22-Phase
Reciprocal exchange, because there are n beads, so there are n types of turnover, each of the number of Flip cycle is (n+1)/2.
When n is even, there are two ways of flipping:
On both sides of the opposite two beads for the axis and center flip, after flipping, the two beads remain unchanged, the remaining beads
22 reciprocal Exchange, a total of N/2 type reversal permutation, each flip cycle of the number of nodes is (n+2)/2.
With the adjacent beads in the middle line for the axis and center flip, after flipping, all beads 22 Mutual swap, a total of N/2
The number of flip loops for each type of rotation is N/2.
Then the result is obtained according to the formula of Polya theorem and the above.
Note: This problem with int word 3^18 is already out of int range, so use __int64 type.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < Cmath> #define LL __int64using namespace std; ll GCD (ll A,ll b) { if (b = = 0) return A; Return GCD (b,a%b);} int main () { LL N; while (~SCANF ("%i64d", &n) && N! =-1) { if (N <= 0) { printf ("0\n"); Continue; } LL sum = 0; for (int i = 1; I <= N; ++i) { LL tmp = GCD (n,i); Sum + = (LL) (POW (3.0,tmp*1.0)); } if (n & 1) sum + = (LL) (n * POW (3.0, (n+1)/2.0)); else { sum + = (LL) ((N/2) * POW (3.0, (n+2)/2.0)); Sum + = (LL) ((N/2) * POW (3.0, n/2.0)); } sum = sum/2/n; printf ("%i64d\n", sum); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ1286 Necklace of beads "Polya theorem"