polya 定理处理的问题 :假设你是个卖项链的,你有m种颜色的珍珠,每串项链有n个珍珠,问一共能做多少种不同的项链
(因为客户不是傻子,所以项链通过旋转、反转变相同的 算同一种项链)
https://blog.csdn.net/xuzengqiang/article/details/7476671
https://blog.csdn.net/liangzhaoyang1/article/details/72639208
看的这两个博客,挺好懂的
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
int gcd(int a,int b){
return b==0 ? a : gcd(b,a%b);
}
LL pow(LL x, LL k){
if (k==0) return 1;
if (k==1) return x;
LL s = pow (x,k/2);
s = s*s;
if (k%2) s*=x;
return s;
}
int main(){
LL n, i, ans, num, m;
while (~scanf("%lld%lld",&n,&m) && n!=-1){ //m种染色方案
if (n==0) {
printf("0\n");
continue;
}
ans=0;
for (i=0; i<n; i++){
ans += pow (m, gcd (n, i));
}
if (n%2)
ans+=n*pow(m,n/2+1);
else {
ans += n/2 * pow (m,n/2);
ans += n/2 * pow (m,n/2+1);
}
printf("%lld\n",ans/(n*2));
}
return 0;
}