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. iis6上使用.htaccess文件,ISAPI_Rewrite的安装及使用

    在Apache上很有用的.htaccess文件在IIS上就没有,要想实现类似的跳转功能可以使用ISAPI_Rewrite来代替,这是一个专门为IIS设置的工具,目的就是代替.htaccess实现很多功 ...

  2. 新书出版《.NET框架设计—模式、配置、工具》感恩回馈社区!

    很高兴我的第一本书由图灵出版社出版.本书总结了我这些年来对框架学习.研究的总结,里面纯干货,无半句废话. 书的详情请看互动网的销售页面:http://product.china-pub.com/377 ...

  3. 从零自学Hadoop(09):使用Maven构建Hadoop工程

    阅读目录 序 Maven 安装 构建 示例下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,Source ...

  4. android edittext属性说明

    将EditText内容转换为字符串: EditText.getText().toString() <EditText android:id="@+id/edt_month" ...

  5. ArrayList,Vector,LinkedList

    在java.util包中定义的类集框架其核心的组成接口有如下:·Collection接口:负责保存单值的最大父接口 |-List子接口:允许保存重复元素,数据的保存顺序就是数据的增加顺序: |-Set ...

  6. django博客功能实现——标签功能

    标签功能添加流程 0.功能概括 标签作为文章中的分类标记,会显示出该文章是关于哪一方面的文章,比如是关于python的还是关于django的. 当我们点击该标签的时候,会出现该博客中所有属于该标签的文 ...

  7. spring初次体验

    1.BeanFactory:实际上是实例化,配置和管理众多bean的容器.这些bean通常会彼此合作,因而它们之间会相互依赖.BeanFactory使用的配置数据可以反映这些依赖关系中(一些依赖可能不 ...

  8. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  9. c# 传递Null的string值导致的调用C++的dll报错 Attempted to read or write protected memory.

    c# 调用C++的dll报错 Attempted to read or write protected memory:   原因是:c# 传递Null的string值导致的,将Null改为string ...

  10. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...