UVa 10733 - The Colored Cubes

10733 - The Colored Cubes

 * http://acm.uva.es/p/v107/10733.html

Summary
All 6 sides of a cube are to be colored with paints. Each side is is painted uniformly with one color. When a selection of $$n$$ different colors of paint is available, how many different cubes can you make?

Two cubes are considered different if it is not possible to rotate one cube into a such position that it appears with the same coloring as the other.

Explanation
It's a pretty straightforward problem, if you know a bit of Polya-Burnside theory of counting.

First, you need to construct the permutation group of cube's rotations. In simple terms, it's the set of ways (permutations) in which you can relabel the cube's faces, and get an equivalent cube (under rotations.)

The cube (with the initial labelling as shown in figure 1) has 24 such ways, listed in the table below. The first column shows the final labelling of the cube, and the second one gives the corresponding permutation of faces.

You can obtain all these permutations by first listing the most important ones - rotation aroung X, Y, and Z axes, and then listing all their possible combinations.

A fixed point of a permutation is some coloring, such that the permutation results in a cube, which has the same coloring. If each face of the cube may be assigned one of $$n$$ colors, and the permutation has $$c$$ disjoint cycles, then it has $$n^c$$ fixed points (the faces of each cycle have to be colored in the same color, there are $$c$$ cycles, and $$n$$ ways to choose colors for each.)

By Burnside's Lemma, the total number of distinct colorings is equal to the arithmetic mean of the number of fixed points of permutations. That is, the answer to the problem is given by:

$$\frac{n^6+3n^4+12n^3+8n^2}{24}$$

If you have never heard of Polya-Burnside theory, there are still some other methods to solve this problem.

For example, you could've guessed, that the function, giving the number of colorings is a polynomial in n; obtain its values for small n by brute force, and use interpolation to find the polynomial's coefficients.

Here's another possible solution. Start by backtracking this subproblem: there are six available paints, the i-th of which must be used to color exactly $$0 \le n_i \le 6$$ sides of the cube (of course $$n_1+\dots+n_6=6$$); how many colorings are possible? Then use well-known combinatorics (and probably, dynamic programming) to reduce the original problem to subproblems of this type.

Input
1 2 1000 0

Output
1 10 41666792167000000