Permutations(hdu2369,置换群)

本文深入探讨了置换理论的基本概念,并提供了一个求置换次数的算法实现。通过实例输入,逐步解析如何找到给定置换的最小周期数,即其达到恒等置换所需的迭代次数。文中详细解释了算法背后的数学原理,包括使用辗转相除法求最大公约数来优化计算过程。此教程适用于计算机科学领域的学习者,特别是那些对离散数学和算法设计感兴趣的人。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=2369

Permutations

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 2384 Accepted: 1235

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))? Its 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

5

4 1 5 2 3

Sample Output

6

Source

Ural State University Internal Contest October'2000 Junior Session

解析:求置换次数

156K 16MS C++ 790B

*/


#include<stdio.h>
#include<string.h>
#include <iostream>
using namespace std;
const int maxn=1000+10;
long long a[maxn];
int vis[maxn];
long long gcd(long long  a,long long b)//这里需要换成long long
{
if(b==0)
return a;
gcd(b,a%b);
}
int main()
{  long long n,i,t,ans,j;
  while(scanf("%I64d",&n)!=EOF)
{
ans=1;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
}
ans=1;
for(i=1;i<=n;i++)
{
if(!vis[i])
{  t=0;
   j=i;
   while(!vis[j])
               {
               t++;
               vis[j]=1;
               j=a[j];
               }
  //printf("t==%d\n",gcd(2,4));
  }
  ans=ans/gcd(ans,t)*t;
}
  printf("%I64d\n",ans);
}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值