Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define N 1000005
- using namespace std;
- ifstream fin("permutarepow.in");
- ofstream fout("permutarepow.out");
- int n,permutation[N];
- unsigned long long int maxlen=1;
- typedef void(*cyclefn)(const int *,unsigned long long int);
- unsigned long long int gcd(unsigned long long int a,unsigned long long int b)
- {
- if(b==0)
- return a;
- return gcd(b,a%b);
- }
- void permutation_cycles_recursive(const int *permutation,int *visited,int start,int current,int *path,int length,cyclefn fun)
- {
- visited[current]=1;
- path[length]=current;
- if(start==current&&length>0)
- fun(path,length);
- else
- permutation_cycles_recursive(permutation,visited,start,permutation[current],path,length+1,fun);
- }
- void permutation_cycles(const int *permutation,int n,cyclefn fun)
- {
- int *visited=new int[n];
- int *path=new int[n];
- if(!(visited&&path))
- {
- free(visited);
- free(path);
- return;
- }
- for(int i=0;i<n;++i)
- if(!visited[i])
- permutation_cycles_recursive(permutation,visited,i,i,path,0,fun);
- free(visited);
- free(path);
- }
- void print(const int *cycle,unsigned long long int length)
- {
- if(length>1)
- maxlen=1ULL*maxlen*length /gcd(maxlen,length);
- }
- int main()
- {
- fin>>n;
- for(int i=1;i<=n;++i)
- fin>>permutation[i];
- ++n;
- permutation_cycles(permutation,n,print);
- fout<<maxlen;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement