描述
"Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. As you can deduce from the company name, their business is beads. Their PR department found out that customers are interested in buying colored bracelets. However, over 90 percent of the target audience insists that the bracelets be unique. (Just imagine what happened if two women showed up at the same party wearing identical bracelets!) It‘s a good thing that bracelets can have different lengths and need not be made of beads of one color. Help the boss estimating maximum profit by calculating how many different bracelets can be produced.
A bracelet is a ring-like sequence of s beads each of which can have one of c distinct colors. The ring is closed, i.e. has 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 that can be made.
输入
Every line of the input file defines a test case and contains two integers: the number of available colors c followed by the length of the bracelets s. Input is terminated by c=s=0. Otherwise, both are positive, and, due to technical difficulties in the bracelet-fabrication-machine, cs<=32, i.e. their product does not exceed 32.
输出
For each test case output on a single line the number of unique bracelets. The figure below shows the 8 different bracelets that can be made with 2 colors and 5 beads.
样例输入
1 1 2 1 2 2 5 1 2 5 2 6 6 2 0 0
样例输出
1 2 3 5 8 13 21
代码如下:
1 #include <cstdio> 2 #include <cmath> 3 4 int gcd(int x, int y) { 5 return y ? gcd(y, x % y) : x; 6 } 7 int main() 8 { 9 int c,s; 10 while(scanf("%d %d",&c,&s)&&(c!=0||s!=0)){ 11 int ans=0; 12 for(int i=1;i<=s;i++){//旋转 13 int xhj=gcd(s,i);//旋转的循环节 14 ans+=(int)pow(c*1.0,xhj*1.0); 15 } 16 if(s%2==1){//翻转s为奇数时 17 ans+=(int)(s*pow(c*1.0,(s+1)/2*1.0));//所有s都是 18 } else{//翻转s为偶数时 19 ans+=(int)((s/2)*pow(c*1.0,(s/2+1)*1.0));//对称轴过顶点循环节数均为s/2+1 20 ans+=(int)((s/2)*pow(c*1.0,s/2*1.0));//对称轴不过顶点循环节数均为s/2 21 } 22 ans/=(2*s); 23 printf("%d\n",ans); 24 } 25 26 return 0; 27 }
Polya定理:
Polya定理是组合数学中十分重要的定理。用一个简单粗暴的例子粗略介绍一下~
eg:用2种颜色去染排成一个圈的6枚棋子。如果通过旋转得到只算一种,问有多少种颜色状态。
置换:用矩阵形式表示的顶点的变换。
例子中,将棋子从某个点顺时针标上1~6,则将所有棋子顺时针旋转一个位置的置换可表示为:
例子中共有6个置换:
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)
即c1=6(6个循环),c2=1(1个循环),c3=2(2个循环),c4=3(3个循环),c5=2(2个循环),c6=1(1个循环)。
循环的找法(自我理解):从第二行找1(记录)。再找第一行的1对应的第二行数字(记录)。以第一行的1对应的第二行的数字为第一行的数字,找其下第二行的数字(记录)。以此类推。
以上主要有用的数据就是分别有几个循环。
再利用下面很重要很重要很重要的公式~
用m种颜色对这N个点染色,则不同的染色方案数为:
S=(mc1+mc2+...+mc|G|)/|G|
带入公式后例题的答案:
S=(26+21+22+23+22+21)/6=14.
Polya定理在算法竞赛的应用(狠狠狠狠狠重要):
常见置换的循环数
Ø计算置换的循环数,是这一算法的瓶颈.如果能够快速计算出各置换的循环数,就可以大大提高程序的运行效率
Ø旋转:n个点顺时针(或逆时针)旋转i个位置的置换,循环数为gcd(n,i)
Ø翻转:
1.n为偶数时,
对称轴不过顶点:循环数为n/2
对称轴过顶点:循环数为n/2+1
2.n为奇数时,循环数为(n+1)/2
解题思路
题目大意:
输入:文件的每行定义了一个测试用例,并且包含两个整数:可用颜色c,接着为手镯s的长度的数目。输入由C = S= 0终止。否则,这两个都为正,并且,由于在手镯制造机技术困难,CS<= 32,即它们的产品不超过32个。
输出:有关单行每个测试用例输出独特的手镯数量。下图显示了可以与2种颜色和5珠粒制成的8个不同的手镯。
。。他喵的写到这里我才发现,好像上面的就是解题思路。本题直接套Polya定理搞定~。