• 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

      题目大意是讲给出一个置换,定义它和它自己的合成运算,问它和它自己进行多少次合成运算后又变回了自己。

      根据置换的知识,任何一个置换都可以表示成轮换

      然后根据人生的经验和数学的直觉,循环周期等于当置换表示成轮换的合成的形式时,每个轮换中元素的个数的最小公倍数(每次每个轮换往前转一次,如果还不能理解,出门左转<组合数学>)。

    Code

     1 /**
     2  * poj
     3  * Problem#2369
     4  * Accepted
     5  * Time:16ms
     6  * Memory:692k
     7  */
     8 #include<iostream>
     9 #include<fstream> 
    10 #include<sstream>
    11 #include<algorithm>
    12 #include<cstdio>
    13 #include<cstring>
    14 #include<cstdlib>
    15 #include<cctype>
    16 #include<cmath>
    17 #include<ctime>
    18 #include<map>
    19 #include<stack>
    20 #include<set>
    21 #include<queue>
    22 #include<vector>
    23 #ifndef WIN32
    24 #define AUTO "%lld"
    25 #else
    26 #define AUTO "%I64d"
    27 #endif
    28 using namespace std;
    29 typedef bool boolean;
    30 #define inf 0xfffffff
    31 #define smin(a, b) (a) = min((a), (b))
    32 #define smax(a, b) (a) = max((a), (b))
    33 template<typename T>
    34 inline boolean readInteger(T& u) {
    35     char x;
    36     int aFlag = 1;
    37     while(!isdigit((x = getchar())) && x != '-' && x != -1);
    38     if(x == -1)    {
    39         ungetc(x, stdin);
    40         return false;
    41     }
    42     if(x == '-') {
    43         aFlag = -1;
    44         x = getchar();
    45     }
    46     for(u = x - '0'; isdigit((x = getchar())); u = u * 10 + x - '0');
    47     u *= aFlag;
    48     ungetc(x, stdin);
    49     return true;
    50 }
    51 
    52 template<typename T>
    53 T gcd(T a, T b) {
    54     if(b == 0)    return a;
    55     return gcd(b, a % b);
    56 }
    57 
    58 int n;
    59 int *f;
    60 
    61 inline void init() {
    62     readInteger(n);
    63     f = new int[(const int)(n + 1)];
    64     for(int i = 1; i <= n; i++)
    65         readInteger(f[i]);
    66 }
    67 
    68 int lcm = 1;
    69 boolean *visited;
    70 inline void solve() {
    71     visited = new boolean[(const int)(n + 1)];
    72     memset(visited, false, sizeof(boolean) * (n + 1));
    73     for(int i = 1; i <= n; i++) {
    74         if(!visited[i]) {
    75             int c = 0, j = i;
    76             while(!visited[j]) {
    77                 visited[j] = true;
    78                 j = f[j], c++;
    79             }
    80             lcm = lcm / gcd(c, lcm) * c;
    81         }
    82     }
    83     printf("%d", lcm);
    84 }
    85 
    86 int main() {
    87     init();
    88     solve();
    89     return 0;
    90 }
  • 相关阅读:
    多数据源配置
    Oracle创建JOB
    2新建Data Adapter
    注解的CRUD;重点:多对一和一对多处理
    Mybatis分页+使用注解开发!
    继续mybatis配置resultMap;讲解日志部分
    Today has a day off.
    Mybatis其他配置!
    Mybatis优化配置!
    Mybatis的CRUD
  • 原文地址:https://www.cnblogs.com/yyf0309/p/6629162.html
Copyright © 2020-2023  润新知