首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2369 - Permutations(置换群)

【POJ】2369 - Permutations(置换群)

作者头像
FishWang
发布2025-08-26 15:33:32
发布2025-08-26 15:33:32
16700
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Permutations

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 2941

Accepted: 1586

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."

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 10 9.

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
5
4 1 5 2 3

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
6

Source

Ural State University Internal Contest October'2000 Junior Session

根据置换群的性质,求置换的秩即为求LCM(所有循环阶数)。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档