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. PHP新手常见的一些不好习惯(抄的 有待理解)

    1.不写注释(是个好习惯,不过也没必要每个语句都要写) 2.不使用可以提高生产效率的IDE工具 3.不使用版本控制 4.不按照编程规范写代码 5.不使用统一的方法 6.编码前不去思考和计划 7.在执行 ...

  2. android adb 命令详解

    ADB (Android Debug Bridge)  是android SDK中的工具,需要先配置环境变量才能使用.起调试桥的作用,可以管理安卓设备.(也叫debug工具) ---------查看设 ...

  3. Flex 1046: 找不到类型,或者它不是编译时常数;1180: 调用的方法 CompPropInfo 可能未定义

    导入项目之后一直报这个错误, 1046: 找不到类型,或者它不是编译时常数: 1180: 调用的方法 CompPropInfo 可能未定义 想这应该是没有把当前这个类编译进项目当中,找了半天也没有找到 ...

  4. freemarker IllegalAccessError 错误

    java.lang.IllegalAccessError: tried to access method freemarker.ext.servlet.AllHttpScopesHashModel.& ...

  5. 处理Https 异常记录 javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    http://blog.csdn.net/baidu_18607183/article/details/51595330 https://blogs.oracle.com/java-platform- ...

  6. PHP Math 函数

    abs() 绝对值. 3 acos() 反余弦. 3 acosh() 反双曲余弦. 4 asin() 反正弦. 3 asinh() 反双曲正弦. 4 atan() 反正切. 3 atan2() 两个参 ...

  7. angular1.x的简单介绍(二)

    首先还是要强调一下DI,DI(Denpendency Injection)伸手获得,主要解决模块间的耦合关系.那么模块是又什么组成的呢?在我看来,模块的最小单位是类,多个类的组合就是模块.关于在根模块 ...

  8. 当攻击者熟读兵法,Camouflage病毒实战演示暗度陈仓之计

    "明修栈道,暗度陈仓"的典故许多人都听说过,该典故出自楚汉争霸时期,刘邦意图进入关中,需要攻下关中咽喉之地--陈仓.韩信献出一计:表面上浩浩荡荡地修复通往陈仓的栈道以迷惑陈仓守将, ...

  9. JS 数据类型转换

    JS 数据类型转换 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把 ...

  10. PhpStorm/Xdebug安装使用

    安装环境:XAMPP;phpStorm版本10; windows 7 64bit. XAMPP.phpStorm 都直接安装在了D盘根目录,9999m目录建在D:\xampp\htocts下,即目录工 ...