Necklace of Beads

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

Solution

polya定理模板题

设\(\overline{G}\)是n个对象的一个置换群, 用m种颜色染图这n个对象,则不同的染色方案数为:

\[L=\frac{1}{|\overline{G}|}[m^{c(\overline{p_1)}}+m^{c(\overline{p_2)}}+...+m^{c(\overline{p_g)}}]
\]

其中 \(\overline{G}=\{\overline{p_1},\overline{p_2},...,\overline{p_g}\}\), \(c(\overline{p_k})\)为 \(\overline{p_k}\) 的循环节数(阶)

如对于n=4:

单位元:仅有(1)(2)(3)(4)一种情况,\((1)^4*1\)

考虑旋转\(\pm90^\circ\),有\((1234)^1\),\((1432)^1\)两种情况,\((4)^1*2\)

考虑旋转\(180^\circ\),有(13)(24)一种情况,\((2)^2*1\)

考虑以两个对立顶点为轴翻转,有(1)(3)(24),(2)(4)(13)两种情况,\((1)^2(2)^1*2\)

考虑以一不经过任一顶点但平分多边形的直径翻转,有(12)(34),(13)(24)两种情况,\((2)^2*2\)

所以答案为$$\frac{34+312+32+332+3^2*2}{1+2+1+2+2}=21$$


  • 考虑旋转,对于\(n\)个球的环旋转\(i\)个球的循环节的个数为\(gcd(n,i)\),这一部分快速幂求和

  • 考虑奇数的翻转,每次选择一个顶点作对称轴翻转,循环节数为\(\frac{n+1}{2}\)(自身不动,剩余两两交换),一共n种选择

  • 考虑偶数的翻转,可选对顶的两顶点作为对称轴旋转,循环节数为\(\frac{n+2}{2}\),(两顶点不动,剩余两两交换),\(\frac{n}{2}种选择\),选择无顶点的对称轴翻转,循环节数为\(\frac{n}{2}\)(全部两两交换),\(\frac{n}{2}\)种选择

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#include <vector>
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define LONG_LONG_MAX 9223372036854775807LL
#define ll LL
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> P;
int n, m, k;
const int maxn = 1e5 + 10;
ll qpow(ll a, ll n)
{
ll res = 1;
while (n)
{
if (n & 1)
res *= a;
a *= a;
n >>= 1;
}
return res;
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll solve()
{
ll res = 0;
if (!n)
return res;
for (int i = 1; i <= n; i++)
{
res += qpow(3, gcd(n, i));
}
if (n & 1)
{
res += (ll)n * qpow(3, (n + 1) / 2);
}
else
{
res += (ll)(n / 2) * qpow(3, (n + 2) / 2);
res += (ll)(n / 2) * qpow(3, n / 2);
}
return res / (2 * n);
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while (cin >> n && n != -1)
{
cout << solve() << '\n';
}
return 0;
}

poj 1286 polya定理的更多相关文章

  1. POJ 1286 Pólya定理

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9162   Accepted: 3786 ...

  2. poj 2409(polya定理模板)

    题意:给你n种颜色和m个小球,问你有多少种不同的方案! 分析:作为模板.. 代码实现: #include <iostream> #include <cstdio> #inclu ...

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

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

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

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

  5. POJ 2409 Let it Bead(Polya定理)

    点我看题目 题意 :给你c种颜色的n个珠子,问你可以组成多少种形式. 思路 :polya定理的应用,与1286差不多一样,代码一改就可以交....POJ 1286题解 #include <std ...

  6. POJ 2409 Let it Bead:置换群 Polya定理

    题目链接:http://poj.org/problem?id=2409 题意: 有一串n个珠子穿起来的项链,你有k种颜色来给每一个珠子染色. 问你染色后有多少种不同的项链. 注:“不同”的概念是指无论 ...

  7. POJ 1286 【POLYA】

    题意: 给你三种颜色的珠子,每次给你N,问在旋转,翻转之后视作相同的情况下,能组成多少种不同的项链. 思路: 让我们借这道题拯救一下我对POLYA定理的理解... sigma(m^(gcd(i,n)) ...

  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 2409 Let it Bead (Polya定理)

    题意 用k种颜色对n个珠子构成的环上色,旋转翻转后相同的只算一种,求不等价的着色方案数. 思路 Polya定理 X是对象集合{1, 2, --, n}, 设G是X上的置换群,用M种颜色染N种对象,则不 ...

随机推荐

  1. div+css 布局技巧总计

    一.css 样式 1.float 首先需要了解块级元素(block element).每个块级元素都默认占用一行,在同一行只能添加一个块元素(float 除外).块级元素一般可以嵌套块级元素或者行内元 ...

  2. CentOS 6.5 下安装及使用中文输入法

    第一次在本机环境下搭载Linux环境,但搭载后发现在CentOS 6.5只能英文输入,于是试着安装并启用中文输入法,经过一翻折腾,实现了在终端和自带火狐浏览器下支持中文输入法. CentOS下安装中文 ...

  3. 8.秋招复习简单整理之Spring面试一般问题

    1.不同版本的Spring Framework有哪些主要功能? 2.什么是Spring Framework? Spring是一个轻量级的IOC和AOP容器框架,是为Java应用程序提供基础性服务的一套 ...

  4. C语言学习书籍推荐《C语言程序设计 现代方法(第2版)》下载

    下载地址:点我 C语言仍然是计算机领域的通用语言之一,但现在的C语言已经和当初的时候大不相同了.本书主要的一个目的就是通过一种“现代方法”来介绍C语言,书中强调标准C,强调软件工程,不再强调“手工优化 ...

  5. Spring MVC源码(二) ----- DispatcherServlet 请求处理流程 面试必问

    前端控制器 前端控制器,即所谓的Front Controller,体现的是设计模式中的前端控制器模式.前端控制器处理所有从用户过来的请求.所有用户的请求都要通过前端控制器.SpringMVC框架和其他 ...

  6. 注入攻击-XSS攻击-CSRF攻击

    1.注入攻击 注入攻击包括系统命令注入,SQL注入,NoSQL注入,ORM注入等 1.1攻击原理 在编写SQL语句时,如果直接将用户传入的数据作为参数使用字符串拼接的方式插入到SQL查询中,那么攻击者 ...

  7. 【LightOJ - 1370】Bi-shoe and Phi-shoe

    Bi-shoe and Phi-shoe Descriptions: 给出一些数字,对于每个数字找到一个欧拉函数值大于等于这个数的数,求找到的所有数的最小和. Input 输入以整数T(≤100)开始 ...

  8. 【CodeForces - 1167C 】News Distribution(并查集)

    News Distribution 题意 大概就是分成几个小团体,给每个人用1 - n编号,当对某个人传播消息的时候,整个小团体就知道这个消息,输出 分别对1 - n编号的某个人传递消息时,有多少人知 ...

  9. RecyclerView下拉加载集合越界问题

    问题描述 在做毕业设计app中遇到这样一个问题,使用RecyclerView进行下拉加载数据的时候,比如我每次让它加载5条数据,当服务器端数据总数刚好是5的倍数的时候,不会出现下拉加载数据集合越界的问 ...

  10. Linux环境Oracle数据库访问出现ORA-01034错误:oracle not available解决

    首先将用户切换为Oracle用户登录,su - oracle; 进入:sqlplus / as sysdba: 问题现象: SQL> select * from dba_users; selec ...