poj 2369 Permutations

这是一道简单的置换群的问题;

View Code
#include<iostream>

 #include<cstdio>

 #include<cstdlib>

 #include<algorithm>

 #include<cmath>

 #include<queue>

 #include<set>

 #include<map>

 #include<cstring>

 #include<vector>

 #include<string>

 #define LL long long

 using namespace std;

 int Gcd( int a , int b )

 {

     return b == 0? a : Gcd( b , a % b );    

 }

 int Solve( int N , int num[] )

 {

    int ans = 1;

    bool visit[1024] = { 0 };

    for( int i = 1; i <= N; i ++ )

    {

         if( !visit[i] )

         {

             visit[i] = true;

             int t = num[i],cnt=1;

             while( i != t )

             {

                   visit[t] = true;

                   cnt ++;

                   t = num[t];

             }

             ans = (ans/Gcd( ans , cnt ))*cnt;

         }        

    }

    return ans;    

 }

 int main(  )

 {

     int num[1024],N;

     while( scanf( "%d",&N )==1 )

     {

          for( int i = 1 ; i <= N ; i ++ )

          {

               scanf( "%d",&num[i] );        

          }    

          printf( "%d\n",Solve( N ,num ) );

     }

     //system( "pause" );

     return 0;

 }

 

你可能感兴趣的:(poj)