Necklace of Beads

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

题目大意:

n个珠子串成一个圆,用三种颜色去涂色。问一共有多少种不同的涂色方法。

不同的涂色方法被定义为:如果这种涂色情况翻转,旋转不与其他情况相同就为不同。

 

解题思路:

Polya定理模版题。

对于顺时针长度为i的旋转,为pow(3, gcd(n,i);

对于翻转,当为奇数时,有:n*pow(3, n/2+1); 

   当为偶数时,有:n/2*pow(3.0,n/2)+n/2*pow(3.0,n/2+1);

一共有2*n种情况,最后要除以2*n

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
int n;
LL Pow(LL a, LL b) // 手写long long
{
LL res = ;
for (int i = ; i < b; i++)
res *= a;
return res;
}
int gcd(int a, int b)
{
if (a == )
return b;
return gcd(b % a, a);
}
int main()
{
while (scanf("%d", &n) != EOF && n != -)
{
if (n == )
{
printf("0\n");
continue;
}
LL ans = ;
for (int i = ; i <= n; i++)
ans += Pow(, gcd(i, n)); if (n & )
{
ans += n * Pow(, (n + ) / );
}
else
{
ans += n / * Pow(3.0, n / + );
ans += n / * Pow(3.0, n / );
//ans += n / 2 * (pow(3.0, n / 2 + 1) + pow(3.0, n / 2));
}
printf("%I64d\n", ans / / n);
}
return ;
}
 

HDU 1817Necklace of Beads(置换+Polya计数)的更多相关文章

  1. hdu 2865 Polya计数+(矩阵 or 找规律 求C)

    Birthday Toy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. hdu 5868 Polya计数

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  3. hdu 5868:Different Circle Permutation 【Polya计数】

    似乎是比较基础的一道用到polya定理的题,为了这道题扣了半天组合数学和数论. 等价的题意:可以当成是给正n边形的顶点染色,旋转同构,两种颜色,假设是红蓝,相邻顶点不能同时为蓝. 大概思路:在不考虑旋 ...

  4. HDU 4633 Who's Aunt Zhang (2013多校4 1002 polya计数)

    Who's Aunt Zhang Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. Polya计数

    Let it Bead Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5365   Accepted: 3585 Descr ...

  6. 《程序设计中的组合数学》——polya计数

    我们在高中的组合数学中常常会碰到有关涂色的问题,例如:用红蓝两种颜色给正方形的四个顶点涂色,会有几种不同的方案.在当时,我们下意识的认为,正方形的四个顶点是各不相同的,即正方形是固定的.而实际上我们知 ...

  7. 组合数学及其应用——polya计数

    在处理类似下面的问题中,一般的计数方法会出现问题:假如你要用红.蓝两种颜色给一个正四面体的四个顶点着色,试问存在多少种不同的着色方案? 在高中我们常用的方法是模拟涂色过程,分情况讨论,然后基于分步乘法 ...

  8. 群论&Polya计数

    群论&Polya计数 其实在我听课的过程中,我发现针对于学习OI中的群并没有什么过多必要向内学习... 群 以后会补的. 就是\(QQ\)群. 置换 置换就是一个... \[ \begin{m ...

  9. [hdu 6184 Counting Stars(三元环计数)

    hdu 6184 Counting Stars(三元环计数) 题意: 给一张n个点m条边的无向图,问有多少个\(A-structure\) 其中\(A-structure\)满足\(V=(A,B,C, ...

随机推荐

  1. SSRS Reports 2008性能优化案例二

    前几天一同事反映海外工厂A的SSRS报表比较慢,让我检查优化一下.于是我检查了下2015-07-13到2015-07-15 12:00这段时间报表的耗时记录 USE [ReportServer];   ...

  2. SQL SERVER中关于OR会导致索引扫描或全表扫描的浅析

    在SQL SERVER的查询语句中使用OR是否会导致不走索引查找(Index Seek)或索引失效(堆表走全表扫描 (Table Scan).聚集索引表走聚集索引扫描(Clustered Index ...

  3. Mysql 安装-windows X64

    1.首先下载mysql文件包 2.将下载到的mysql-5.6.24-x64.zip进行解压. 3.安装,直接下一步. 4.进入文件夹内复制my-default.ini文件,并重命名为my.ini 5 ...

  4. SCCM 2012 R2安装部署过程和问题(一)

    在进行Windows Server 2012 R2虚拟化测试前,由于需要安装,部署和管理很多的服务器,自然会想到该如何提高效率和有效的管理.在Windows Server 2008的时代微软已经提供称 ...

  5. mac版本navicat连接oracle报错ORA-21561

    最近电脑更换成mac,很多软件都和win不一样了,正在慢慢适应,连接oracle原来用的客户端是pl/sql develop,蛋疼的是没有了mac版本, 用了navicat,具体设置如下 1.新建连接 ...

  6. Java连接SQLServer2008终极解决办法(亲身上机演练版)

    今天我一学妹问我,Java连接SQLServer2008数据库的问题,一直无法连接成功.想起自己刚开始学习的时候,在网上找各种文章,然后实际上机验证操作,花了一两天时间才搞定,一把辛酸泪呀!记得当时是 ...

  7. Cookie无法读取

    问题描述:用谷歌调试可以看到,但是用js去读取的时候,一直提示undefined

  8. [转]说说JSON和JSONP,也许你会豁然开朗,含jQuery用例

    本文转自:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临两 ...

  9. php 获取当前服务器 系统

    引子: 今天遇到一个问题,当执行文件操作是,不同系统之间的命令是不同的 , 所以需要判断当前系统. $is_win = strtoupper(substr(PHP_OS,0,3))==='WIN'?1 ...

  10. Hibernate双向多对多关联

    一.配置双向多对多关联 以Project类(项目)和Emp类(员工)为例: 1.创建Project类,并需要定义集合类型的Emp属性 public class Project { //编号 priva ...