a53

permutarePow_of

a53
Dec 2nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define LMAX 10005
  4. typedef int Huge[LMAX];
  5. void Set(Huge &A){
  6. memset(A,0,sizeof(A));
  7. }
  8. void Atrib(Huge &A,int x){
  9. Set(A);
  10. if(!x) A[++A[0]]=0;
  11. while(x){
  12. A[++A[0]]=x%10;
  13. x/=10;
  14. }
  15. }
  16. void Write(Huge A){
  17. for(int i=A[0];i;--i)
  18. printf("%d",A[i]);
  19. printf("\n");
  20. }
  21. void ProdMic(Huge &A,int x){
  22. if(x==0){
  23. Atrib(A,0);
  24. return ;
  25. }
  26. int i,r=0;
  27. for(i=1;i<=A[0]||r;++i){
  28. int aux=r+A[i]*x;
  29. A[i]=aux%10;
  30. r=aux/10;
  31. }
  32. A[0]=i-1;
  33. }
  34. #define NMAX 1000000
  35. int G[NMAX+5];
  36. bool Viz[NMAX+5];
  37. char fact[NMAX+5];
  38. void Descomp(int x){
  39. int d=2;
  40. while(d*d<=x){
  41. char p=0;
  42. while(x%d==0){
  43. ++p;
  44. x/=d;
  45. }
  46. fact[d]=max(fact[d],p);
  47. ++d;
  48. }
  49. if(x>1) fact[x]=max(fact[x],(char)1);
  50. }
  51. int DFS(int Start){
  52. /// Calculeaza lungimea unui circuit
  53. Viz[Start]=1;
  54. int len=1,poz=G[Start];
  55. while(poz!=Start){
  56. Viz[poz]=1;
  57. ++len;
  58. poz=G[poz];
  59. }
  60. return len;
  61. }
  62. Huge CMMMC;
  63. int main(){
  64. freopen("permutarepow.in","r",stdin);
  65. freopen("permutarepow.out","w",stdout);
  66. int n;
  67. scanf("%d",&n);
  68. for(int i=1;i<=n;++i)
  69. scanf("%d",&G[i]);
  70. for(int i=1;i<=n;++i)
  71. if(Viz[i]==0)
  72. Descomp(DFS(i));
  73. Atrib(CMMMC,1);
  74. for(int i=1;i<=n;++i){
  75. int P=1;
  76. for(char j=1;j<=fact[i];++j)
  77. P*=i;
  78. if(P!=1) ProdMic(CMMMC,P);
  79. }
  80. Write(CMMMC);
  81. return 0;
  82. }
Add Comment
Please, Sign In to add comment