Cod sursa(job #166781)

Utilizator MirageRobert Sandu Mirage Data 28 martie 2008 14:44:29
Problema Sandokan Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.52 kb
#include<stdio.h>
int v[100000],p;
int ciclu(int x,int c){
	++p;
	if(x==c)
		return p;
	return ciclu(v[x],c);
}
int cmmdc(int a,int b){
	if(!b)
		return a;
	return cmmdc(b,a%b);
}
int cmmmc(int a,int b){
	return (a*b)/cmmdc(a,b);
}
int main () {
	freopen("perm2.in","r",stdin);
	freopen("perm2.out","w",stdout);
	int n,i,x,pr=1;
	scanf("%d",&n);
	for(i=1;i<=n;++i)
		scanf("%d",&v[i]);
	for(i=1;i<=n;++i){
		p=0;
		x=ciclu(v[i],i);
		if(i>=2)
			pr=cmmmc(pr,x);
	}
	printf("%d\n",pr);
	return 0;
}