题目链接:http://poj.org/problem?id=1286
题目大意:正n边形n个顶点用3种颜色染色有多少种方法,对称旋转可得到的算同一种方法
解题思路:polay计数,不懂polya计数,没看懂,暂且记下代码
直接使用polay计数:
///2014.6.23
///poj1286
/*
*polya计数,不懂,但是记住了
*/
#include <cstdio>
#include <math.h>
#define LL long long
LL gcd(LL a,LL b){
return b ? gcd(b,a%b) : a ;
}
LL polya(LL n){
LL ret = 0 ;
for(LL i=0 ; i<n ; i++)
ret += pow(3,gcd(i,n));
if( n & 1 )
ret += n * pow(3,n/2+1);
else
ret += n/2 * ( pow(3,n/2) + pow(3,n/2+1) );
return ret/2/n;
}
int main(){
// freopen("in","r",stdin);
// freopen("out","w",stdout);
LL n;
while( ~scanf("%lld",&n) && (n+1) ){
if( n<=0 )
printf("0\n");
else
printf("%lld\n",polya(n) );
}
return 0;
}
因为有效的数据只有从0到23,所以可以打表:
///2014.6.23
///poj1286
/*
*打表
*/
#include <cstdio>
int ret[24] = {0,3,6,10,21,39,92,198,498,1219,
3210,8418,22913,62415,173088,
481598,1351983,3808083,10781954,
30615354,87230157,249144711,
713387076,2046856566};
int main(){
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int n;
while( ~scanf("%lld",&n) && (n+1) ){
if( n<0 )
printf("0\n");
else
printf("%d\n",ret[n] );
}
return 0;
}