poj 2409解题报告

来源:互联网 发布:淘宝网中老年春秋女装 编辑:程序博客网 时间:2024/06/06 08:34

 

Let it Bead
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2190 Accepted: 1348

Description

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

Input

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.

Output

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.

Sample Input

1 12 12 25 12 52 66 20 0

Sample Output

123581321
用c种颜色的珠子组成长度为s的项链问题。。polya计数经典问题,这道题出的太标准了,简直就是模板。下面的代码可以直接作为模板。
代码:
语言:c++
 

这道题,还有另一种方法就是直接套用polya公式,先生成所有的置换,每生成一种置换,然后计算每种置换的循环节数,最后通过公式求出来结果,这道题的关键是生成所有种类的置换,现在就主要讨论一下如何生成置换,因为旋转和翻转都是相同的,所以需要考虑旋转和翻转两种情况,选来看旋转的情况,任何一个项链都可以绕着它的中心旋转0个,1个,2个,..........,n-1个位置,在这样的变换下,各个点的编号换了位置,但形状和原形状相同,同样,顺时针旋转i个位置,等同于逆时针旋转n-i个位置,因此,所有这些旋转都可以用逆时针旋转来表示,于是,共有n个旋转,那么有位置0变到位置i的旋转课表示为:

0->i, 1->i+1,2->i+2,.......n-1->i+n-1,这里i是从0到n-1的。

然后说翻转,翻转一共分两种情况,n为奇数和n为偶数。

先看n为奇数的情况,一共有n中翻转,此时只有一种形式,即经过某个顶点i与中心的连线为轴的翻转:

i->i,i+1->i-1,i+2->i-2,.........(i+(n-1))%n->(i-(n-1)+n)%n

再看n为偶数的情况,在这里又分为了两种情况。

(1)这种是经过某个顶点i与中心的连线为轴的翻转,由于n为偶数,有对称性,所以此种共n/2种翻转:

i->i,i+1->i-1,i+2->i-2,.........(i+(n-1))%n->(i-(n-1)+n)%n

(2)这种是以顶点i和i+1的连线的中点与中心的连线为轴的翻转,同样,根据对称性,也有n/2种翻转:

i->i+1,i-1->i+2,i-2->i+3,..................(i-(n-1)+n)%n->(i+(n-1)+1)%n

所以给定长度n,共有2n种置换。下面是A了的代码

 

原创粉丝点击