hdu 1210 求置换循环节

这个题的置换恰好是有规律的,所以也不用把置换给存下来,然后只要求出置换的循环节就可以了。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 
 7 const int N = 200001;
 8 bool visit[N];
 9 
10 int gcd( int x, int y )
11 {
12     if ( !y ) return x;
13     return gcd( y, x % y );
14 }
15 
16 int lcm( int x, int y )
17 {
18     return x / gcd( x, y ) * y;
19 }
20 
21 int main ()
22 {
23     int n;
24     while ( scanf("%d", &n) != EOF )
25     {
26         memset( visit, false, sizeof(visit) );
27         int ans = 1;
28         for ( int i = 1; i <= 2 * n; i++ )
29         {
30             if ( visit[i] ) continue;
31             int len = 0, j = i;
32             while ( !visit[j] )
33             {
34                 visit[j] = true;
35                 len++;
36                 if ( j & 1 ) j = n + ( j + 1 ) / 2;
37                 else j = j / 2;
38             }
39             ans = lcm( ans, len );
40         }
41         printf("%d\n", ans);
42     }
43     return 0;
44 }

 

posted @ 2015-07-23 19:00  hxy_has_been_used  阅读(148)  评论(0编辑  收藏  举报