描述
"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.
输入
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.
输出
讯享网
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.
样例输入

1 1 2 1 2 2 5 1 2 5 2 6 6 2 0 0
讯享网
样例输出
讯享网1 2 3 5 8 13 21
代码如下:
1 #include <cstdio> 2 #include <cmath> 3 4 int gcd(int x, int y) { 5 return y ? gcd(y, x % y) : x; 6 } 7 int main() 8 { 9 int c,s; 10 while(scanf("%d %d",&c,&s)&&(c!=0||s!=0)){ 11 int ans=0; 12 for(int i=1;i<=s;i++){ //旋转 13 int xhj=gcd(s,i);//旋转的循环节 14 ans+=(int)pow(c*1.0,xhj*1.0); 15 } 16 if(s%2==1){ //翻转s为奇数时 17 ans+=(int)(s*pow(c*1.0,(s+1)/2*1.0));//所有s都是 18 } else{ //翻转s为偶数时 19 ans+=(int)((s/2)*pow(c*1.0,(s/2+1)*1.0));//对称轴过顶点循环节数均为s/2+1 20 ans+=(int)((s/2)*pow(c*1.0,s/2*1.0));//对称轴不过顶点循环节数均为s/2 21 } 22 ans/=(2*s); 23 printf("%d\n",ans); 24 } 25 26 return 0; 27 }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/89751.html