分别按2和5dp一次,求到达每个位置时的2或5的最小个数
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ls rt<<1
#define rs ls1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-9)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 100000007
#define pi acos(-1.0)
#define M 1000+5
int g[M][M],d[2][M][M],t[2][M][M];
int n,flag,ax,ay; char p[2][M][M];
int cnt(int x,int y){
int n=0;
while(x%y==0)x/=y,n++;
return n;
}
int dp(int f){
int i,j;
d[f%2][0][0]=t[f%2][0][0];
for(i=0;i<n;i++)
for(j=0;j<n;j++){
if(i+1<n&&d[f%2][i][j]+t[f%2][i+1][j]<d[f%2][i+1][j]){
d[f%2][i+1][j]=d[f%2][i][j]+t[f%2][i+1][j];
p[f%2][i+1][j]='U';
}
if(j+1<n&&d[f%2][i][j]+t[f%2][i][j+1]<d[f%2][i][j+1]){
d[f%2][i][j+1]=d[f%2][i][j]+t[f%2][i][j+1];
p[f%2][i][j+1]='L';
}
}
return d[f%2][n-1][n-1];
}
void print(int i,int j,int f){
int x;
if(i==0&&j==0)return;
if(p[f%2][i][j]=='U') print(i-1,j,f),x=1;
else if(p[f%2][i][j]=='L')print(i,j-1,f),x=2;
if(x==1) putchar('D');
else if(x==2)putchar('R');
}
int main(){
int i,j,x;
while(~scanf("%d",&n)){
flag=0;
memset(d,127,sizeof(d));
memset(t,0,sizeof(t));
memset(p,0,sizeof(p));
for(i=0;i<n;i++)
for(j=0;j<n;j++){
scanf("%d",&g[i][j]);
if(g[i][j]!=0){
t[0][i][j]=cnt(g[i][j],2);
t[1][i][j]=cnt(g[i][j],5);
}else{
flag=1;
ax=i;ay=j;
t[0][i][j]=t[1][i][j]=1;
}
}
i=dp(2); j=dp(5);
x=min(i,j);
if(x>=1&&flag==1){
puts("1");i=j=0;
while(i<ax) putchar('D'),i++;
while(j<n-1)putchar('R'),j++;
while(i<n-1)putchar('D'),i++;
}else if(x==i){
printf("%d\n",x);
print(n-1,n-1,2);
}else if(x==j){
printf("%d\n",x);
print(n-1,n-1,5);
}
puts("");
}
return 0;
}