标签:
找循环节,分解质因数,求LCM
2 3 1 3 2 6 2 3 4 5 6 1
2 6
/* ***********************************************
Author :CKboss
Created Time :2015年08月17日 星期一 15时04分29秒
File Name :HDOJ5392.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int LL;
const int maxn=3003000;
int n;
int a[maxn];
bool vis[maxn];
vector<LL> loop;
void nextInt(int &x)
{
bool flag=false;
char ch; x=0;
while(ch=getchar())
{
if(ch>='0'&&ch<='9')
{
flag=true; x=x*10+ch-'0';
}
else
{
if(flag==true) break;
}
}
}
int prime[maxn/10],pn;
int wei[220000];
bool used[maxn];
void init()
{
memset(used,true,sizeof(used));
for(int i=2;i<=maxn;i++)
{
if(used[i])
{
prime[pn++]=i;
for(int j=i*2;j<=maxn;j+=i) used[j]=false;
}
}
}
const LL mod=3221225473LL;
LL quickPow(LL x,int m)
{
LL e=1LL;
while(m)
{
if(m&1) e=(e*x)%mod;
x=(x*x)%mod; m/=2;
}
return e%mod;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init();
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
loop.clear();
memset(wei,0,sizeof(wei));
memset(vis,false,sizeof(vis));
nextInt(n);
bool gogo=true;
for(int i=1;i<=n;i++)
{
nextInt(a[i]);
if(gogo&&a[i]!=i) gogo=false;
}
if(gogo)
{
puts("0"); continue;
}
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
int cur=0,u=i;
while(true)
{
if(vis[u]) break;
vis[u]=true; cur++;
u=a[u];
}
loop.push_back(cur);
}
for(int i=0,sz=loop.size();i<sz;i++)
{
int x=loop[i];
int nt=0;
while(x!=1)
{
if(x%prime[nt]==0)
{
int temp=0;
while(x%prime[nt]==0)
{
temp++; x/=prime[nt];
}
wei[nt]=max(wei[nt],temp);
}
nt++;
}
}
LL ans=1;
for(int i=0;i<220000;i++)
{
if(wei[i])
{
ans=(ans*quickPow(prime[i],wei[i]))%mod;
}
}
cout<<ans<<endl;
}
return 0;
}
版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss
HDOJ 5392 Infoplane in Tina Town LCM
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/47726993