poj 2409、1286 Let it Bead

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


 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <string>
 5 using namespace std;
 6 __int64 num[33];
 7 int c,s;
 8 void init()
 9 {
10     num[0]=1;
11     for(int i=1;i<=s;i++)
12     {
13         num[i]=num[i-1]*c;
14     }
15     return;
16 }
17 int gcd(int a,int b)
18 {
19     return b?gcd(b,a%b):a;
20 }
21 
22 int main()
23 {
24     while(~scanf("%d%d",&c,&s))
25     {
26         if(c==0 && s==0)
27         {
28             break;
29         }
30         init();
31         __int64 ans=0;
32         for(int i=1;i<=s;i++)
33         {
34             ans+=num[gcd(i,s)];
35         }
36         if(s%2)
37         {
38             ans+=s*num[(s+1)/2];
39         }
40         else
41         {
42             ans+=s/2*(num[s/2]+num[s/2+1]);
43         }
44         printf("%I64d
",ans/(2*s));
45     }
46     return 0;
47 }
View Code

poj2409 和poj1286这两个题目意思差不多,解法也是一样。

两个题目考察的是polya计数法,具体去看黑书,其实我也没怎么弄明白,有些地方也还是没搞懂,就拿翻转对称来说,当s为偶数的时候,为什么会是那样的表达式,确实有疑问,不过现在也只能先练熟模版,遇到类似的题目可以有办法解决。

原文地址:https://www.cnblogs.com/ouyangduoduo/p/3199996.html