Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("zero1.in");
- ofstream fout("zero1.out");
- int a2[505][505] , a5[505][505] , n , d2[505][505] , d5[505][505];
- queue< pair<int , int> > q;
- int dx[] = {0 , 1};
- int dy[] = {1 , 0};
- void Citire()
- {
- int i , j , x;
- fin >> n;
- for(i = 1; i <= n; i++)
- for(j = 1; j <= n; j++)
- {
- fin >> x;
- if(x == 0)
- {
- a2[i][j] = a5[i][j] = -1;
- continue;
- }
- int e = 0;
- while(x % 2 == 0)
- {
- x /= 2;
- e++;
- }
- a2[i][j] = e;
- e = 0;
- while(x % 5 == 0)
- {
- x /= 5;
- e++;
- }
- a5[i][j] = e;
- }
- }
- void Bordare()
- {
- int i;
- for(i = 0; i <= n + 1; i++)
- a2[i][0] = a2[0][i] = a2[i][n + 1] = a2[n + 1][i] = -1;
- for(i = 0; i <= n + 1; i++)
- a5[i][0] = a5[0][i] = a5[i][n + 1] = a5[n + 1][i] = -1;
- }
- void Lee2()
- {
- int i , j , x , y , k;
- for(i = 1; i <= n; i++)
- for(j = 1; j <= n; j++)
- d2[i][j] = 1000;
- q.push(make_pair(1 , 1));
- d2[1][1] = a2[1][1];
- while(!q.empty())
- {
- i = q.front().first;
- j = q.front().second;
- q.pop();
- for(k = 0; k < 2; k++)
- {
- x = i + dx[k];
- y = j + dy[k];
- if(a2[x][y] != -1 && d2[x][y] > d2[i][j] + a2[x][y])
- {
- d2[x][y] = d2[i][j] + a2[x][y];
- q.push(make_pair(x , y));
- }
- }
- }
- }
- void Lee5()
- {
- int i , j , x , y , k;
- for(i = 1; i <= n; i++)
- for(j = 1; j <= n; j++)
- d5[i][j] = 1000;
- q.push(make_pair(1 , 1));
- d5[1][1] = a5[1][1];
- while(!q.empty())
- {
- i = q.front().first;
- j = q.front().second;
- q.pop();
- for(k = 0; k < 2; k++)
- {
- x = i + dx[k];
- y = j + dy[k];
- if(a5[x][y] != -1 && d5[x][y] > d5[i][j] + a5[x][y])
- {
- d5[x][y] = d5[i][j] + a5[x][y];
- q.push(make_pair(x , y));
- }
- }
- }
- }
- void Afisare()
- {
- int x;
- x = min(d2[n][n] , d5[n][n]);
- fout << x << "\n";
- }
- int main()
- {
- Citire();
- Bordare();
- Lee2();
- Lee5();
- Afisare();
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement