POJ 2409-Let it Bead(Polya定理-旋轉+翻轉 串項鍊)

kewlgrl發表於2017-04-18
Let it Bead
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5895   Accepted: 3950

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

Source


題目意思:

給定m種顏色的珠子,每種顏色的珠子個數不限,將它們串成長度為n的項鍊,計算一共能做成多少種不重複的項鍊。
兩條項鍊相同,當且僅當兩條項鍊能通過旋轉或者翻轉後能重合在一起。

解題思路:

①旋轉,將項鍊順時針旋轉i格之後,其迴圈節數是gcd(n,i),計算出所有不同的著色方案;
②翻轉,不同的方法數=迴圈群個數*pow(不同顏色數,迴圈節數)之和,然後再除以所有的置換數之和(2*n),其中:
當n為奇數,共有n個迴圈節數為(n+1)/2的迴圈群;
當n為偶數,共有n/2個迴圈節數為(n+2)/2的迴圈群和n/2個迴圈節數為n/2的迴圈群。

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
int gcd(int a,int b)
{
    return (a%b!=0?(gcd(b,a%b)):b);
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("G:/cbx/read.txt","r",stdin);
    //freopen("G:/cbx/out.txt","w",stdout);
#endif
    int n,m;
    while(scanf("%d%d",&m,&n))
    {
        if(m==0&&n==0) break;
        int ans=0;
        for(int i=1; i<=n; ++i)//旋轉,計算出著色方案
        {
            int t=gcd(n,i);
            ans+=(int)(pow(m,t));
        }
        //翻轉,計算出迴圈群個數
        if(n&1)//奇數
            ans+=(int)(n*pow(m,(n+1)/2));
        else//偶數
        {
            ans+=(int)(n/2*pow(m,(n+2)/2));
            ans+=(int)(n/2*pow(m,(n)/2));
        }
        ans/=2*n;//置換群個數是2*n
        printf("%d\n",ans);
    }
    return 0;
}


相關文章