HDU5392——最小公倍数——Infoplane in Tina Town

http://acm.hdu.edu.cn/showproblem.php?pid=5392

/*
先把循环长度提取出来,再求这些长度的最小公倍数
把质因数都分解出来,每个质因数取最大所有数之积就是最小公倍数
*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-17 11:11:42
* File Name     :HDU5392.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 3e6 + 10;
const int INF = 0x3f3f3f3f;
const long long  MOD = 3221225473;
int vis[MAXN];
int a[MAXN];
int b[MAXN];

int main(){
    int T;
    int n;
    scanf("%d", &T);
    while(T--){
        memset(b, 0, sizeof(b));
        memset(vis, 0, sizeof(vis));
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++){
            int k = a[i];
            int len = 1;
            if(vis[k]) continue;
                while(k!=i){
                    vis[k] = 1;
                    len++;
                    k = a[k];
                }
                vis[k] = 1;
           // printf("%d", len);
            for(int j = 2; j*j <= len; j++){
                if(len % j == 0){
                    int c = 0;
                    while(len % j == 0){
                        len /= j;
                        c++;
                    }
                    b[j] = max(b[j], c);
                }
            }
            b[len] = max(b[len], 1);
        }
        ll ans = 1;
        for(int i = 2; i <= n; i++){
            for(int j = 0; j < b[i]; j++){
                ans = ans * i % MOD;
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

  

posted @ 2015-08-17 14:02  Painting、时光  阅读(346)  评论(0编辑  收藏  举报