poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)
Description
Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < ). 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.
- 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
-
Sample Output
Source
1、题目类型:Polya定理、组合数学、置换群。
2、解题思路:Polya定理:(1)设G是p个对象的一个置换群,用k种颜色突然这p个对象,若一种染色方案在群G的作用下变为另一种方案,则这两个方案当作是同一种方案,这样的不同染色方案数为:
;
(2)置换及循环节数的计算方法:对于有n个位置的手镯,有n种旋转置换和n种翻转置换.
对于旋转置换: c(fi) = gcd(n,i) i为一次转过i颗宝石( i = 0 时 c=n;);
对于翻转置换:如果n为偶数:c(f) = n/2 的置换有n/2个;
c(f) = n/2+1 的置换有n/2个;
如果n为奇数:c(f) = n/2+1.
3、注意事项:注意对于翻转置换过程中对于奇偶数情况的区分处理。
相同的gcd合并在一起计算:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define ll long long
ll pow_mod(ll a,ll i){
if(i==)
return ;
ll t=pow_mod(a,i/);
ll ans=t*t;
if(i&)
ans=ans*a;
return ans;
} vector<ll> divisor(ll n){
vector<ll> res;
for(ll i=;i*i<=n;i++){
if(n%i==){
res.push_back(i);
if(i*i!=n){
res.push_back(n/i);
}
}
}
return res;
}
ll eular(ll n){
ll res=;
for(ll i=;i*i<=n;i++){
if(n%i==){
n/=i,res*=i-;
while(n%i==){
n/=i;
res*=i;
}
}
}
if(n>) res*=n-;
return res;
}
ll polya(ll m,ll n){
//map<ll,ll> primes = prime_factor(n);
vector<ll> divs = divisor(n);
ll res=;
for(ll i=;i<divs.size();i++){
ll euler=eular(divs[i]);
res+=euler*pow_mod(m,n/divs[i]);
}
res/=n;
return res;
}
int main()
{
ll n,m=;
while(~scanf("%I64d",&n) && n!=-){
if(n==){
puts("");
continue;
}
ll count=polya(m,n)*n;//旋转情况
if(n&){//奇数
count+=n*pow_mod(m,n/+);//翻转情况
}
else{//偶数
count += (pow_mod(m, n / + ) + pow_mod(m, n / )) * (n / );//翻转情况
}
count/=*n;
printf("%I64d\n",count);
}
return ;
}
附上大神代码:
相同的gcd合并在一起计算:
#include <iostream>
#include <map>
#include <vector>
using namespace std; #define LL long long inline LL power(LL p, LL n)
{
LL sum = ;
while (n)
{
if (n & )
sum *= p;
p *= p;
n /= ;
}
return sum;
} //************************************
// Method: divisor
// FullName: divisor
// Access: public
// Returns: vector<int> 约数
// Qualifier: 约数枚举
// Parameter: const int & n 目标数n
//************************************
vector<int> divisor(const int& n)
{
vector<int> res;
for (int i = ; i * i <= n; ++i)
{
if (n % i == )
{
res.push_back(i);
if (i != n / i)
{
res.push_back(n / i);
}
}
} return res;
} //************************************
// Method: prime_factor
// FullName: prime_factor
// Access: public
// Returns: map<int, int>
// Qualifier: 整数分解
// Parameter: int n
//************************************
map<int, int> prime_factor(int n)
{
map<int, int> res;
for (int i = ; i * i <= n; ++i)
{
while (n % i == )
{
++res[i];
n /= i;
}
}
if (n != )
{
res[n] = ;
}
return res;
} LL polya(const int& m, const int& n)
{
map<int, int> primes = prime_factor(n);
vector<int> divs = divisor(n);
LL res = ;
for (int i = ; i < divs.size(); ++i)
{
// 求divs[i]的欧拉函数值
LL euler = divs[i];
for (map<int, int>::iterator it = primes.begin(); it != primes.end(); ++it)
{
int p = it->first;
if (divs[i] % p == ) euler = euler / p * (p - );
} res += euler * power(m, n / divs[i]);
} // 最后除以n
res /= n;
return res;
} ///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n; const LL m = ;
while (~scanf("%d", &n) && n != -)
{
if (n == )
{
puts("");
continue;
} LL count = polya(m, n) * n;
if (n & )
count += n * power(m, n / + );
else
count += (power(m, n / + ) + power(m, n / )) * (n / );
count /= * n;
printf("%lld\n", count);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return ;
}
还有一种暴力求法:
#include <iostream>
using namespace std; #define LL long long int gcd(int a, int b)
{
return b == ? a : gcd(b, a % b);
} LL power(LL p, LL n)
{
LL sum = ;
while (n)
{
if (n & )
sum *= p;
p *= p;
n /= ; }
return sum;
} ///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n; const LL m = ;
while (~scanf("%d", &n) && n != -)
{
if (n == )
{
puts("");
continue;
}
LL count = ;
for (int i = ; i <= n; ++i)
count += power(m, gcd(i, n));
if (n & )
count += n * power(m, n / + );
else
count += n / * (power(m, n / + ) + power(m, n / ));
count /= n * ;
printf("%lld\n", count);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif
return ;
}
poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)的更多相关文章
- poj 1286 Necklace of Beads & poj 2409 Let it Bead(初涉polya定理)
http://poj.org/problem?id=1286 题意:有红.绿.蓝三种颜色的n个珠子.要把它们构成一个项链,问有多少种不同的方法.旋转和翻转后同样的属于同一种方法. polya计数. 搜 ...
- POJ 1286 Necklace of Beads(Polya简单应用)
Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换假设同样就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺 ...
- POJ 1286 Necklace of Beads(项链的珠子)
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7874 Accepted: 3290 ...
- 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 ...
- 数学计数原理(Pólya):POJ 1286 Necklace of Beads
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7763 Accepted: 3247 ...
- 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. ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- POJ 1286 Necklace of Beads(Polya定理)
点我看题目 题意 :给你3个颜色的n个珠子,能组成多少不同形式的项链. 思路 :这个题分类就是polya定理,这个定理看起来真的是很麻烦啊T_T.......看了有个人写的不错: Polya定理: ( ...
- poj 1286 Necklace of Beads【polya定理+burnside引理】
和poj 2409差不多,就是k变成3了,详见 还有不一样的地方是记得特判n==0的情况不然会RE #include<iostream> #include<cstdio> us ...
随机推荐
- lesson6:jmeter和badboy配合使用
由于jmeter不支持脚本的录制,只能手动设置,在某些集成型的压力测试时,使用不是很方便,这时可以和badboy一起配合使用,badboy支持网页操作的录制功能,并能把录制的操作导出为jmeter的脚 ...
- [转]Android Volley完全解析(四),带你从源码的角度理解Volley
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17656437 经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了,但是 ...
- [置顶] NO.4 使用预处理器进行调试
<c++ primer>第四版 p190 ************************************************************************* ...
- 使用memcached加速web应用实例
在实际应用中,一般会把数据库查询的结果保存到memcached中,下次訪问数据库时直接从memcached中获取.而不再进行数据库操作,这样非常大的程度上减轻了数据库的负担. [演示样例]: < ...
- VS2010调试小技巧
在VS下做开发的时候我们进行调试的时候路径是这个样子的:http://localhost:端口号/项目名称/index.aspx 但是发布到服务器上面的时候却是这个样子的:http://www.xxx ...
- Android(java)学习笔记262:JNI之工具快速开发步骤
下面通过一个案例说明一下,利用工具jni快速开发步骤 1.新建一个Android工程,命名为"03_对int数组加1",如下: 2. 在MainActivity.java中对add ...
- Panel( 面板) 组件 上
一. 加载方式//class 加载方式<div class="easyui-panel" data-options="closable:true"titl ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程 索引
Senparc.Weixin.MP SDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到比较稳定的版本,这个过程中我觉得有必要整理一些思路和经验,和大家一起分享. ...
- this——笔记
this是执行上下文中的一个属性.this与上下文中可执行代码的类型有直接关系,this值在进入上下文时确定,并且在上下文运行期间永久不变. 在这里一切都简单.在全局代码中,this始终是全局对象本身 ...
- MySql不支持事务解决
用的是一个绿色版的mysql数据库,发现不支持事务,在网络上搜集资料找到解决方案: 1.执行语句 SHOW ENGINES; 如果发现InnoDB全部显示为“YES”,说明该版本的数据库支持事务 2 ...