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
分析:
这是一道等价类计数的问题
回忆一下Burnside引理:
等价类数目等于所有置换不动点的平均数
本题有两种置换:
旋转和翻转
为了方便思考,我们把珠子按逆时针从0到n-1编上号
旋转
我们定义所有旋转都是逆时针的,这样每转动i个珠子
0,i,2i,3i….就构成一个轮换
这个轮换的元素个数是n/gcd(i,n)
因此针对“转动i个珠子”这个置换
可以看做是gcd(i,n)个轮换的乘积(每个轮换的元素个数都是n/gcd(i,n),置换中的元素个数是n,轮换的个数就很好算了)
一共有n-1个置换,
这些置换的不动点总数为翻转
需要分两种情况讨论
1.n为奇数
对称轴有n条(n个置换),每一条都穿过一个珠子,形成1个单元素轮换和(n-1)/2个双元素轮换,
一共有(n+1)/2个轮换
这些置换的不动点总数为
2.n为偶数
对称轴有两种,
一种是不穿过任何珠子的对称轴,有n/2条,形成n/2个双元素轮换
另一种是穿过两个珠子的对称轴,有n/2条,形成(n/2-1)个双元素轮换和两个单元素轮换,一共(n/2+1)个轮换,
这些置换(共n个)的不动点总数为
最后答案为
(a+b)/2n
tip
在计算旋转置换的时候
gcd是从0开始计算的
(毕竟不旋转也是一种置换,
那为什么没有“不翻转”这种置换呢,
因为“不翻转”和“不旋转”得到的置换一样,而我们的原则就是不重不漏)
//这里写代码片
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
ll pow[40];
int n,m;
int gcd(int a,int b)
{
int r=a%b;
while (r)
{
a=b;b=r;
r=a%b;
}
return b;
}
int main()
{
scanf("%d%d",&m,&n);
while (n&&m)
{
pow[0]=1;
for (int i=1;i<=n;i++)
pow[i]=pow[i-1]*m;
ll a=0;
for (int i=0;i<n;i++)
a+=pow[gcd(i,n)];
ll b=0;
if (n&1) //奇数
b=n*pow[(n+1)/2];
else b=n/2*(pow[n/2]+pow[n/2+1]);
a=(ll)(a+b)/2/n;
printf("%lld
",a);
scanf("%d%d",&m,&n);
}
return 0;
}