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

4
5
-1

Sample Output

21
39

Source

数学问题 统计 polya原理

和POJ2409一样的套路

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL phi(int x){
int m=sqrt(x+0.5);
LL res=x;
for(int i=;i<=m;i++)
if(x%i==){
res=res/i*(i-);
while(x%i==)x/=i;
}
if(x>)res=res/x*(x-);
return res;
}
int n;
int gcd(int a,int b){
return (!b)?a:gcd(b,a%b);
}
LL ksm(LL c,LL k){
LL res=;
while(k){
if(k&)res=res*c;
c*=c;
k>>=;
}
return res;
}
int main(){
int i,j;
while(){
n=read();
if(n==-)break;
if(!n){
cout<<<<endl;
continue;
}
LL ans=;
for(i=;i<=n;i++){
if(n%i==)ans+=ksm(,i)*phi(n/i);
}
if(!(n&)){
ans+=ksm(,n/)*n/;
ans+=ksm(,n/+)*n/;
}
else ans+=ksm(,(n+)/)*n;
ans/=*n;
cout<<ans<<endl;
}
return ;
}

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. luogu4238 【模板】多项式求逆

    ref #include <iostream> #include <cstdio> using namespace std; typedef long long ll; int ...

  2. I两种冒泡算法

    两种冒泡算法: 第一个循环,I 定位当前坐标,第二个循环 把 I 之后的每个数都与 I 比较(比 I 小的都去坐标I),第二个循环之后 坐标 I 为数组里最小的数值. 效率比较高的冒泡算法: stat ...

  3. git部署详解

    1.1 关于版本控制 1.1.1 本地版本控制 本地版本控制系统 许多人习惯用复制整个项目目录的方式来保存不同的版本,或许还会改名加上备份时间以示区别.这么做唯一的 好处就是简单,但是特别容易犯错.有 ...

  4. PyQt5图像全屏显示

    Windows装这个:https://pypi.python.org/pypi/PyQt5Ubuntu输入这个:sudo apt-get install python3-pyqt5 或者直接输入:pi ...

  5. for循环再探

    摘要:for循环头的组成.for的执行流程 一.for 语句的组成 0. 举个例子 for (int val = 1; val <= 10; ++val) sum += val; 1. 循环头的 ...

  6. android-ViewList的通用ViewHold

    在写ViewList的时候要写Adapter的时候,经常大量的代码都是差不多的. 1 ViewHold 2 if(convertView ==null ){}else{} 3 setTag 4 FIn ...

  7. 点击查看大图Activity

    1.使用方式 Intent intent = new Intent(FriendCircleActivity.this, ImageGralleryPagerActivity.class);//0,索 ...

  8. MUI scroll 定位问题

    做一个微信项目,使用MUI做框架,在使用scroll定位的时候,出现了定位不准确的问题,查询了好多资料,得知他是相对定位.折腾了好久,才搞定,现在做一个笔记. mui('body').on('tap' ...

  9. 用vs调试项目页面无样式

    ASP.NET Development Server 上的文件授权 在文件系统网站中,静态文件(例如图像和样式表)遵守 ASP.NET 授权.例如,如果禁用了对静态文件的匿名访问,匿名用户则不能使用文 ...

  10. js获取上传文件内容

    js 获取上传文件的字节数及内容 <div> 上传文件 : <input type="file" name = "file" id = &qu ...