CodeForces - 2B
There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that
- starts in the upper left cell of the matrix;
- each following cell is to the right or down from the current cell;
- the way ends in the bottom right cell.
Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.
The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
3
1 2 3
4 5 6
7 8 9
0
DDRR
题意:
一个数字矩阵,只能向下走或者向右走,从左上角,到右下角所有数字的乘积末尾最少有几个0,输出其个数,并且输出路径
做法:
预处理,每个数字有多少个2的质因子和5的质因子,分别储存为a[i][j][0]和a[i][j][1]
记忆化搜索,f[x][y][0]为x,y为终点经过的个点的2的质因子的个数的和
f[x][y][1]为x,y为终点经过的个点的5的质因子的个数的和
f[x][y][k] = min(f[x-1][y][k],f[x][y-1][k] )+a[x][y][k]
特殊处理0的情况,把0当成10,去处理,看最小答案是否大于1,如果大于1,那么最优答案就是1,也就是经过0的任意路径。
1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 #include<cstring> 5 int f[1002][1002][2]; 6 int a[1002][1002][2]; 7 int fx[1002][1002][2]; 8 int v[1002][1002][2]; 9 int dfs(int x,int y,int k){ 10 if(x==1&&y==1){ 11 if(k==0) 12 return a[1][1][0]; 13 if(k==1) 14 return a[1][1][1]; 15 } 16 if(x<=0||y<=0) 17 return 1e7; 18 if(v[x][y][k]!=0) 19 return f[x][y][k]; 20 v[x][y][k]=1; 21 int ans; 22 ans = dfs(x-1,y,k); 23 fx[x][y][k] = 0; 24 int ans2=dfs(x,y-1,k); 25 if(ans2<ans){ 26 ans = ans2; 27 fx[x][y][k] =1; 28 } 29 if(k==0) 30 f[x][y][k] = ans + (a[x][y][k]); 31 if(k==1) 32 f[x][y][k] = ans + (a[x][y][k]); 33 return f[x][y][k]; 34 } 35 void out(int x,int y,int k){ 36 // cout<<x<<" "<<y<<endl; 37 if(x<=0||y<=0) 38 return; 39 if(x==1&&y==1) 40 return; 41 if(fx[x][y][k]==0){ 42 out(x-1,y,k); 43 putchar('D'); 44 } 45 else{ 46 out(x,y-1,k); 47 putchar('R'); 48 } 49 } 50 int main(){ 51 int n; 52 scanf("%d",&n); 53 int num=0; 54 int numx,numy; 55 int x; 56 for(int i=1;i<=n;i++){ 57 for(int j=1;j<=n;j++){ 58 scanf("%d",&x); 59 if(x==0) 60 a[i][j][1]=a[i][j][0]=1,num+=1,numx=i,numy=j; 61 else{ 62 a[i][j][1]=a[i][j][0]=0; 63 int p=x; 64 while(p%2==0){ 65 a[i][j][0]+=1; 66 p/=2; 67 } 68 p=x; 69 while(p%5==0){ 70 a[i][j][1]+=1; 71 p/=5; 72 } 73 } 74 } 75 } 76 int pt; 77 pt = dfs(n,n,0); 78 int pt2; 79 pt2 = dfs(n,n,1); 80 int k = 0; 81 if(pt2<pt){ 82 pt=pt2; 83 k=1; 84 } 85 int ans = pt; 86 /* for(int i=1;i<=n;i++){ 87 for(int j=1;j<=n;j++){ 88 cout<<dfs(i,j,0)<<" "; 89 } 90 cout<<endl; 91 } 92 cout<<endl<<endl<<endl; 93 for(int i=1;i<=n;i++){ 94 for(int j=1;j<=n;j++){ 95 cout<<dfs(i,j,1)<<" "; 96 } 97 cout<<endl; 98 }*/ 99 if(num==0||ans<=1){ 100 printf("%d\n",ans); 101 out(n,n,k); 102 return 0; 103 } 104 printf("%d\n",1); 105 for(int i=1;i<numx;i++) 106 putchar('D'); 107 for(int i=1;i<numy;i++) 108 putchar('R'); 109 for(int i=numx;i<n;i++) 110 putchar('D'); 111 for(int i=numy;i<n;i++) 112 putchar('R'); 113 return 0; 114 }