POJ 2369 Permutations (置换的秩P^k = I)
题意
给定一个置换形式如
,问经过几次置换可以变为恒等置换
思路
就是求k使得Pk = I.
我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数.
把一个置换转换成轮换的方法也很简单,从一个数出发按照置换图置换,直到置换到已经置换过的数,则这些数就构成一个轮换。
代码
[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;
int gcd(int a, int b){
return b?gcd(b, a%b):a;
}
int a[1005];
bool vis[1005];
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n;
while(scanf("%d", &n) != EOF){
for (int i = 1; i <= n; i ++){
scanf("%d", &a[i]);
}
MEM(vis, false);
int res = 1;
for (int i = 1; i <= n; i ++){
if (!vis[i]){
vis[i] = 1;
int len = 1;
int p = i;
while (1){
p = a[p];
if (vis[p]) break;
vis[p] = 1;
len ++;
}
res = res / gcd(res, len) * len;
}
}
printf("%d\n", res);
}
return 0;
}
[/cpp]
POJ 2369 Permutations (置换的秩P^k = I)的更多相关文章
- poj 2369 Permutations 置换
题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...
- poj 2369 Permutations (置换入门)
题意:给你一堆无序的数列p,求k,使得p^k=p 思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案 代码: #include <cstdio> #include ...
- poj 2369 Permutations - 数论
We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...
- POJ 2369 Permutations(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- POJ 2369 Permutations
傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
- UVA - 11077 Find the Permutations (置换)
Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...
随机推荐
- Selenium之使用PageFactory初始化pageobject
使用PageFactory初始化pageobject有什么作用呢,下面举个例子来说明 public BaiduPage baiduPage = PageFactory.initElements(dri ...
- Writing a device driver for Windows
Writing a device driver for Windows In order to write a device driver for windows, one needs ...
- 字典的fromkeys的用法
fromkeys方法语法 dict.fromkeys(iterable[,value=None]) iterable 用于创建新的字典的键的可迭代对象(字符串,列表,元组,字典) value 可选参数 ...
- SQL Server创建事务——锁
参考地址:http://www.cnblogs.com/knowledgesea/p/3714417.html 事务定义: 事务是作为单个逻辑单元执行的一系列操作,它是一个不可分割的工作逻辑单元.它包 ...
- openJudge C17K:Lying Island
地址:http://poj.openjudge.cn/practice/C17K/ 题目: C17K:Lying Island 查看 提交 统计 提问 总时间限制: 2000ms 内存限制: 26 ...
- GYM - 101490 J Programming Tutors (匈牙利+二分)
题意:有N个学生和N个老师,每个人都有自己的坐标X,Y,给每个学生匹配一个老师,要求N个匹配中的距离最大值最小.其中距离的定义为:|X − X’| + |Y − Y ‘|. 分析:一道典型的最大值最小 ...
- Unix/Linux操作系统中如何在sqlplus/rman中使用方向键
默认情况下在Unix/Linux中使用Oracle的sqlplus/rman是无法使用↑↓←→几个方向键进行操作的,要想达到Windows下使用sqlplus/rman的效果需要安装rlwrap. r ...
- 【VS2015】未能创建 Visual C# 2015编译器
今天在安装完成Visual Studio 2015后,在执行update 3时出错,导致再次打开VS2015时提示错误:“未能创建 Visual C# 2015编译器”和“未能正确加载CSharpPa ...
- 【VS Error】VS2008在编译时出现:Error 15 Cannot register assembly
现象: 在visual studio 2008在编译类库时提示如下错误: Error 15 Cannot register assembly "D:\01_Work\02_SVN\OCRpl ...
- GET 对比 POST
HTTP 方法:GET 对比 POST HTTP 消息 标签列表(字母排序) 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户 ...