# UVa 10733

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

Error creating thumbnail: Unable to save thumbnail to destination
Figure 1

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.

Cube's arrangement Permutation Number of fixed points
abcdef(a)(b)(c)(d)(e)(f)n6
aecfdb(a)(bedf)(c)n3
afcebd(a)(bfde)(c)n3
bcdaef(abcd)(e)(f)n3
bedfac(abe)(cdf)n2
bfdeca(abf)(cde)n2
cdabef(ac)(bd)(e)(f)n4
ceafbd(ac)(be)(df)n3
cfaedb(ac)(bf)(de)n3
eafcbd(aeb)(cfd)n2
ebfdca(aecf)(b)(d)n3
edfbac(ae)(bd)(cf)n3
faecdb(afb)(ced)n2
fbedac(afce)(b)(d)n3
fceabd(afd)(bce)n2
fdebca(af)(bd)(ce)n3

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\leq n_{i}\leq 6$ sides of the cube (of course $n_{1}+\dots +n_{6}=6$ ); how many colorings are possible? Then use well-known combinations (and probably, dynamic programming) to reduce the original problem to sub-problems of this type.

For proof and details: http://en.wikipedia.org/wiki/Cycle_index#Case_study:_face_permutations_of_a_cube

## Input

1
2
1000
0


## Output

1
10
41666792167000000