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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<map> 5 #include<set> 6 #include<vector> 7 using namespace std; 8 #define ll long long 9 ll pow_mod(ll a,ll i){ 10 if(i==0) 11 return 1; 12 ll t=pow_mod(a,i/2); 13 ll ans=t*t; 14 if(i&1) 15 ans=ans*a; 16 return ans; 17 } 18 19 vector<ll> divisor(ll n){ 20 vector<ll> res; 21 for(ll i=1;i*i<=n;i++){ 22 if(n%i==0){ 23 res.push_back(i); 24 if(i*i!=n){ 25 res.push_back(n/i); 26 } 27 } 28 } 29 return res; 30 } 31 32 ll eular(ll n){ 33 ll res=1; 34 for(ll i=2;i*i<=n;i++){ 35 if(n%i==0){ 36 n/=i,res*=i-1; 37 while(n%i==0){ 38 n/=i; 39 res*=i; 40 } 41 } 42 } 43 if(n>1) res*=n-1; 44 return res; 45 } 46 47 ll polya(ll m,ll n){ 48 vector<ll> divs = divisor(n); 49 ll res=0; 50 for(ll i=0;i<divs.size();i++){ 51 ll euler=eular(divs[i]); 52 res+=euler*pow_mod(m,n/divs[i]); 53 } 54 res/=n; 55 return res; 56 } 57 58 int main() 59 { 60 ll n,m; 61 while(scanf("%I64d%I64d",&m,&n)==2 && n+m!=0){ 62 ll ans=polya(m,n)*n;//旋转情况 63 if(n&1){//奇数 64 ans+=n*pow_mod(m,n/2+1);//翻转情况 65 } 66 else{//偶数 67 ans += (pow_mod(m, n / 2 + 1) + pow_mod(m, n / 2)) * (n / 2);//翻转情况 68 } 69 ans/=2*n; 70 printf("%I64d\n",ans); 71 } 72 return 0; 73 }
暴力枚举k
1 #include <iostream> 2 using namespace std; 3 4 #define LL long long 5 6 int gcd(int a, int b) 7 { 8 return b == 0 ? a : gcd(b, a % b); 9 } 10 11 LL power(LL p, LL n) 12 { 13 LL sum = 1; 14 while (n) 15 { 16 if (n & 1) 17 sum *= p; 18 p *= p; 19 n /= 2; 20 21 } 22 return sum; 23 } 24 25 ///////////////////////////SubMain////////////////////////////////// 26 int main() 27 { 28 29 LL n; LL m; 30 while (~scanf("%I64d%I64d", &m,&n) && n+m!=0) 31 { 32 LL count = 0; 33 for (int i = 1; i <= n; ++i) 34 count += power(m, gcd(i, n)); 35 if (n & 1) 36 count += n * power(m, n / 2 + 1); 37 else 38 count += n / 2 * (power(m, n / 2 + 1) + power(m, n / 2)); 39 count /= n * 2; 40 printf("%lld\n", count); 41 } 42 43 return 0; 44 }
原文:http://www.cnblogs.com/UniqueColor/p/4776588.html