Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8263   Accepted: 3452

Description

Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). 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

  1. 4
  2. 5
  3. -1

Sample Output

  1. 21
  2. 39

Source

数学问题 统计 polya原理

和POJ2409一样的套路

  1. /*by SilverN*/
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<cmath>
  7. #include<vector>
  8. #define LL long long
  9. using namespace std;
  10. int read(){
  11. int x=,f=;char ch=getchar();
  12. while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
  13. while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
  14. return x*f;
  15. }
  16. LL phi(int x){
  17. int m=sqrt(x+0.5);
  18. LL res=x;
  19. for(int i=;i<=m;i++)
  20. if(x%i==){
  21. res=res/i*(i-);
  22. while(x%i==)x/=i;
  23. }
  24. if(x>)res=res/x*(x-);
  25. return res;
  26. }
  27. int n;
  28. int gcd(int a,int b){
  29. return (!b)?a:gcd(b,a%b);
  30. }
  31. LL ksm(LL c,LL k){
  32. LL res=;
  33. while(k){
  34. if(k&)res=res*c;
  35. c*=c;
  36. k>>=;
  37. }
  38. return res;
  39. }
  40. int main(){
  41. int i,j;
  42. while(){
  43. n=read();
  44. if(n==-)break;
  45. if(!n){
  46. cout<<<<endl;
  47. continue;
  48. }
  49. LL ans=;
  50. for(i=;i<=n;i++){
  51. if(n%i==)ans+=ksm(,i)*phi(n/i);
  52. }
  53. if(!(n&)){
  54. ans+=ksm(,n/)*n/;
  55. ans+=ksm(,n/+)*n/;
  56. }
  57. else ans+=ksm(,(n+)/)*n;
  58. ans/=*n;
  59. cout<<ans<<endl;
  60. }
  61. return ;
  62. }

POJ1286 Necklace of Beads的更多相关文章

  1. POJ1286 Necklace of Beads(Polya定理)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9359   Accepted: 3862 Description Beads ...

  2. 【数论】【Polya定理】poj1286 Necklace of Beads

    Polya定理:设G={π1,π2,π3........πn}是X={a1,a2,a3.......an}上一个置换群,用m中颜色对X中的元素进行涂色,那么不同的涂色方案数为:1/|G|*(mC(π1 ...

  3. poj1286 Necklace of Beads—— Polya定理

    题目:http://poj.org/problem?id=1286 真·Polya定理模板题: 写完以后感觉理解更深刻了呢. 代码如下: #include<iostream> #inclu ...

  4. poj1286 Necklace of Beads【裸polya】

    非常裸的polya,只是我看polya看了非常久 吉大ACM模板里面也有 #include <cstdio> #include <cmath> #include <ios ...

  5. POJ 1286 Necklace of Beads(Polya简单应用)

    Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换假设同样就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺 ...

  6. 数学计数原理(Pólya):POJ 1286 Necklace of Beads

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7763   Accepted: 3247 ...

  7. POJ 1286 Necklace of Beads(项链的珠子)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7874   Accepted: 3290 ...

  8. Necklace of Beads(polya计数)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 3102 ...

  9. hdu 1817 Necklace of Beads(Polya定理)

    Necklace of Beads Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. ubuntu设置ssh登陆

    转: 默认请况下,ubuntu是不允许远程登陆的.(因为服务没有开,可以这么理解.) 想要用ssh登陆的话,要在需要登陆的系统上启动服务.即,安装ssh的服务器端 $ sudo apt-get ins ...

  2. C#窗口抖动

    用过QQ的窗口抖动功能吧.是不是觉得很神奇?很有意思?其实,仔细想想,使用的原理还是挺简单的:让窗口的位置不断快速地发生变化. 说出了原理,是不是一下恍然大悟?顿时理解了.我以前也想过如何实现这个功能 ...

  3. 第三十四篇 Python面向对象之 反射(自省)

    什么是反射? 反射的概念是由Smith在1982年提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序语 ...

  4. Android Spiner实现Key-Value

    原网址:http://www.eoeandroid.com/thread-29687-1-1.html?_dsign=02d5cd6a 学习到的方法,直接上代码了: 1.定义一个class publi ...

  5. Gym101981I Magic Potion(最大流)

    Problem I. Magic Potion There are n heroes and m monsters living in an island. The monsters became v ...

  6. 1066 Root of AVL Tree (25 分)(平衡二叉树)

    就是AVL的模板题了 注意细节 #include<bits/stdc++.h> using namespace std; typedef struct node; typedef node ...

  7. UVA 1085 House of Cards(对抗搜索)

    Description   Axel and Birgit like to play a card game in which they build a house of cards, gaining ...

  8. ubuntu 和 centOS 的apache设置

    更改ubuntu的网站访问根目录: 在sudo gedit /etc/apache2/sites-enabled/000-default,把 DocumentRoot /var/www      #这 ...

  9. Bitcoin-NG

    Bitcoin-NG,一个新的可扩展的区块链协议 Bitcoin-NG仅受限于网络的传输延时,它的带宽仅受限于个人节点的处理能力.通过将比特币的区块链操作分解为两部分来实现这个性能改善:首领选择(le ...

  10. MapReduce 并行编程理论基础

    对于mapreduce这一并行计算模型,一直以来都不是很清楚其具体的执行细节,今天看了学院一位老师的实验指导书,对这一过程有了一个初步的理解,特别是map阶段和reduce阶段,所以做了一份笔记,现在 ...