poj 2409解題報告

 

Let it Bead
Time Limit: 1000MS   Memory Limit: 65536K
Total 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 1
2 1
2 2
5 1
2 5
2 6
6 2
0 0

Sample Output

1
2
3
5
8
13
21
用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了的代碼

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章