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. ORACLE触发器判断是否更新了某个字段

           今天遇到一个有意思的小问题,一同事有这样一个需求:在更新表T时,如果只是更新字段C,那么不希望触发该该表的触发器去处理业务逻辑.即想在触发器中做出判断:如果更新了字段C,那么跳出触发器, ...

  2. SYMBDSNAP_SDK[3943]: Failed to open device: /dev/symbdsnapctl, errno: 2

    在Linux的日志/var/log/message里面看到下面错误信息: Oct 26 09:48:42 xxxxxxx SYMBDSNAP_SDK[3943]: Failed to open dev ...

  3. SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_job_id"

    案例环境: 数据库版本: Microsoft SQL Server 2005 (Microsoft SQL Server 2005 - 9.00.5000.00 (X64) ) 案例介绍: 对一个数据 ...

  4. stm32 UART串口

    void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò { u8 Res; #ifdef OS_TICKS_PER_SEC //Èç¹ûʱÖÓ½ÚÅÄÊý¶ ...

  5. 一个新人眼中的O2O

    O2O是近几年来电商行业颇为受宠的方向.就其所代表的意思,线上线下,大部分人都耳熟能详.然而对于这样一个行业,很多人都有附势之嫌.最终是人力物力出尽,效果不佳.一直以来,喜欢互联网,梦想创业,梦想在互 ...

  6. python 聊天室

    server端程序 # -*- coding: utf-8 -*- #!/usr/bin/python """ """ import soc ...

  7. python3 下的文件输入输出特性以及如何覆盖文件内容和接下去输入

    今天碰到了一个非常有意思的python特性.本来我是想打开一个文件,在文件的末尾接下去输入一些内容的,代码如下: f = open('test.txt', 'r+') f.write(content) ...

  8. SQL-语句实现九九乘法表

    下面用while 和 if 条件写的SQL语句的四种九九乘法表  sql语句实现--x 左下角九九乘法表 DECLARE @I INT ,@J INT,@S VARCHAR(100) SET @I=1 ...

  9. AC日记——最大数 洛谷 P1198 [JSOI2008]

    题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前数列的长度. 2. 插入操作 ...

  10. 深入理解Java之泛型

    原文出处: absfree 1. Why ——引入泛型机制的原因 假如我们想要实现一个String数组,并且要求它可以动态改变大小,这时我们都会想到用ArrayList来聚合String对象.然而,过 ...