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. java红黑树

    从这里学了一些知识点https://blog.csdn.net/sun_tttt/article/details/65445754,感谢作者

  2. Linux 日志分析工具(logwatch)安装及使用

    Linux 日志分析工具(logwatch)安装及使用 日志是非常重要的系统文件,管理员每天的重要工作就是分析和查看服务器的日志,判断服务器的健康状态.但是日志管理又是一项非常枯燥的工作,如果需要管理 ...

  3. adb常用操作

    1.安装程序 adb -s serialno install -r path 2.切换电源 adb -s serialno shell input keyevent 26 3.主页键 adb -s s ...

  4. 【c++ primer, 5e】函数匹配

    练习 6.49 候选函数:与所调用的函数的名字相同的函数的集合. 可行函数:给候选函数加上参数数量.参数类型的约束所得到的函数的集合. 6.50 a 3.4可行,二义匹配 b 2.4可行,2是最佳匹配 ...

  5. Web安全学习笔记之Kali部署DVWA和OWASPBWA

    0x0 前言 kali安装完成,下面要进行实战操作了,喵~~(OWASPBWA请直接跳到第八部分) #既然你诚心诚意的问了,我们就大发慈悲的告诉你! #为了防止世界被破坏! #为了守护世界的和平! # ...

  6. JPA、JTA与JMS

    三者都属于Java企业级规范 JPA(java persistence API) JPA 通过JDK5.0的注解或XML来描述 对象-关系表的映射关系,并将运行期的实体对象持久化存储到数据库中. JT ...

  7. Linux下C连接MySql数据库

    目录: 一.解决小的问题: 二.大问题,如果你不小心把/usr/lib的所属用户改了导致sudo命令用不了: 三.C连接MySql编程本身: 其实写这个程序真的很简单,十多分钟的事情,只是以前没在Li ...

  8. Servlet3.0异步请求

    在Servlet3.0之前,Servlet采用Thread-Per-Request的方式处理请求 即每次Http请求都有一个线程从头到尾负责处理 如果一个请求需要进行IO操作,比如访问数据库.调用第三 ...

  9. maven项目中使用redis集群报错: java.lang.NumberFormatException: For input string: "7006@17006"

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [redis.client ...

  10. 08_MySQL DQL_SQL99标准中的多表查询(内连接)

    # sql99语法/*语法: select 查询列表 from 表1 别名 [连接类型] join 表2 别名 on 连接条件 [where 筛选条件] [group by 分组] [having 分 ...