【POJ】2369 - Permutations(置换群)
2016-07-19 09:14
441 查看
点击打开题目
Permutations
Description
We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Less formally, that is a way to reorder elements of the set. For example, one can define a permutation of the set {1,2,3,4,5} as follows:
![](http://poj.org/images/2369_1.jpg)
This record defines a permutation P as follows: P(1) = 4, P(2) = 1, P(3) = 5, etc.
What is the value of the expression P(P(1))? It’s clear, that P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3. One can easily see that if P(n) is a permutation then P(P(n)) is a permutation as well. In our example (believe us)
![](http://poj.org/images/2369_2.jpg)
It is natural to denote this permutation by P2(n) = P(P(n)). In a general form the defenition is as follows: P(n) = P1(n), Pk(n) = P(Pk-1(n)). Among the permutations there is a very important one — that moves nothing:
![](http://poj.org/images/2369_3.jpg)
It is clear that for every k the following relation is satisfied: (EN)k = EN. The following less trivial statement is correct (we won't prove it here, you may prove it yourself incidentally): Let P(n) be some permutation of an N elements set. Then there exists
a natural number k, that Pk = EN. The least natural k such that Pk = EN is called an order of the permutation P.
The problem that your program should solve is formulated now in a very simple manner: "Given a permutation find its order."
Input
In the first line of the standard input an only natural number N (1 <= N <= 1000) is contained, that is a number of elements in the set that is rearranged by this permutation. In the second line there are N natural numbers of the range from 1 up to N, separated
by a space, that define a permutation — the numbers P(1), P(2),…, P(N).
Output
You should write an only natural number to the standard output, that is an order of the permutation. You may consider that an answer shouldn't exceed 109.
Sample Input
Sample Output
Source
Ural State University Internal Contest October'2000 Junior Session
根据置换群的性质,求置换的秩即为求LCM(所有循环阶数)。
代码如下:
#include <cstdio>
int GCD(int a,int b)
{
if (a % b == 0)
return b;
return GCD(b , a % b);
}
int LCM(int a,int b)
{
return a / GCD(a,b) * b;
}
int main()
{
int n;
int num[1000+22];
int ans;
int ant; //循环阶数
while (~scanf ("%d",&n))
{
ans = 1;
for (int i = 1 ; i <= n ; i++)
scanf ("%d",&num[i]);
for (int i = 1 ; i <= n ; i++)
{
int t = num[i];
ant = 1;
while (t != i)
{
t = num[t];
ant++;
}
ans = LCM(ans , ant);
}
printf ("%d\n",ans);
}
return 0;
}
Permutations
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2941 | Accepted: 1586 |
We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Less formally, that is a way to reorder elements of the set. For example, one can define a permutation of the set {1,2,3,4,5} as follows:
![](http://poj.org/images/2369_1.jpg)
This record defines a permutation P as follows: P(1) = 4, P(2) = 1, P(3) = 5, etc.
What is the value of the expression P(P(1))? It’s clear, that P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3. One can easily see that if P(n) is a permutation then P(P(n)) is a permutation as well. In our example (believe us)
![](http://poj.org/images/2369_2.jpg)
It is natural to denote this permutation by P2(n) = P(P(n)). In a general form the defenition is as follows: P(n) = P1(n), Pk(n) = P(Pk-1(n)). Among the permutations there is a very important one — that moves nothing:
![](http://poj.org/images/2369_3.jpg)
It is clear that for every k the following relation is satisfied: (EN)k = EN. The following less trivial statement is correct (we won't prove it here, you may prove it yourself incidentally): Let P(n) be some permutation of an N elements set. Then there exists
a natural number k, that Pk = EN. The least natural k such that Pk = EN is called an order of the permutation P.
The problem that your program should solve is formulated now in a very simple manner: "Given a permutation find its order."
Input
In the first line of the standard input an only natural number N (1 <= N <= 1000) is contained, that is a number of elements in the set that is rearranged by this permutation. In the second line there are N natural numbers of the range from 1 up to N, separated
by a space, that define a permutation — the numbers P(1), P(2),…, P(N).
Output
You should write an only natural number to the standard output, that is an order of the permutation. You may consider that an answer shouldn't exceed 109.
Sample Input
5 4 1 5 2 3
Sample Output
6
Source
Ural State University Internal Contest October'2000 Junior Session
根据置换群的性质,求置换的秩即为求LCM(所有循环阶数)。
代码如下:
#include <cstdio>
int GCD(int a,int b)
{
if (a % b == 0)
return b;
return GCD(b , a % b);
}
int LCM(int a,int b)
{
return a / GCD(a,b) * b;
}
int main()
{
int n;
int num[1000+22];
int ans;
int ant; //循环阶数
while (~scanf ("%d",&n))
{
ans = 1;
for (int i = 1 ; i <= n ; i++)
scanf ("%d",&num[i]);
for (int i = 1 ; i <= n ; i++)
{
int t = num[i];
ant = 1;
while (t != i)
{
t = num[t];
ant++;
}
ans = LCM(ans , ant);
}
printf ("%d\n",ans);
}
return 0;
}
相关文章推荐
- 面向对象的理解
- java 虚拟机参数配置详解
- 48. Rotate Image
- Execution failed for task ':app:transformClassesWithDexForDebug'和UnsupportedClassVersionError
- spring(九):基于配置的spring aop
- GDB调试,makefile使用,linux相关
- SSH项目开发中表现层和业务逻辑层之间的传值问题
- floodfill算法
- 实习见闻(1)——第一天
- Android中SQLite应用详解
- 解决vs2008无法切换设计视图的问题
- js动态添加事件-事件委托
- mysql备份之xtrabackup
- 杭电1004
- ROS学习之 cpp名字和节点信息
- 代码优化
- Hadoop HDFS概念学习系列之熟练掌握HDFS的Shell访问(十五)(博主推荐)
- MySQL · 特性分析 · 内部临时表
- Java中String类的一些用法 和含义
- 专访|HPE软件部中国区总经理李时:HPE引领IT战略新形态