POJ 2369 Permutations (置换群)
来源:互联网 发布:apache fastcgi c 编辑:程序博客网 时间:2024/06/07 13:52
Permutations
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2819 Accepted: 1493
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:
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)
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:
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."
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)
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:
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
54 1 5 2 3
Sample Output
6
Source
Ural State University Internal Contest October'2000 Junior Session
题目链接:http://poj.org/problem?id=2369
题目大意:求将一个排列p(n)还原成En(1,2,3,4...)的最小置换次数
题目分析:计算置换中每个循环节内元素的个数,答案就是这个数的最小公倍数,很好理解,假设某个循环节包含3个元素,则这个循环节还原需要3次,另一个循环节包含2个元素,需要2次置换还原,因此我要让全部序列都还原,只需要取它们的最小公倍数即可
题目链接:http://poj.org/problem?id=2369
题目大意:求将一个排列p(n)还原成En(1,2,3,4...)的最小置换次数
题目分析:计算置换中每个循环节内元素的个数,答案就是这个数的最小公倍数,很好理解,假设某个循环节包含3个元素,则这个循环节还原需要3次,另一个循环节包含2个元素,需要2次置换还原,因此我要让全部序列都还原,只需要取它们的最小公倍数即可
#include <cstdio>#include <cstring>int const MAX = 1e3 + 5;int a[MAX], p[MAX];bool vis[MAX]; int gcd(int a, int b){ return b ? gcd(b, a % b) : a;}int lcm(int a, int b){ return a * b / gcd(a, b);}int main(){ memset(vis, false, sizeof(vis)); int n, ans = 1; scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); p[i] = i; } for(int i = 1; i <= n; i++) { int j = i, cnt = 0; while(!vis[j]) { cnt ++; vis[j] = true; j = p[a[j]]; } if(cnt) ans = lcm(ans, cnt); } printf("%d\n", ans);}
0 0
- poj 2369 Permutations(置换群)
- poj 2369 Permutations (置换群)
- POJ 2369 - Permutations 【置换群】
- POJ 2369 Permutations (置换群)
- POJ 2369 Permutations 置换群
- poj 2369 Permutations 【置换群】
- POJ 2369 Permutations (置换群)
- POJ 2369 Permutations 【置换群】
- POJ 2369 Permutations【置换群】
- POJ 2369 Permutations(置换)
- 【POJ 2369】Permutations(置换群)
- 【POJ】2369 - Permutations(置换群)
- poj 2369 Permutations (置换群入门)
- poj 2369 Permutations【简单置换群*详解】
- poj 2369 Permutations(置换)
- [ACM] poj 2369 Permutations (置换群循环节长度)
- poj 2369-Permutations置换及其应用
- poj 2369 Permutations 置换水题
- TextView相关属性
- 06_IO输入输出流
- spring.ftl文件不明白--Spring整合FreeMarker本地化动态设置
- iOS apache 的使用(10.10系统)
- Button设置内部的控件位置
- POJ 2369 Permutations (置换群)
- 07_集合
- Android实现推送方式解决方案
- nyoj 206 矩形的个数 【规律题】
- Android Studio 的简单配置使用以及各文件用途(一)
- iOS 项目的完整命名方法以及去除项目警告方法
- Linux进程间通信之共享内存(二)
- Oracle Net Configurantion Assistant failed,oracle grid 安装
- 剑指offer面试题40-数组中只出现一次的数字