百练_2409 Let it Bead(Polya定理)

描述

"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.

输出百练_2409 Let it Bead(Polya定理)

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

代码如下:

 #include <cstdio>
#include <cmath> int gcd(int x, int y) {
return y ? gcd(y, x % y) : x;
}
int main()
{
int c,s;
while(scanf("%d %d",&c,&s)&&(c!=||s!=)){
int ans=;
for(int i=;i<=s;i++){//旋转
int xhj=gcd(s,i);//旋转的循环节
ans+=(int)pow(c*1.0,xhj*1.0);
}
if(s%==){//翻转s为奇数时
ans+=(int)(s*pow(c*1.0,(s+)/*1.0));//所有s都是
} else{//翻转s为偶数时
ans+=(int)((s/)*pow(c*1.0,(s/+)*1.0));//对称轴过顶点循环节数均为s/2+1
ans+=(int)((s/)*pow(c*1.0,s/*1.0));//对称轴不过顶点循环节数均为s/2
}
ans/=(*s);
printf("%d\n",ans);
} return ;
}

Polya定理:

Polya定理是组合数学中十分重要的定理。用一个简单粗暴的例子粗略介绍一下~

eg:用2种颜色去染排成一个圈的6枚棋子。如果通过旋转得到只算一种,问有多少种颜色状态。

置换:用矩阵形式表示的顶点的变换。

例子中,将棋子从某个点顺时针标上1~6,则将所有棋子顺时针旋转一个位置的置换可表示为:

百练_2409 Let it Bead(Polya定理)

例子*有6个置换:

百练_2409 Let it Bead(Polya定理)

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定理搞定~。

上一篇:azkaban架构介绍


下一篇:FileWriter类的flush方法的作用