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. vue笔记

    安装vue脚手架工具 sudo cnpm install -g vue-cli

  2. monkey工具使用中遇到的问题之二:尝试了各种方法通过adb都无法找到设备

    测试环境: 1.用的是adt-bundle-windows-x86_64-20140702里面的adb 2.用的是手机模拟器(夜神) 问题描述: 已搭建好adb的环境,输入adb,可以看到以下相关信息 ...

  3. CrashMonkey4IOS App测试

    下载地址:https://github.com/vigossjjj/CrashMonkey4IOS 根据下载地址里面的说明安装一下,以下进行配置 1.进入CrashMonkey4IOS-master/ ...

  4. wpf——三维学习1

    以下xmal是我从msdn上复制下来的.是用于在wpf中创建3d模型的实例链接https://msdn.microsoft.com/zh-cn/library/ms747437.aspx看它的使用方式 ...

  5. centos6.6 安装jdk1.7

    1:在oracle官网下载jdk liunx版本,放入到虚拟机中 2:解压jdk,解压命令 tar -xvzf jdk-7u15-linux-x64.tar.gz 解压完成(如下图) 3:在/usr/ ...

  6. 111. for(元素变量x:遍历对象obj)

    package com.chongrui.test;/* * for(元素变量x:遍历对象obj){ * 引用X的java语句 *  * } *  *  * */public class test { ...

  7. vs使用

    1.控制dll是否生成到本地,如图,右击dll,选择属性,设置复制到本地为true即可

  8. https单向认证和双向认证

    单向认证: .clinet<--server .clinet-->server .client从server处拿到server的证书,通过公司的CA去验证该证书,以确认server是真实的 ...

  9. 【Java EE 学习 55】【酒店会员管理系统项目总结】

    本酒店会员管理系统使用了SSH框架和传值播客提供的协同OA静态页面. 项目地址:https://github.com/kdyzm/HotelMembersManagement 一.需求分析 酒店会员管 ...

  10. Swift -运算符和循环结构

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0. ...