POJ1286,2409——Let it Bead,Necklace of Beads( Pólya定理)

Necklace of Beads
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5737Accepted: 2367
Description

Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). If the repetitions that are produced by rotation around the center of the circular necklace or reflection to the axis of symmetry are all neglected, how many different forms of the necklace are there? 


Input
The input has several lines, and each line contains the input data n. 
-1 denotes the end of the input file. 
Output
The output should contain the output data: Number of different forms, in each line correspondent to the input data.
Sample Input
4
5
-1
Sample Output
21
39
Source
Xi'an 2002


Let it Bead
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 3802
Accepted: 2477
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
Ulm Local 2000

解析:

         這兩道題都是很裸的polya定理題,而且代碼幾乎一樣,所以把他們放到一起。。。

         至於polya定理的理論部分,可以看我的上一篇blog。。。

         這兩道題:

         對於旋轉的情況:共有n個置換,其中旋轉k個位置的置換的循環節數爲gcd(n,k)。——(|)證明如下
         對於翻轉的情況:若n爲奇數,則對稱軸過一頂點和一邊中點,n種置換,循環節長度n/2+1;若n爲偶數,對稱軸有兩種,過兩點和過兩邊中點,兩者各有n/2種置換,前者          循環節長度爲n/2+1,後者爲n/2。

         總共有2*n個置換。

         這部分也很好理解。。。接下來我要來證明一下(|)。。。

          假設旋轉k個位置,因爲polya定理要求在置換後元素不變的情況下每種置換的循環節數,所以當他旋轉到他原來的位置時,

          所經過的點數既是n的倍數,又是k的倍數,且是n和k的最小公倍數,即lcm(n,k);

          因爲每次旋k個位置,所以當前置換中循環的個數爲lcm(n,k)/k;

          所以每個置換的循環節數=n/(lcm(n,k)/k)=gcd(n,k)。

代碼:

poj1286

#include<cstdio>
#include<cmath>
using namespace std;
int n;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

int polya(int n)
{
    if(n==0)return 0;
    double ans=0;
    for(int i=1;i<=n;i++)ans+=pow(3.0,gcd(n,i));
    if(n&1)ans+=n*pow(3.0,n/2+1);
    else ans+=n/2*(pow(3.0,n/2)+pow(3.0,n/2+1));
    return ans/(2*n);
}

int main()
{
    freopen("poj1286.in","r",stdin);
    freopen("poj1286.out","w",stdout);
    while(scanf("%d",&n))
    {
        if(n==-1)break;
        printf("%d\n",polya(n));
    }
    return 0;
}
poj2409

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

int polya(int n,int m)
{
    int ans=0,i;
    for(i=1;i<=n;i++)ans+=pow(m*1.0,gcd(n,i));
    if(n&1)ans+=n*pow(m*1.0,n/2+1);
    else ans+=n/2*(pow(m*1.0,(n/2+1))+pow(m*1.0,n/2*1.0));
    return ans/(2*n);
}

int main()
{
    freopen("poj2409.in","r",stdin);
    freopen("poj2409.out","w",stdout);
    while(scanf("%d%d",&m,&n)&&n&&m)printf("%d\n",polya(n,m));
    return 0;
}



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