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 }
     
  • 相关阅读:
    120. Triangle
    Effective C++ 条款36 绝不重新定义继承而来的non-virtual函数
    Effective C++ 条款31 将文件中间的编译依存关系降至最低
    Effective C++ 条款35 考虑virtual函数以外的其他选择
    Effective C++ 条款34 区分接口继承和实现继承
    Effective C++ 条款33 避免遮掩继承而来的名称
    Effective C++ 条款32 确定你的public继承塑模出is-a关系
    Effective C++ 条款30 透彻了解inlining的里里外外
    Effective C++ 条款29 为"异常安全"而努力是值得的
    Effective C++ 条款28 避免返回handles指向对象内部成分
  • 原文地址:https://www.cnblogs.com/zzqc/p/9442684.html
Copyright © 2011-2022 走看看