Permutations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3039   Accepted: 1639

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

Sample Input

5
4 1 5 2 3

Sample Output

6

置换的周期是轮换长度的最小公倍数

代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 1005
long long lcm(long long a,long long b)
{
long long temp;
long long a0,b0;
a0=a;
b0=b;
while(b)
{
temp=b;
b=a%b;
a=temp;
}
return a0/a*b0;
}
int main()
{
int n;
int i,j;
long long p[MAXN];
long long g[MAXN];
int cnt;
long long l;
bool flag[MAXN];
memset(flag,false,sizeof(flag));
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%I64d",&p[i]);
cnt=;
for(i=;i<=n;i++)
{
if(flag[i])
continue;
g[cnt]=;
j=i;
while(p[j]!=i)
{
flag[j]=true;
j=p[j];
g[cnt]++;
}
cnt++;
}
l=g[];
for(i=;i<cnt;i++)
{
l=lcm(l,g[i]);
}
printf("%I64d\n",l);
return ;
}

POJ2369 Permutations(置换的周期)的更多相关文章

  1. 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)

    Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...

  2. UVA - 11077 Find the Permutations (置换)

    Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...

  3. poj2369 Permutations ——置换群

    link:http://poj.org/problem?id=2369 置换群,最简单的那种. 找所有数字循环节的最小公倍数. /* ID: zypz4571 LANG: C++ TASK: perm ...

  4. poj 2369 Permutations 置换

    题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...

  5. poj 2369 Permutations (置换入门)

    题意:给你一堆无序的数列p,求k,使得p^k=p 思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案 代码: #include <cstdio> #include ...

  6. POJ 2369 Permutations (置换的秩P^k = I)

    题意 给定一个置换形式如,问经过几次置换可以变为恒等置换 思路 就是求k使得Pk = I. 我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数. 把一个置换转换成轮换的方法也 ...

  7. UVA11077 Find the Permutations —— 置换、第一类斯特林数

    题目链接:https://vjudge.net/problem/UVA-11077 题意: 问n的全排列中多有少个至少需要交换k次才能变成{1,2,3……n}. 题解: 1.根据过程的互逆性,可直接求 ...

  8. POJ2369 Permutations【置换群】

    题目链接: http://poj.org/problem?id=2369 题目大意: 给定一个序列.问最少须要多少次置换才干变为 1.2.-.N 的有序序列.比方说给 定5个数的序列 4 1 5 2 ...

  9. POJ置换群入门[3/3]

    POJ 3270 Cow Sorting 题意: 一个序列变为升序,操作为交换两个元素,代价为两元素之和,求最小代价 题解: 看了黑书... 首先循环因子分解 一个循环完成的最小代价要么是循环中最小元 ...

随机推荐

  1. 关于ZedGraph

    http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET

  2. Java面试常见知识点总结(二)

    11.构造方法(构造器): 构造方法是一种特殊的方法,具有以下特点.    (1) 构造方法的方法名必须与类名相同.    (2) 构造方法没有返回类型,也不能定义为void,在方法名前面不声明方法类 ...

  3. python shutil.copy()用法

    shutil.copyfile(src, dst):复制文件内容(不包含元数据)从src到dst. DST必须是完整的目标文件名; 如果src和dst是同一文件,就会引发错误shutil.Error. ...

  4. 使用Nito.AsyncEx实现异步锁(转)

    转载地址:http://www.cnblogs.com/1zhk/p/5269279.html Lock是常用的同步锁,但是我们无法在Lock的内部实现异步调用,比如我们无法使用await. 以下面的 ...

  5. 数据库连接池c3p0学习

    这里只记录c3p0的数据源,不会涉及到其它方面和别的数据库连接池的对比 配置文件主要的实现方式有三种: 1.手写代码去加载一个配置文件 创建一个config.properties文件如下: drive ...

  6. Remove Duplicates from Sorted Array II

    题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

  7. EF不能很好的支持DDD?估计是我们搞错了!

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:最近在ABP项目中尝试纯粹的DDD,然后遇到EF实现的Repository似乎不能很好 ...

  8. 远程debug调试java代码

    远程debug调试java代码 日常环境和预发环境遇到问题时,可以用远程调试的方法本地打断点,在本地调试.生产环境由于网络隔离和系统稳定性考虑,不能进行远程代码调试. 整体过程是通过修改远程服务JAV ...

  9. Windows远程桌面打印机映射

    计算机的打印机驱动能打印,需要满足两个条件,一个是有打印驱动本身,一个是要有连接好了的端口.这样,打印作业就会被打印驱动程序封装成一种打印机能识别的组织形式,然后通过打印端口发送给打印机,然后打印! ...

  10. Learn Git and GitHub without any code!

    What is GitHub? GitHub is a code hosting platform for version control and collaboration.代码托管平台. repo ...