Let it Bead polya计数

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.

长度为n的圆环,用c种颜色染色,问有多少种不同的染色法。其中,可通过旋转和翻转得到的视为同一种。旋转的循环节数位gcd;翻转分成奇偶讨论,基数是每条边的中垂线,循环节为(n+1)/2。偶数是1、对称点,循环节为(n+2)/2,还有对边的中垂线,有n/2个循环节。

这里求阶乘用了快速幂。数据类型为longlong

#include<stdio.h>
using namespace std;
long long M,N;
long long gcd(long long x,long long y){
    return y==0? x : gcd(y,x%y);
}
long long fast_pow(long long m,long long n){
    long long ans=1;
    for(;n;n>>=1,m=m*m)
        if(n&1)
            ans=ans*m;
    return ans;
}
int main(){
    while(~scanf("%d%d",&M,&N)&&(M||N)){
        long long sum=0;
        for(int i=1;i<=N;i++){
            long long temp=gcd(N,i);
            sum+=fast_pow(M,temp);
        }
        if(N&1){
            sum+=N*fast_pow(M,(N+1)/2);
        }
        else{
            sum+=(N/2)*(fast_pow(M,(N+2)/2));
            sum+=(N/2)*(fast_pow(M,N/2));
        }
        sum=sum/(2*N);
        printf("%lld\n",sum);
    }
    //system("pause");
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注