poj 1286 Necklace of Beads(polya 定理)

本文探讨了由红、蓝、绿三种颜色珠子组成的圆形项链的不同形态计数问题,在忽略旋转和反射产生的重复形态后,如何计算不同形式的项链数量。通过具体的输入输出示例,介绍了计算方法,并给出了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Necklace of Beads
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7571 Accepted: 3152

Description

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题解:与poj 2409类似。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long 
using namespace std;
LL ans,n,m;
LL gcd(LL x,LL y)
{
	LL r;
	while (y){
		r=x%y;
		x=y;
		y=r;
	}
	return x;
}
LL pow(LL num,LL x)
{
	LL base=num; LL ans=1;
	while (x){
		if (x&1)  ans=ans*base;
		x>>=1;
		base=base*base;
	}
	return ans;
}
int main()
{
	n=3;
	while (scanf("%lld",&m)!=EOF){
		if (m==0){
			printf("0\n");
			continue;
		}
		if (m==-1)  break;
		ans=0;
		for (LL i=1;i<=m;i++)
		 ans+=pow(n,gcd(m,i));
		if (m%2)  ans+=pow(n,m/2+1)*m;
		else ans+=pow(n,m/2+1)*(m/2),
		     ans+=pow(n,m/2)*(m/2);
		printf("%lld\n",ans/(m*2));
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值