HDU1817 Necklace of Beads

Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). If the repetitions that are produced by rotation around the center of the circular necklace or reflection to the axis of symmetry are all neglected, how many different forms of the necklace are there? 

Input
The input has several lines, and each line contains the input data n. 
-1 denotes the end of the input file. 
Output
The output should contain the output data: Number of different forms, in each line correspondent to the input data.
Sample Input
   
   
4
5
-1
Sample Output
   
   
21
39
参考了网上polya定理的课件以及别人博客对于poj同类型题目的讲解,终于明白了为什么。
参考的PPT以及文章的链接:
PPT:https://wenku.baidu.com/view/e929a04cdd36a32d72758106.html
poj2409:http://blog.csdn.net/wu_tongtong/article/details/78178926
两篇都讲得很清楚,直接贴代码了,这次学习到了polya定理
#include<stdio.h>
#include<cstring>
#include<cmath>
#define ll long long
//要是长整型才行。不然溢出了 
ll gcd(ll x,ll y)
{
	ll g = y;
	ll r = x%y;
	while(r != 0)
	{
		g = r;
		r = y%r;
		y = g;
	}
	return g;
}
ll pow_long(ll base, ll exp)
{
	ll ans= 1;
	while(exp)
	{
		if(exp & 0x1)
			ans *= base;
		base *= base;
		exp >>= 1;
	}
	return ans;
}
void polya(long long n)
{
	//不动点数 = Σ涂得颜色种类^第k种置换有的轮换的个数 
	if(n == 0)
	{
		printf("0\n");
		return;
	}
	long long a = 0;
	for(int i = 1; i<=n; i++)	//旋转i个珠子情况下每个置换有gcd(i,n)个轮换 
	{
		ll turn_cnt = gcd(n,i);
		a += pow_long(3,turn_cnt);
	}
	//翻转 
	if(n&0x1)	//奇数,n条对称轴,一个置换有1+(n-1)/2个轮换 
	{
		ll turn_cnt = 1+((n-1)/2);
		a += n*pow_long(3,turn_cnt);
	}
	else	//偶数,①n/2条对称轴,1个置换有n/2个轮换;②n/2组点构成对称轴,1个置换(n-2)/2+2个轮换
	{
		ll turn_cnt1 = n/2;
		ll turn_cnt2 = (n-2)/2 + 2;
		a += (n/2) * (pow_long(3,turn_cnt1) + pow_long(3,turn_cnt2)); 	
	} 
	printf("%lld\n",a/2/n);
}
int main()
{
	ll n;
	while(scanf("%lld",&n)!=EOF && n!=-1)
	{
		polya(n);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值