组合数学(Pólya计数原理):UvaOJ 10601 Cubes

Cubes

You are given 12 rods of equal length. Each of them is colored in certain color. Your task is to determine in how many different ways one can construct a cube using these rods as edges. Two cubes are considered equal if one of them could be rotated and put next to the other, so that the corresponding edges of the two cubes are equally colored.

Input

The first line of input contains T (1≤T≤60), the number of test cases. Then T test cases follow. Each test case consists of one line containing 12 integers. Each of them denotes the color of the corresponding rod. The colors are numbers between 1 and 6.

Output

The output for one test consists of one integer on a line - the number of ways one can construct a cube with the described properties.

Sample Input

Sample Output

3

1 2 2 2 2 2 2 2 2 2 2 2

1 1 2 2 2 2 2 2 2 2 2 2

1 1 2 2 3 3 4 4 5 5 6 6

1

5

312120

Problem source: Bulgarian National Olympiad in Informatics 2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov, K M Hasan

 

  除去找规律的那一部分,这道题是一道好题。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=15;
 6 int a[maxn][maxn]={
 7     {12,1,1,1,1,1,1,1,1,1,1,1,1},
 8     {3,4,4,4},{6,2,2,2,2,2,2},
 9     {7,1,1,2,2,2,2,2},{4,3,3,3,3},
10 };
11 int b[maxn]={1,6,3,6,8};
12 int c[maxn];
13 
14 int DFS(int p,int id){
15     if(p>a[id][0])
16         return 1;
17     int ret=0;
18     for(int i=1;i<=6;i++)//every color
19         if(c[i]>=a[id][p]){
20             c[i]-=a[id][p];
21             ret+=DFS(p+1,id);
22             c[i]+=a[id][p];
23         }    
24     return ret;
25 }
26 
27 int main(){
28     int T,ans;
29     scanf("%d",&T);
30     while(T--){
31         memset(c,0,sizeof(c));
32         for(int i=1,x;i<=12;i++){
33             scanf("%d",&x);c[x]+=1;
34         }
35         ans=0;
36         //every kind of permutation
37         for(int i=0;i<=5;i++)
38             ans+=DFS(1,i)*b[i]; 
39         //b[i]: the number of the iTH permutation
40         printf("%d
",ans/24);
41     }
42     return 0;
43 }
原文地址:https://www.cnblogs.com/TenderRun/p/5656204.html