Let it Bead
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6870 | Accepted: 4565 |
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
题意:手镯是由若干珠子组成,旋转、翻转后看作相同,给你C种颜色的S颗珠子,每种颜色的珠子可以无限次使用,但是珠子的总数必须为S,问你可以制作出多少种不同的手镯。
题解:既然题目说手镯可以旋转、翻转。那我们分别考虑旋转和翻转。
旋转:如果逆时针旋转 i 颗珠子,那么珠子 0, i, 2i, ...构成一个循环,循环中一共有 n / gcd( i, s)个元素,因为你每次要旋转 i 个,
而且要旋转到 s ,所以等价于每次旋转 gcd(i, s) 个,所以循环中一共有 s / gcd( i, s)个元素。用 s 除以循环中的元素个数,就能得到有多少的循环。LCM(i, s)/i = i * s/GCD(i, s) / i = s / GCD(i, s)。 这些置换不动点的个数 a = c ^ gcd(0, s) + c ^ gcd(1, s) + c ^ gcd(2, s) + c ^ gcd(3, s) + ... c ^ gcd(s-1, s)。
翻转:翻转就是考虑对称了,分为奇数颗珠子和偶数颗珠子两种情况 。
1:奇数,一共有 s 条对称轴,每条对称轴形成 (s-1) / 2 个长度为 2 的循环和 1 个长度为 1 的循环,即
(s+1) / 2 个循环,不动点个数为 b = s * (c ^ ( ( s + 1) / 2 ) ) 。
2:偶数,偶数情况下还要再分两种,穿过珠子的对称轴有 s / 2 条,形成 s / 2 -1 个长度为 2 的循环和 2 个长度为 1 的循环。不穿过珠子的对称轴有 s / 2 条,形成 s / 2个长度为 2 的循环。这些置换的不动点个数为
b = s / 2 *( c ^ ( s / 2+1) + c ^ ( s / 2 ) )。
由 polya 定理,手镯总数为 (a + b) / 2s
#include<cstdio>
using namespace std;
const int maxn=100+7;
typedef long long ll;
int gcd(int a,int b)
{
if(b==0) return a;
int t;
while(b){
t=a;
a=b;
b=t%b;
}
return a;
}
int main()
{
int c,s;
while(scanf("%d %d",&c,&s)==2&&c&&s){
ll pow[maxn];
pow[0]=1;
for(int i=1;i<=s;i++) pow[i]=pow[i-1]*c;
ll a=0;
for(int i=0;i<s;i++) a+=pow[gcd(i,s)];
if(s%2==1) a+=s*(pow[(s+1)/2]);
else a+=s/2*(pow[(s+2)/2]+pow[s/2]);
printf("%lld\n",a/2/s);
}
return 0;
}