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. 论文阅读之: Hierarchical Object Detection with Deep Reinforcement Learning

    Hierarchical Object Detection with Deep Reinforcement Learning NIPS 2016 WorkShop  Paper : https://a ...

  2. SQL Server 常见数据类型介绍

    数据表是由多个列组成,创建表时必须明确每个列的数据类型,以下列举SQL Server常见数据类型的使用规则,方便查阅. 整数类型 int 存储范围是-2,147,483,648到2,147,483,6 ...

  3. -第3章 jQuery方法实现下拉菜单显示和隐藏

    知识点 jquery 的引入方式 本地下载引入 在线引入 children 只获取子元素,不获取孙元素 show() 显示. hide() 隐藏. 完整代码 <!-- Author: XiaoW ...

  4. SAP按销售订单生产和标准结算配置及操作手册

    SAP按销售订单生产和标准结算配置及操作手册 http://blog.sina.com.cn/s/blog_6787c0b80101a3tl.html SAP按销售订单生产和标准结算配置及操作手册 S ...

  5. 理解 Redis(4) - 关于 string 的更多命令(SETEX, SETNX, INCR, DECR, MSET...)

    上一节介绍了关于字符串值的一些基本命令, 这一节将介绍一些进阶命令: 清理终端: 127.0.0.1:6379> clear 设置一个键值对, 同时设置过期时间为10秒: 127.0.0.1:6 ...

  6. centos nginx 中安装ssl证书 以及在项目中的使用

    今天阿里云的证书到期了,重新申请了一个,下面是从申请到安装以及结合项目使用的过程: 1.登录阿里云   2.在左侧找到SSL证书 3.申请免费的证书 4.下载证书 5.根据说明配置nginx 6.在项 ...

  7. SqlServer中 将某个表的某个字段改个默认值

    ALTER TABLE [dbo].[表名] ADD CONSTRAINT [DF_表名_列名] DEFAULT ((默认值)) FOR [列名] ALTER TABLE QA_API ADD CON ...

  8. vuex中的辅助函数 mapState,mapGetters, mapActions, mapMutations

    1.导入辅助函数 导入mapState可以调用vuex中state的数据 导入mapMutations可以调用vuex中mutations的方法 四个辅助函数 各自对应自己在vuex上的自己 2.ma ...

  9. mint19 源码安装python3.7

    基于Ubuntu 18.04  自带了3.6,可惜 3.7秒出. 一个原则是: 自带的3.6不要动 防止用apt install python3-XXX时版本不对. 理由: 自带的2.7和3.6都是让 ...

  10. Spring AOP 理论

    一.AOP AOP 产生的背景 “存在即合理”,任何一种理论或技术的产生,必然有它的原因.了解它产生的背景.为了解决的问题有助于我们更好地把握AOP的概念. 软件开发一直在寻求一种高效开发.护展.维护 ...