[POJ2409]Let it Bead - Polya定理

[POJ2409]Let it Bead

Time Limit: 1000MS   Memory Limit: 65536K

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

题目大意给出一串长为s的环,用c种颜色染色,可以翻转可以旋转,问有多少种不同的,即两两之间不能通过翻转或旋转得到的染色方法?


题解
看到这种题目,便想到了Polya定理(What‘s that?)。如果不知什么是置换以及Polya定理,请戳上面的链接。

先考虑旋转的置换:共n个置换对于翻转m次(翻转一次就是翻360°/n,或第k+m个元素变为第k个元素的值),一共有gcd(n,m)个循环节(1≤m≤n)我们记S1=∑sgcd(n,m) (1≤m≤n)

接着考虑翻转的置换:能发现一共也是n个置换其次可以发现,偶数和奇数翻转的情况不一样那么我们分类讨论

首先讨论最简单的奇数:那当然是从每个点对半切开

然后可以容易地发现对于每个点都有(n+1)/2个置换那么我们记S2=n*s(n+1)/2

然后是偶数的情况类似于奇数但是有两种情况1:从相对的两点切开,共n/2个置换,每个有(n+2)/2个循环节2:切开后两边都是n/2个点,共n/2个置换,每个有n/2个循环节那么我们记S2=n/2*s(n+2)/2 + n/2*sn/2=(s+1)*n/2*sn/2

根据公式可以算出最终的答案其中|G|=2n,右边括号里边的和即之前的S1+S2Ans=(S1+S2)/2n

代码如下:
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 #define LLI long long
 5 int gcd(int a,int b){
 6     return b==0?a:gcd(b,a%b);
 7 }
 8 int fastpow(int a,int b){int res=1;
 9 for(;b;b>>=1,a=a*a)if(b&1)res=a*res;return res;}
10 int main(){
11     int a,b;LLI ans;
12     while(scanf("%d%d",&a,&b)==2){
13         if(a==0&&b==0)break;
14         ans=0;
15         for(int i=1;i<=b;++i)ans+=fastpow(a,gcd(i,b));
16         if(b&1)ans+=1LL*b*fastpow(a,b+1>>1);
17         else ans+=1LL*(a+1)*(b>>1)*fastpow(a,b>>1);
18         ans/=b<<1;
19         printf("%lld\n",ans);
20     }
21     return 0;
22 }

代码写的有点丑……
时间: 2024-08-02 18:51:16

[POJ2409]Let it Bead - Polya定理的相关文章

NYOJ 280 LK的项链 &amp;&amp;POJ 2409 Let it Bead(polya 定理)

NYOJ 280 LK的项链  :click here POJ 2409 Let it Bead:click here 题意:一盒有红.蓝.绿三种颜色的珠子,每种颜色珠子的个数都大于24,现在LK想用这一盒珠子穿出一条项链,项链上的珠子个数为n(0<=n<=24),请你帮她计算一下一共可以用这一盒珠子可以穿出多少条不同的项链.通过旋转.翻转达到同一种状态的被认为是相同的项链. poj 上是c种颜色,s个珠子组成,数据比24小. 思路:今天刚接触到polya 定理: Polya定理:设G是n个对

POJ_2409 Let it Bead(Polya定理)

描述 "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,

poj2409:Let it Bead(置换群 polya定理)

题目大意:长度为n的项链,要染m种颜色,可以通过旋转或翻转到达的状态视为同一种,问有多少种染色方案. 学了一波polya定理,发现很好理解啊,其实就是burnside定理的扩展. burnside定理告诉我们不同染色方案数是每种置换的不变元素个数除以置换总数,而polya定理就是在这个基础上用公式计算出置换的不变元素个数.而且polya定理非常好理解,我们要让元素不变,所以对于每个循环节我们要染一样的颜色,有m种颜色,c(pk)个循环节,于是每种置换的不变元素个数就是m^c(pk). 对于这道题

POJ2409 Let it Bead【Polya定理】

题目链接: http://poj.org/problem?id=2409 题目大意: 给定M种颜色的珠子,每种颜色珠子的个数均不限,将这些珠子做成长度为N的项链. 问能做成多少种不重复的项链,最后的结果不会超过int类型数据的表示范围.并且两 条项链相同,当且仅当两条项链通过旋转或是翻转后能重合在一起,且对应珠子的颜 色相同. 解题思路: Polya定理的应用.先来看Polya定理. Polya定理:设 G = {a1,a2,-,ag}是 N 个对象的置换群,用 M 种颜色给这 N 个 对象着色

poj 1286 Necklace of Beads &amp; poj 2409 Let it Bead(初涉polya定理)

http://poj.org/problem?id=1286 题意:有红.绿.蓝三种颜色的n个珠子,要把它们构成一个项链,问有多少种不同的方法.旋转和翻转后相同的属于同一种方法. polya计数. 搜了一篇论文Pólya原理及其应用看了看polya到底是什么东东,它主要计算全部互异的组合的个数.对置换群还是似懂略懂.用polya定理解决问题的关键是找出置换群的个数及哪些置换群,每种置换的循环节数.像这种不同颜色的珠子构成项链的问题可以把N个珠子看成正N边形. Polya定理:(1)设G是p个对象

poj2409 polya定理经典问题

/* ID: neverchanje PROG: LANG: C++11 */ #include<vector> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cstdio> #include<set> #include<queue> #include<map> #define I

[POJ1286&amp;POJ2154&amp;POJ2409]Polya定理

Polya定理 L=1/|G|*(m^c(p1)+m^c(p2)+...+m^c(pk)) G为置换群大小 m为颜色数量 c(pi)表示第i个置换的循环节数 如置换(123)(45)(6)其循环节数为3 ------------------------------------------------------------------------------------------- POJ1286&POJ2409 都是简单的处理串珠子的问题. 题目中隐藏着3种不同的置换类别. 1.旋转 注意不

poj 1286 Necklace of Beads &amp;amp; poj 2409 Let it Bead(初涉polya定理)

http://poj.org/problem?id=1286 题意:有红.绿.蓝三种颜色的n个珠子.要把它们构成一个项链,问有多少种不同的方法.旋转和翻转后同样的属于同一种方法. polya计数. 搜了一篇论文Pólya原理及其应用看了看polya究竟是什么东东.它主要计算所有互异的组合的个数.对置换群还是似懂略懂.用polya定理解决这个问题的关键是找出置换群的个数及哪些置换群,每种置换的循环节数.像这样的不同颜色的珠子构成项链的问题能够把N个珠子看成正N边形. Polya定理:(1)设G是p

HDU 3923 Invoker 【裸Polya 定理】

参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转,翻转等.我们将每一种变换转换成一个置换群,通过置换群得到的都是等价的染色方案 最终我们要求的是非等价的染色方案数. 在Burnside定理中给出,在每一种置换群也就是等价群中的数量和除以置换群的数量,即非等价的着色数等于在置换群中的置换作用下保持不变的着色平均数. 我们以POJ 2409 Let i