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. web前端基础——初识JavaScript

    1 JavaScript概述 JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚 ...

  2. Spark --【宽依赖和窄依赖】

    前言 Spark中RDD的高效与DAG图有着莫大的关系,在DAG调度中需要对计算过程划分stage,暴力的理解就是stage的划分是按照有没有涉及到shuffle来划分的,没涉及的shuffle的都划 ...

  3. linux 注销其他用户

    本文主要参考:http://ysz520020.blog.163.com/blog/static/352595722011969264745 linux kill掉其他的登录用户 : 方法一: 使用 ...

  4. 在安装好MySql后忘记root的密码,或者给root添加密码

    一.编辑MySql的配置文件:my.ini(在MySql安装目录下). 打开配置文件,在文件最后一行添加:skip-grant-tables,然后保存退出. 意思为就是在启mysql时不启动grant ...

  5. [Deep Learning]学习资料积累

    1. ufldl教程√ Andrew Ng的教程,matlab代码. 2. Neural Network and Deep Learning√: 一本未写完的书,非常细致,对基础的概念比如cross ...

  6. 【DeepLearning学习笔记】Neurons神经元

    今天找到一个比较好的deep learning的教材:Neural Networks and Deep Learning 对神经网络有详细的讲解,鉴于自己青年痴呆,还是总结下笔记吧=.= Percep ...

  7. CentOS系统下yum命令的详细使用方法

    yum是什么yum = Yellow dog Updater, Modified 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 yum特点 ...

  8. Myeclipse中java项目转换为Web项目

    https://blog.csdn.net/u010097777/article/details/51281059 这两天工作安排做一个跳转页面,不过昨天发布自己的Tomact花了不少时间,给的项目添 ...

  9. Windows下如何配置apache虚拟主机

    其实apache配置虚拟主机说简单也简单,但是就是就有几个坑,要是稍不注意就掉坑里了. --小树前言 坑三连 没遇到这三个坑,就配置得很顺畅了 用自己指定的域名进入不了任何页面. 只能进apache的 ...

  10. 【网络结构可视化】Visualizing and Understanding Convolutional Networks(ZF-Net) 论文解析

    目录 0. 论文地址 1. 概述 2. 可视化结构 2.1 Unpooling 2.2 Rectification: 2.3 Filtering: 3. Feature Visualization 4 ...