Necklace of Beads

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 630    Accepted Submission(s): 232

Problem Description
Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 40 ). 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
Recommend
We have carefully selected several similar problems for you:  2481 3547 1812 1465 1398 
 
题解:

这道题和POJ2409是一样的题目,只不过这道题规定了颜色数目。

Polya定理的应用。先来看Polya定理。

Polya定理:设 G = {a1,a2,…,ag}是 N 个对象的置换群,用 M 种颜色给这 N 个对象着色,则不同的着色 方案数为:

|G|^(-1) * {M^c(a1) + M^c(a2) + … + M^c(ag)}。

其中 c(ai)为置换 ai 的循环节数,( i = 1,2,…,g )。

对于这道题,直接用Polya定理求解,找出所有的置换,并求出置换的循环节数。然后根据上边公式求出 3^c(ai) 的总和,再除以置换群个数。

题中有两种置换方式:

1.旋转置换。分别顺时针旋转 i 个珠子,其循环节长度为 LCM(N,i) / i,循环节数为

N / (LCM(N,i) / i),即 GCD(N,i)。

2.翻转置换。根据 N 的奇偶性分情况讨论。

N为奇数时:

以第 i 个珠子为顶点和中心翻转,翻转后,第 i 个珠子保持不变,其余珠子两两相互对换,因为有 N 个珠子,所以有 N 种翻转置换,每种翻转循环节数为 (N+1) / 2。

N为偶数时,有两种翻转方式:

以两边相对的两个珠子为轴和中心翻转,翻转后,这两个珠子保持不变,其余珠子两两相互对换,共有 N/2 种翻转置换,每种翻转循环节数为 (N+2) / 2。

以相邻的珠子中间连线为轴和中心翻转,翻转后,所有珠子两两相互对换,共有 N/2种翻转置换,每种翻转循环节数为 N/2。

注: 用long long 或__int64定义,本题的n可能是0,所以刚开始错误是RE,要特殊判断。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std;
long long res,n;
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;
}
long long poww(long long a,long long b)
{
long long ans=;
while(b)
{
if (b%==) ans*=a;
a*=a;
b/=;
}
return ans;
}
int main()
{
while(~scanf("%lld",&n))
{
if (n==-) break;
if(n<=)
{printf("0\n"); continue;} res=;
for(long long i=;i<=n;i++)
res+=poww((long long),gcd(n,i));
if(n%==)
res+=poww((long long),(n+)/)*n;
else
{
res+=poww((long long),n/+)*(n/);
res+=poww((long long),n/)*(n/);
}
printf("%lld\n",res/(*n));
}
return ;
}

转自:http://blog.csdn.net/lianai911/article/details/48271557

hdu 1817 Necklace of Beads(Polya定理)的更多相关文章

  1. hdu 1817 Necklace of Beads (polya)

    Necklace of Beads Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. poj1286 Necklace of Beads—— Polya定理

    题目:http://poj.org/problem?id=1286 真·Polya定理模板题: 写完以后感觉理解更深刻了呢. 代码如下: #include<iostream> #inclu ...

  3. Necklace of Beads(polya定理)

    http://poj.org/problem?id=1286 题意:求用3种颜色给n个珠子涂色的方案数.polya定理模板题. #include <stdio.h> #include &l ...

  4. POJ1286 Necklace of Beads(Polya定理)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9359   Accepted: 3862 Description Beads ...

  5. poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)

      Description Beads of red, blue or green colors are connected together into a circular necklace of ...

  6. Necklace of Beads(polya计数)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 3102 ...

  7. HDU 3923 Invoker 【裸Polya 定理】

    参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...

  8. Necklace of Beads (polya定理的引用)

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...

  9. POJ 1286 Necklace of Beads(Polya定理)

    点我看题目 题意 :给你3个颜色的n个珠子,能组成多少不同形式的项链. 思路 :这个题分类就是polya定理,这个定理看起来真的是很麻烦啊T_T.......看了有个人写的不错: Polya定理: ( ...

随机推荐

  1. SqlHelper简单实现(通过Expression和反射)10.使用方式

    以下是整个SqlHelper的Demo: public Result<List<ArticleDTO>> GetIndexArticleList(int count, int ...

  2. Tasks in parallel

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. 51nod 1391 01串(hash+DP)

    题目链接题意:给定一个01串S,求出它的一个尽可能长的子串S[i..j],满足存在一个位置i<=x <=j, S[i..x]中0比1多,而S[x + 1..j]中1比0多.求满足条件的最长 ...

  4. $Python常用内置函数典型用法

    Python中有许多功能丰富的内置函数,本文基于Python 2.7,就常用的一些函数的典型用法做一些积累,不断更新中. sorted函数的三种用法 # coding:utf-8 # sorted函数 ...

  5. centos配置jdk的环境变量

    1.首先呢,centos下的JDK环境配置分两种情况,一直是root用户级别的jdk配置,另一种是其他用户组级别的配置.这里讲解的是root用户级别的配置. 我们已经下载解压好了jdk的目录.如下 2 ...

  6. ES6 利用 Set 数组去重法

    例子: const set = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => set.add(x) ); const arr = [...set]; ...

  7. selenium 之定位方法

    1 id 定位 driver.find_element_by_id() HTML 规定id 属性在HTML 文档中必须是唯一的.这类似于公民的身份证号,具有很强的唯一性 from selenium i ...

  8. [NOI2014]动物园(kmp)

    题目 https://www.luogu.org/problemnew/show/P2375 做法 查找多少个前缀与后缀配对,其实就是\(fail\)树的深度 而不可重叠,其实\(i\)不可用的,\( ...

  9. 20145310 《Java程序设计》第7周学习总结

    20145310 <Java程序设计>第7周学习总结 教材学习内容总结 本周主要进行第十二章和第十三章的学习. Lambda定义:一个不用被绑定到一个标识符上,并且可能被调用的函数. 在只 ...

  10. 20145314郑凯杰 《Java程序设计》实验四 实验报告

    20145314郑凯杰 <Java程序设计>实验四 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...