zoukankan      html  css  js  c++  java
  • 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
    ", ans);
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    git
    浏览器喧嚷过程
    B/S架构与C/S架构
    simpleDateFormat
    oracle中case when的用法
    Java程序利用Jdbc连接数据库
    List 和 Set与Map
    队列和栈
    toString方法分析
    java中的构造器
  • 原文地址:https://www.cnblogs.com/huoxiayu/p/4671418.html
Copyright © 2011-2022 走看看