POJ 2409 Let it Bead ACM Polya

来源:互联网 发布:日本剪发多少钱 知乎 编辑:程序博客网 时间:2024/06/06 00:23

POJ 2409 Let it Bead ACM

题目

    Time Limit: 1000MS      Memory Limit: 65536K    Total Submissions: 5232     Accepted: 3498

Problem 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

AABCD
CDAA
ASD
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
Alt text

题解

题目意思就是用c种颜色给n个珠子上色,通过旋转和翻转得到相同的算一种着色方式,问有多少种上色方式。

分析

这道题主要运用polya定理。

Polya定理

设G设 是n个对象的一个置换群,即有多少种置换方式, 用m种颜色染图这n个对象,则不同的染色方案数为:
Alt text
其中 , Alt textAlt text的循环节数

代码

#include <iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>using namespace std;typedef long long ll;int gcd(int a,int b){    return b?gcd(b,a%b):a;}ll polya(int c,int n){    int i;    ll ans=0;    //旋转置换,共n种,每种旋转i个格,循环节有gcd(n,i)个    for(i=0; i<n; i++)    {        ans+=(ll)pow(1.0*c,gcd(n,i));    }    if(n&1) //奇数翻转,沿每个点同中心点连线翻转共n种,    {        //循环节有(n-1)/2+1        ans+=n*(ll)pow(1.0*c,(n+1)/2);    }    else //偶数翻转    {        ans+=n/2*(ll)pow(1.0*c,n/2);//两个相邻点中点和圆心        //的连线也是n/2条,循环节的个数是n/2        ans+=n/2*(ll)pow(1.0*c,n/2+1);//对称轴是每个点和对        //面的点的连线,共n/2条,循环节的个数是 (n-2)/2+2    }    return ans/(2*n);}int main(){    int c,n;    while(scanf("%d %d",&c,&n),n+c)    {        printf("%d\n",polya(c,n));    }    return 0;}

问题与建议

  • 微博:@妥妥
  • 邮箱:iyatuo@gmail.com
0 0
原创粉丝点击