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. methods 方法选项

    最简单的使用方法,一个数字,每点击一下按钮加1 html <div id="app"> <span v-text="number">&l ...

  2. C++笔记(2018/2/6)

    引用 & 某个变量的引用,等价于这个变量,相当于该变量的一个别名. 定义引用时一定要将其初始化成引用某个变量. 初始化后,它就一直引用该变量,不会再引用别的变量了. 通过引用所做的读写操作,会 ...

  3. Docker3之Swarm

    Make sure you have published the friendlyhello image you created by pushing it to a registry. We’ll ...

  4. vue.js精讲02

    2017-09-17 笔记及源码地址 : https://github.com/wll8/vue_note vue 中的事件深入. 事件: @click/mouseover…事件简写: @ 如 @cl ...

  5. Jquery 点击事件重复获取叠加 (一)

    用jquery添加绑定事件 添加多少次 点击的时候就触发多少次 如果想解决这个问题 就在点击函数里先用 $(对象).off("click") 取消上一次的点击事件 上码: $(&q ...

  6. netstat 在windows下和Linux下查看网络连接和端口占用

    假设忽然起个服务,告诉我8080端口被占用了,OK,我要去看一下是什么服务正在占用着,能不能杀 先假设我是在Windows下: 第一列: Proto 协议 第二列: 本地地址[ip+端口] 第三列:远 ...

  7. Jmeter 二次开发 将CSV Data Set Config添加从哪一行开始读数据

    经常遇到性能测试的时候,有100万条数据,才用了5万条,中途因为某些原因停止了,继续用的时候, 要么要清除DB中数据,要么要清除数据源中的数据, 觉得特别麻烦, 希望改写下代码,将 Ignore fi ...

  8. isnull和sum的关系

    这是我刚刚写存储过程的时候意识到的一个问题!!! 先问大家这样一个问题,print 100+null  等于多少? 在一组数据统计的过程中,只要使用到sum函数,就必须使用isnull函数包含起来,因 ...

  9. vue-cli3快速创建项目

    文档:https://cli.vuejs.org/zh/guide/ 条件: npm 更至最新 node >=8.9 1.全局安装 npm install -g @vue/cli 或 yarn ...

  10. ImageConverter引起的 invalid address or address of corrupt block 0xb7feab58 passed to dlfree

    虹软人脸识别,其方法要传NV21格式的byte[], github上有一个虹软的Demo,是不是虹软工作人员写的不清楚,这个Demo里bitmap转NV21格式byte[]用的是一个第三方库https ...