• [ACM] poj 2369 Permutations (置换群循环节长度)


    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

    Source


    解题思路:

    题目要求为给定一个排列 4  1  5  2  3,问经过多少次置换可以重新回到这个排列。

    定理:


    什么是循环节。。比如

    1  2 3 4  5     从上向下看

    4 1 5 2 3
    
    1->4->2->1 (1,4,2)为一个循环节,长度为3    3->5->3(3,5)为一个循环节,长度为2    则所求的次数即定理中的k

    下面的样例解释转于博客   http://blog.csdn.net/cqlf__/article/details/7910849

    分析下样例
    1 2 3 4 5 

    原始序列: 4 1 5 2 3

    2 4 3 1 5
    p(p(1))=p(4)=2;
    p(p(2))=p(1)=4;
    p(p(3))=p(5)=3;
    ...
    ...

    1 2 5 4 3
    p(p(p(1)))=p(2)=1;
    p(p(p(2)))=p(4)=2;
    p(p(p(3)))=p(3)=5;
    ...
    ...

    4 1 3 2 5
    p(p(p(p(1))))=p(1)=4;
    p(p(p(p(2))))=p(2)=1;
    p(p(p(p(3))))=p(5)=3;
    2 4 5 1 3

    1 2 3 4 5

    就是两个循环节(1,2,4) 和(3,5) 我们只要求出lcm(循环节长度)便是其需要移动的次数




    代码:

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int gcd(int a,int b)
    {
        return b==0?a:gcd(b,a%b);
    }
    int lcm(int a,int b)//求最小公倍数
    {
        return a/gcd(a,b)*b;
    }
    
    const int maxn=1005;
    int num[maxn];
    int visit[maxn];
    
    int main()
    {
        int n;
        while(cin>>n)
        {
            for(int i=1;i<=n;i++)
                cin>>num[i];
            memset(visit,0,sizeof(visit));
            int res=1,flag;
            for(int i=1;i<=n;i++)
            {
                int count=0;//用来记录每个循环节的长度
                flag=i;//记录下标
                while(!visit[flag])//没有被访问过,不存在于之前的循环节中
                {
                    count++;
                    visit[flag]=1;
                    flag=num[flag];
                }
                if(count)//注意有可能count是0的情况,即外层循环循环到第i个数,而此时第i个数在之前寻找循环节中已经被访问过
                    res=lcm(count,res);
            }
            cout<<res<<endl;
        }
        return 0;
    }
    



  • 相关阅读:
    linux 配置nginx+php-cgi
    有个故事
    2014短学期实习报告
    快速排序
    C++之共用体
    不能言传,等于不会
    java 动态绑定
    编程的理论深度
    Java 多客户端版 2048 源码
    热河看待苦难
  • 原文地址:https://www.cnblogs.com/vivider/p/3697509.html
Copyright © 2020-2023  润新知