Advertisement
a53

PermutarePow_50p

a53
Sep 30th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 1000005
  3. using namespace std;
  4. ifstream fin("permutarepow.in");
  5. ofstream fout("permutarepow.out");
  6. int n,permutation[N];
  7. unsigned long long int maxlen=1;
  8. typedef void(*cyclefn)(const int *,unsigned long long int);
  9.  
  10. unsigned long long int gcd(unsigned long long int a,unsigned long long int b)
  11. {
  12. if(b==0)
  13. return a;
  14. return gcd(b,a%b);
  15. }
  16.  
  17. void permutation_cycles_recursive(const int *permutation,int *visited,int start,int current,int *path,int length,cyclefn fun)
  18. {
  19. visited[current]=1;
  20. path[length]=current;
  21. if(start==current&&length>0)
  22. fun(path,length);
  23. else
  24. permutation_cycles_recursive(permutation,visited,start,permutation[current],path,length+1,fun);
  25. }
  26.  
  27. void permutation_cycles(const int *permutation,int n,cyclefn fun)
  28. {
  29. int *visited=new int[n];
  30. int *path=new int[n];
  31. if(!(visited&&path))
  32. {
  33. free(visited);
  34. free(path);
  35. return;
  36. }
  37. for(int i=0;i<n;++i)
  38. if(!visited[i])
  39. permutation_cycles_recursive(permutation,visited,i,i,path,0,fun);
  40. free(visited);
  41. free(path);
  42. }
  43.  
  44. void print(const int *cycle,unsigned long long int length)
  45. {
  46. if(length>1)
  47. maxlen=1ULL*maxlen*length /gcd(maxlen,length);
  48. }
  49.  
  50. int main()
  51. {
  52. fin>>n;
  53. for(int i=1;i<=n;++i)
  54. fin>>permutation[i];
  55. ++n;
  56. permutation_cycles(permutation,n,print);
  57. fout<<maxlen;
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement