zoukankan      html  css  js  c++  java
  • poj-2369-置换

      

    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|次就会回到初始状态,所以找到一个最小公倍数使得所有循环都回到初始状态。
      
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<map>
     5 #include<set>
     6 #include<vector>
     7 #include<algorithm>
     8 #include<cmath> 
     9 using namespace std;
    10 #define LL long long 
    11 #define PI acos(-1.0)
    12 int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
    13 int lcm(int a,int b){return a*b/gcd(a,b);}
    14 int a[1010];
    15 bool v[1010];
    16 int main()
    17 {
    18     int T,n,m,k,i,j,d;
    19     while(scanf("%d",&n)!=EOF){
    20         int ans=1;
    21         for(i=1;i<=n;++i){
    22             scanf("%d",&a[i]);
    23         }
    24         memset(v,0,sizeof(v));
    25         for(i=1;i<=n;++i){
    26             if(!v[i]){
    27                 int tmp=0;
    28                 j=i;
    29                 while(!v[j]){
    30                     tmp++;
    31                     v[j]=1;
    32                     j=a[j];
    33                 }
    34                 ans=lcm(ans,tmp);
    35             }
    36         }
    37         cout<<ans<<endl;
    38     }
    39     return 0;
    40 }
     
  • 相关阅读:
    免费部署Woocall到您自己的网站上
    服务器控件开发之复杂属性
    删除数据库的所有存储过程、主键、外键、索引等
    怎样在dropdownlist的每一项前加一个或多个空格
    Java的内部类学习
    StringUtils全览 (转)
    Java异常大全
    Java web 开发小问题总结(持续更新中)
    Java常用方法总结(持续更新中)
    Python 常用函数
  • 原文地址:https://www.cnblogs.com/zzqc/p/9442684.html
Copyright © 2011-2022 走看看