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

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

4
5
-1

Sample Output

21
39
/*Polya原理*/
#include <cstdio>
#include <iostream>
using namespace std;
long long aaaa(long long a,long long b)
{
long long res=,tmp=a;
while(b)
{
if (b&)
res*=tmp;
tmp*=tmp;
b>>=;
}
return res;
}
int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
}
int main()
{
int n;
while ( ~scanf("%d", &n) && n != -)
{
long long a = , b = ;
for (int i=;i<n;i++)
{
a+=aaaa(,gcd(i,n));
}
if (n&)
b=(long long)n*aaaa(,(n+)/);
else
b=(long long)n/*(aaaa(,n/+)+aaaa(,n/));
if (n==)
printf("0\n");
else
printf("%lld\n",(a+b)//n);
}
return ;
}

POJ 1286 Necklace of Beads(项链的珠子)的更多相关文章

  1. poj 1286 Necklace of Beads &amp; poj 2409 Let it Bead(初涉polya定理)

    http://poj.org/problem?id=1286 题意:有红.绿.蓝三种颜色的n个珠子.要把它们构成一个项链,问有多少种不同的方法.旋转和翻转后同样的属于同一种方法. polya计数. 搜 ...

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

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

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

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

  4. poj 1286 Necklace of Beads poj 2409 Let it Bead HDU 3923 Invoker <组合数学>

    链接:http://poj.org/problem?id=1286 http://poj.org/problem?id=2409 #include <cstdio> #include &l ...

  5. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  6. poj 2409 Let it Bead && poj 1286 Necklace of Beads(Polya定理)

    题目:http://poj.org/problem?id=2409 题意:用k种不同的颜色给长度为n的项链染色 网上大神的题解: 1.旋转置换:一个有n个旋转置换,依次为旋转0,1,2,```n-1. ...

  7. POJ 1286 Necklace of Beads(Polya定理)

    点我看题目 题意 :给你3个颜色的n个珠子,能组成多少不同形式的项链. 思路 :这个题分类就是polya定理,这个定理看起来真的是很麻烦啊T_T.......看了有个人写的不错: Polya定理: ( ...

  8. poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)

      Description Beads of red, blue or green colors are connected together into a circular necklace of ...

  9. poj 1286 Necklace of Beads【polya定理+burnside引理】

    和poj 2409差不多,就是k变成3了,详见 还有不一样的地方是记得特判n==0的情况不然会RE #include<iostream> #include<cstdio> us ...

随机推荐

  1. 你好,C++(6)2.3 C++兵器谱

    2.3  C++兵器谱 正所谓“工欲善其事,必先利其器”,而要想做好C++程序设计,自然也离不开几件像样的兵器.下面我们就来看看C++兵器谱上有哪些神兵利器值得我们学习掌握.排在兵器谱上首要位置的就是 ...

  2. Linux 编译安装httpsqs

    wget http://httpsqs.googlecode.com/files/libevent-2.0.12-stable.tar.gz tar zxvf libevent-2.0.12-stab ...

  3. apscheduler的使用

    最近一个程序要用到后台定时任务,看了看python后台任务,一般2个选择,一个是apscheduler,一个celery.apscheduler比较直观简单一点,就选说说这个库吧.网上一搜索,晕死,好 ...

  4. Haskell趣學指南--这个有意思

    正在慢慢了解不同于命令式的函数式语言. 希望能获得新的视野.. ~~~~~~~~~~~ http://learnyouahaskell-zh-tw.csie.org/zh-cn/ready-begin ...

  5. haskell学习笔记_函数

    一开始学习函数式编程语言就被告知函数式编程语言是一种“定义式”的语言,而不是一种命令式的语言,在学习haskell的函数语法时,此感觉更加强烈,haskell的函数定义倾向于一种类似C++里面的swi ...

  6. 直接修改别人jar包里面的class文件 工具:jclasslib

    出于某些原因 需要把别人jar包里面的class修改一下信息 配置文件*.properties MANIFEST.MF 这些东西可以直接用记事本打开修改 然后替换掉就OK.. 在网上游荡了半天,没有找 ...

  7. Could not find *.apk!解决办法

    右键点击项目选择Properties,把Libraries下Android x.x给remove了. 点右侧的Add Library,选择JRE System Library然后next,重新指定JR ...

  8. Kill 锁,1222:已超过了锁请求超时时段,

    应该是你的表体积很大,处理的时候费事,因为几乎所有数据库操作都需要加或多或少的锁,所以会超时.首先你可以使用select * from sys.sysprocesses where blocked&l ...

  9. 使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道

    使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道 使用python/casperjs编写终极爬虫-客户端App的抓取

  10. JS字符串拼接优化

    // 请把以下用于连接字符串的JavaScript代码修改为更高效的方式 var htmlString = ‘ < div class=”container” > ’ + ‘ < u ...