Advertisement
a53

zero1

a53
Oct 7th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("zero1.in");
  4. ofstream fout("zero1.out");
  5. int a2[505][505] , a5[505][505] , n , d2[505][505] , d5[505][505];
  6. queue< pair<int , int> > q;
  7. int dx[] = {0 , 1};
  8. int dy[] = {1 , 0};
  9.  
  10. void Citire()
  11. {
  12. int i , j , x;
  13. fin >> n;
  14. for(i = 1; i <= n; i++)
  15. for(j = 1; j <= n; j++)
  16. {
  17. fin >> x;
  18. if(x == 0)
  19. {
  20. a2[i][j] = a5[i][j] = -1;
  21. continue;
  22. }
  23. int e = 0;
  24. while(x % 2 == 0)
  25. {
  26. x /= 2;
  27. e++;
  28. }
  29. a2[i][j] = e;
  30. e = 0;
  31. while(x % 5 == 0)
  32. {
  33. x /= 5;
  34. e++;
  35. }
  36. a5[i][j] = e;
  37. }
  38. }
  39.  
  40. void Bordare()
  41. {
  42. int i;
  43. for(i = 0; i <= n + 1; i++)
  44. a2[i][0] = a2[0][i] = a2[i][n + 1] = a2[n + 1][i] = -1;
  45. for(i = 0; i <= n + 1; i++)
  46. a5[i][0] = a5[0][i] = a5[i][n + 1] = a5[n + 1][i] = -1;
  47. }
  48.  
  49. void Lee2()
  50. {
  51. int i , j , x , y , k;
  52. for(i = 1; i <= n; i++)
  53. for(j = 1; j <= n; j++)
  54. d2[i][j] = 1000;
  55. q.push(make_pair(1 , 1));
  56. d2[1][1] = a2[1][1];
  57. while(!q.empty())
  58. {
  59. i = q.front().first;
  60. j = q.front().second;
  61. q.pop();
  62. for(k = 0; k < 2; k++)
  63. {
  64. x = i + dx[k];
  65. y = j + dy[k];
  66. if(a2[x][y] != -1 && d2[x][y] > d2[i][j] + a2[x][y])
  67. {
  68. d2[x][y] = d2[i][j] + a2[x][y];
  69. q.push(make_pair(x , y));
  70. }
  71.  
  72. }
  73. }
  74. }
  75.  
  76. void Lee5()
  77. {
  78. int i , j , x , y , k;
  79. for(i = 1; i <= n; i++)
  80. for(j = 1; j <= n; j++)
  81. d5[i][j] = 1000;
  82. q.push(make_pair(1 , 1));
  83. d5[1][1] = a5[1][1];
  84. while(!q.empty())
  85. {
  86. i = q.front().first;
  87. j = q.front().second;
  88. q.pop();
  89. for(k = 0; k < 2; k++)
  90. {
  91. x = i + dx[k];
  92. y = j + dy[k];
  93. if(a5[x][y] != -1 && d5[x][y] > d5[i][j] + a5[x][y])
  94. {
  95. d5[x][y] = d5[i][j] + a5[x][y];
  96. q.push(make_pair(x , y));
  97. }
  98.  
  99. }
  100. }
  101. }
  102.  
  103. void Afisare()
  104. {
  105. int x;
  106. x = min(d2[n][n] , d5[n][n]);
  107. fout << x << "\n";
  108. }
  109.  
  110. int main()
  111. {
  112. Citire();
  113. Bordare();
  114. Lee2();
  115. Lee5();
  116. Afisare();
  117. fin.close();
  118. fout.close();
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement