poj2409--Let it Bead(置换群+polya奇数)

题目链接:点击打开链接

题目大意:给出m种颜色的小球,现在要求用n个串成一个环,经过旋转翻转后,能形成多少个不同的环。

参考:点击打开链接


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
#define LL __int64
LL gcd(LL a,LL b) {
    return b == 0 ? a : gcd(b,a%b) ;
}
LL pow(LL x,LL k) {
    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 , m , i , ans , num ;
    while( scanf("%I64d %I64d", &m, &n) && n+m != 0 ) {
        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("%I64d\n", ans/(n*2)) ;
    }
    return 0 ;
}


你可能感兴趣的:(poj2409--Let it Bead(置换群+polya奇数))