Permutations

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

5
4 1 5 2 3

Sample Output

6
  给出一个置换A,求使得A^k=A成立的最小的k值。
  先把A分解成若干个循环的乘积,A=p1*p2*...*pm ,答案就是lcm(|p1|,|p2|,,,,|pm|);
每个循环只要执行|p1|次就会回到初始状态,所以找到一个最小公倍数使得所有循环都回到初始状态。
  
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define PI acos(-1.0)
int gcd(int a,int b){return b==?a:gcd(b,a%b);}
int lcm(int a,int b){return a*b/gcd(a,b);}
int a[];
bool v[];
int main()
{
int T,n,m,k,i,j,d;
while(scanf("%d",&n)!=EOF){
int ans=;
for(i=;i<=n;++i){
scanf("%d",&a[i]);
}
memset(v,,sizeof(v));
for(i=;i<=n;++i){
if(!v[i]){
int tmp=;
j=i;
while(!v[j]){
tmp++;
v[j]=;
j=a[j];
}
ans=lcm(ans,tmp);
}
}
cout<<ans<<endl;
}
return ;
}
 

poj-2369-置换的更多相关文章

  1. poj 2369 Permutations 置换

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

  2. poj 2369 Permutations (置换入门)

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

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

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

  4. poj 2369 Permutations - 数论

    We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...

  5. poj 3270 置换

    poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...

  6. POJ 2369 Permutations(置换群概念题)

    Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...

  7. POJ 2369 Permutations

    傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  8. poj 2369 Permutations 更换水称号

    寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...

  9. poj 2369(置换群)

    Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3041   Accepted: 1641 Desc ...

  10. poj 3270(置换 循环)

    经典的题目,主要还是考思维,之前在想的时候只想到了在一个循环中,每次都用最小的来交换,结果忽略了一种情况,还可以选所有数中最小的来交换一个循环. Cow Sorting Time Limit: 200 ...

随机推荐

  1. (转) The Incredible PyTorch

    转自:https://github.com/ritchieng/the-incredible-pytorch The Incredible PyTorch What is this? This is ...

  2. C++笔记(2017/2/9)

    this指针 this指针作用就是指向成员函数所作用的对象. 非静态成员函数中可以直接使用this来代表指向该函数作用的对象的指针. 静态成员函数中不能使用this指针. 静态成员 static 定义 ...

  3. HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)

    题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...

  4. 2、iptables基本应用

    iptables:规则管理工具 添加.修改.删除.显示等: 规则和链有计数器: pkts:  由规则或链所匹配到的报文的个数: bytes:由规则或链匹配到的所有报文大小之和: iptables命令: ...

  5. 【Python】【有趣的模块】【sys&time&os】

    [模块] sys.path.append('C:/Users/wangxue1/PycharmProjects/selenium2TestOne') 然后就可以直接import 这个路径下的模块了 [ ...

  6. django 应用中获取访问者ip地址

    通常访问者的IP就在其中,所以我们可以用下列方法获取用户的真实IP: #X-Forwarded-For:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP,只有在通过了HTTP 代理或者负载 ...

  7. [osg][原]osg的坐标系:使用右手法则Y轴向前、X向、右Z向上。camare的默认姿态:向下看(Z轴负向),头向前(Y轴正向)

    参考:http://blog.csdn.net/tmljs1988/article/details/7561887 图中上半边为opengl坐标系,下半边的osg坐标系: osg::Camare的默认 ...

  8. PSFTP用法

    PSFTP是PuTTY SFTP客户端,用于本地与服务器间安全传输文件(使用SSH连接). 1. 启动PSFTP 在Windows命令提示符中输入 set PATH=C:/PSFTP.exe所在路径; ...

  9. Lua和C++交互 学习记录之四:全局table交互

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...

  10. vue 上传单个图片自定义增加progress改良用户体验

    <el-tab-pane label="开发商logo" name="first" style="position: relative;&quo ...