a53

PermutarePow

a53
Sep 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin ("permutarepow.in");
  6. ofstream fout ("permutarepow.out");
  7.  
  8.  
  9. const int NMAX = 1000000;
  10.  
  11. int fr[NMAX + 5] , ans[10001] , a , nans , n , b[10001] , nb , aa;
  12.  
  13. bool viz[NMAX + 5];
  14.  
  15. inline int Rest()
  16. {
  17. int R = 0;
  18. for(int i = nans ; i >= 1 ; i--)
  19. R = (R * 10 + b[i]) % a;
  20. return R;
  21. }
  22.  
  23. inline void CMMDC()
  24. {
  25. int r , aux;
  26. while(a)
  27. {
  28. r = Rest();
  29. aux = a;
  30. nb = 0;
  31. while(aux)
  32. {
  33. b[++nb] = aux % 10;
  34. aux /= 10;
  35. }
  36. a = r;
  37. }
  38. }
  39.  
  40. inline void INPARTIRE()
  41. {
  42. int i , t = 0;
  43. for(i = nans ; i >= 1 ; i-- , t %= a)
  44. ans[i] = (t = t * 10 + ans[i]) / a;
  45. for(; nans > 1 && !ans[nans] ; --nans)
  46. ;
  47. }
  48.  
  49. inline void INMULTIRE()
  50. {
  51. int i , t = 0;
  52. for(i = 1 ; i <= nans || t ; i++ , t /= 10)
  53. ans[i] = (t += ans[i] * aa) % 10;
  54. nans = i - 1;
  55. }
  56.  
  57. int main()
  58. {
  59. int x , c , s;
  60. fin >> n;
  61. for(int i = 1 ; i <= n ; i++)
  62. fin >> x , fr[i] = x;
  63. for(int i = 1 ; i <= n ; i++)
  64. if(!viz[i] && i != fr[i])
  65. {
  66. c = fr[i];
  67. s = 1;
  68. viz[c] = true;
  69. while(c != i)
  70. {
  71. ++s;
  72. c = fr[c];
  73. viz[c] = true;
  74. }
  75. if(!nans)
  76. {
  77. while(s > 0)
  78. {
  79. ans[++nans] = s % 10;
  80. s /= 10;
  81. }
  82. }
  83. else
  84. {
  85. a = aa = s;
  86. nb = 0;
  87. for(int i = 1 ; i <= nans ; i++)
  88. b[++nb] = ans[i];
  89.  
  90. CMMDC();
  91. a = 0;
  92. for(int i = 1 ; i <= nb ; i++)
  93. a = a * 10 + b[i];
  94. INPARTIRE();
  95. INMULTIRE();
  96. }
  97. }
  98. if(!nans)
  99. {
  100. nans = 1;
  101. ans[nans] = 1;
  102.  
  103. }
  104. for(int i = nans ; i >= 1 ; i--)
  105. fout << ans[i];
  106. fout << "\n";
  107. fin.close();
  108. fout.close();
  109. return 0;
  110. }
Add Comment
Please, Sign In to add comment