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 ...
随机推荐
- CSS3:优雅地绘制不规则ICON
早上在w3ctech上看到 中国第二届CSS Conf总结 的时候,真是开心极了: 自从去年在慕课网上看了第一届CSS conf 视频之后,整个人都震惊了,原来还有大会是专门用来讨论CSS的,而且分 ...
- Android应用程序中Activity的生命周期
Android应用程序中Activity的生命周期 对于Android来说Activity的生命周期是非常的重要,尤其是对于新学者来说,只有充分了解了Activity的生命周期,才能写出优良用户体验的 ...
- [Docker] Install Docker on Windows (hp) and start with Kitematic
Well, on Windows costs a little bit effort to run docker. 1. You need to enable Virtulization: Oh hp ...
- Android实现获取本机中所有图片
本示例演示如何在Android中使用加载器(Loader)来实现获取本机中的所有图片,并进行查看图片的效果. 在这个示例中,我使用android-support-v4.jar中的加载器(Loader) ...
- 关于 视频同步vsync 信号在不同一时候钟域採样问题
今天调试 视频 4k(3840 x 1920)的vsync信号(时钟为 297Mhz) 进入 170Mhz 的时钟域, 发现输出来的信号信号抖动特别厉害.后来才发现这是不同一时候钟域 造成的影响. 快 ...
- 整理:C#写ActiveX, 从代码到打包到签名到发布的示例
对于不懂C++和VB的我, 在工作上却遇到需要重写旧ActiveX控件的任务. 好在客户机都是Windows PC, 基本上都有.net framework 2.0, 勉强用C#实现可以满足需求 所以 ...
- new的原理
先来个构造函数的例子: function Prince(name,age){ this.name=name; this.age=age; } var prince=new Prince("c ...
- CSS3动画(360度旋转、旋转放大、放大、移动)
Title div { width: 120px; height: 120px; line-height: 120px; margin: 20px; background-color: #5cb85c ...
- nagios配置
接近两个星期的奋战,nagios的安装搭建以及监控服务自动报警功能终于基本得以实现,现在自己整理一份安装技术手册,方便自己以后查阅和回顾. 接近两个星期的奋战,nagios的安装搭建以及监控服务自动报 ...
- edit编辑框相关
从Edit Control获取值,然后通过MessageBox输出出来 void CNowaMagic_MFCDlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代 ...