1. 程式人生 > >codeforces 2B The least round way

codeforces 2B The least round way

i++ current logs long hat mem 題意 eof you

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.

Input

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).

Output

In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.

Example

Input
3
1 2 3
4 5 6
7 8 9
Output
0
DDRR

解題思路:
題意: 從左上角開始走,走到右下角,只能向右或向下走,將經過的數字相乘,求最後結果0最少的路徑
思路:若要使相乘後尾部有0,有兩種情況:1.其中包含2和5,記錄2和5的個數分別為a,b要求尾部的0最少那麽只要求經過的2或5最少的路徑即可,也就是取a和b中較小值,
2.當經過0時,不論其他數為多少,最後都為1個0,如果1情況大於1,則優先選擇2.


實現代碼:
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using
namespace std; int f[1001][1001][2],m,x,k; char g[1001][1001][2]; void fuck(int x,int y) { if(x==1&&y==1) return ; if(g[x][y][k]){ fuck(x-1,y),putchar(D); } else{ fuck(x,y-1),putchar(R); } } int main() { int i; cin>>m; memset(f,0,sizeof(f)); for(i=2;i<=m;i++){ f[i][0][0]=f[0][i][0]=f[i][0][1]=f[0][i][1] = inf; } for(int i=1;i<=m;i++) { for(int j=1;j<=m;j++){ scanf("%d",&k); if(!k) x = i; else{ while(k%2==0) ++f[i][j][0],k/=2; while(k%5==0) ++f[i][j][1],k/=5; } for(k=0;k<2;k++){ if(g[i][j][k]=f[i-1][j][k]<f[i][j-1][k]) f[i][j][k] += f[i-1][j][k]; else f[i][j][k] += f[i][j-1][k]; } } } k = f[m][m][1] < f[m][m][0]; if(x&&f[m][m][k]>1){ cout<<"1\n"; for(i=2;i<=x;i++) putchar(D); for(i=2;i<=m;i++) putchar(R); for(i=x+1;i<=m;i++) putchar(D); } else{ cout<<f[m][m][k]<<endl; fuck(m,m); } return 0; }

codeforces 2B The least round way