Let it Bead
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 4885 |
|
Accepted: 3229 |
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
考虑旋转和翻转,建议先做poj1286,题意我就不多做解释了。
Polya定理:
(1)设G是p个对象的一个置换群,用k种颜色给这p个对象,若一种染色方案在群G的作用下变为另一种方案,则这两个方案当作是同一种方案,这样的不同染色方案数为
(2)把长度m的手镯看作有m个珠子构成的项链,则共有m种旋转置换和m种翻转置换。
对于旋转置换:每种置换的循环节数c(fi) = gcd(m,i),(i为一次转过多少个珠子即1 <= i <= m)
对于翻转置换:如果m为奇数,共有m种翻转置换,每种置换的循环节数均为c(f) = m/2 + 1;
如果m为偶数,分两种情况 <1> 从空白处穿对称轴,则轴两边各有m/2个对象,得到c(f) = m/2;
<2> 从两个对象上穿对称轴,则轴两边各有m/2-2个对象,得到c(f) = m/2 + 1。
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#define LL long long
int dp[33][33];//dp[i][j]表示 用i种颜色 能组成多少个长度为j的手镯
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a%b);
}
int sum(int n, int m)//求用n种颜色组成 长度为m 的方案数
{
int ans = 0;
/*m次旋转置换*/
for(int i = 1; i <= m; i++)
ans += pow(n*1.0, gcd(i, m));
/*m次翻转置换*/
ans += m&1? pow(n*1.0, m/2+1)*m: (pow(n*1.0, m/2+1)+pow(n*1.0, m/2))*m/2;
ans /= 2*m;
return ans;
}
void getdp()
{
int i, j;
for(i = 1; i <= 32; i++)
{
for(j = 1; j <= 32 && i*j <= 32; j++)
{
dp[i][j] = i==1 ? i : sum(i,j);
}
}
}
int main()
{
getdp();
int c, s;
while(scanf("%d%d", &c, &s), c||s)
{
printf("%d\n", dp[c][s]);
}
return 0;
}