poj1286--Necklace of Beads(置换群+polya计数)
2015-07-28 10:25
232 查看
题目链接:点击打开链接
题目大意:给出三种颜色红绿蓝,对一串n个小球的环染色,环可以旋转和翻转,问最终可能有多少不同的染色方案。
首先说明polya计数:
由这个公式,既可以计算出不同的染色方案,那么我们需要求的也就是不同置换的个数,和每一个置换的循环节数
旋转,旋转i个小球的距离,那么会得到0~n-1的置换方案,共有n中,对于旋转i个小球的循环节数为gcd(n,i)
翻转,对于偶数,不经过小球有对称抽有n/2个,每种置换方案有n/2+1个循环节;经过小球的对称轴有n/2个,每种置换方案有n/2个循环节
对于奇数,只存在经过小球的对称轴,有n个,每种方案有n/2+1个循环节
注意:n==0时,输出0
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
#define LL __int64
LL gcd(LL a,LL b) {
return b == 0 ? a : gcd(b,a%b) ;
}
LL pow(LL x,LL k) {
if( k == 1 ) return x ;
LL s = pow(x,k/2) ;
s = s*s ;
if( k%2 ) s *= x ;
return s ;
}
int main() {
LL n , i , ans , num ;
while( scanf("%I64d", &n) && n != -1 ) {
if( n == 0 ){
printf("0\n") ;
continue ;
}
ans = 0 ;
for(i = 0 ; i < n ; i++)
ans += pow(3,gcd(n,i)) ;
if( n%2 ) {
ans += n*pow(3,n/2+1) ;
}
else {
ans += n/2*pow(3,n/2) ;
ans += n/2*pow(3,n/2+1) ;
}
printf("%I64d\n", ans/(n*2)) ;
}
return 0 ;
}
题目大意:给出三种颜色红绿蓝,对一串n个小球的环染色,环可以旋转和翻转,问最终可能有多少不同的染色方案。
首先说明polya计数:
由这个公式,既可以计算出不同的染色方案,那么我们需要求的也就是不同置换的个数,和每一个置换的循环节数
旋转,旋转i个小球的距离,那么会得到0~n-1的置换方案,共有n中,对于旋转i个小球的循环节数为gcd(n,i)
翻转,对于偶数,不经过小球有对称抽有n/2个,每种置换方案有n/2+1个循环节;经过小球的对称轴有n/2个,每种置换方案有n/2个循环节
对于奇数,只存在经过小球的对称轴,有n个,每种方案有n/2+1个循环节
注意:n==0时,输出0
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
#define LL __int64
LL gcd(LL a,LL b) {
return b == 0 ? a : gcd(b,a%b) ;
}
LL pow(LL x,LL k) {
if( k == 1 ) return x ;
LL s = pow(x,k/2) ;
s = s*s ;
if( k%2 ) s *= x ;
return s ;
}
int main() {
LL n , i , ans , num ;
while( scanf("%I64d", &n) && n != -1 ) {
if( n == 0 ){
printf("0\n") ;
continue ;
}
ans = 0 ;
for(i = 0 ; i < n ; i++)
ans += pow(3,gcd(n,i)) ;
if( n%2 ) {
ans += n*pow(3,n/2+1) ;
}
else {
ans += n/2*pow(3,n/2) ;
ans += n/2*pow(3,n/2+1) ;
}
printf("%I64d\n", ans/(n*2)) ;
}
return 0 ;
}
相关文章推荐
- 天声人語 20150728
- Linux进程调度-------O(1)调度和CFS调度器
- Linux下配置本地yum源
- 为什么我们应该为Ruby2.0的GC感到excited!
- hdu3665Seaside 最短路水题
- OSX工具集之——Theos
- 高逼格UI-ASD(Android Support Design)
- Lua实现的Base64编码
- 各种环境的配置方式
- nyoj 491 幸运三角形 (dfs + 打表 出现木马 哈哈)
- C# 自带的.net类库 实现得到本机IP以及网关地址
- 构造可靠的软件测试过程
- 在rails的console下调用helper方法
- Mac OS X上使用Wireshark抓包
- getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的作用
- netty4源码分析——写操作
- 2015年暑假阿里内推面试经历(止步于2面)
- 搜索引擎:MapReduce实战----倒排索引
- web前端面试01
- Spring MVC学习-----------异常处理以及日志记录