dp题,考虑最小个数的2--n2 和最小个数的5--n5 如果n2 < n5 那0的个数就是n2 因为最小的n5大于n2
如果出现0 设0的n2=n5=1 如果结果大于等于1 就输出1并按0的路线
#include<cstdio>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
const int F=0,T=1;
const int maxn=1007,
maxf=2;
const int inf=1<<30;
int dp[maxf][maxn][maxn];
int a[maxn][maxn][maxf];
void search(int tp,int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
dp[tp][i][j]=min(dp[tp][i-1][j],dp[tp][i][j-1]) + a[i][j][tp];
}
}
}
void getway(int tp,int n)
{
int x,y;
x=y=n;
stack<char> st;
while(x!=1 || y!=1)
{
if(x!=1&&dp[tp][x][y]==dp[tp][x-1][y] + a[x][y][tp])
{
x--;
st.push('D');
}
else
{
y--;
st.push('R');
}
}
while(!st.empty())
{
putchar(st.top());
st.pop();
}
puts("");
}
int main()
{
int n;
while(~scanf("%d",&n))
{
bool hava0=false;
int i,j,t,zx,zy;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&t);
if(t==0)
{
zx=i;
zy=j;
hava0=true;
a[i][j][T]=a[i][j][F]=1;
}
else
{
a[i][j][T]=0;
while(t%2==0)
{
a[i][j][T]++;
t/=2;
}
a[i][j][F]=0;
while(t%5==0)
{
a[i][j][F]++;
t/=5;
}
}
}
}
for(i=0;i<=n;i++)
{
dp[T][0][i]=dp[F][0][i]=inf;
dp[T][i][0]=dp[F][i][0]=inf;
}
dp[T][0][1]=dp[F][0][1]=0;
search(T,n);
search(F,n);
int ans=T;
if(dp[1-ans][n][n] < dp[ans][n][n])
{
ans=1-ans;
}
if(hava0&&dp[ans][n][n]>=1)
{
puts("1");
int t=zx-1;
while(t--)
putchar('D');
t=zy-1;
while(t--)
putchar('R');
t=n-zx;
while(t--)
putchar('D');
t=n-zy;
while(t--)
putchar('R');
puts("");
}
else
{
printf("%d\n",dp[ans][n][n]);
getway(ans,n);
}
}
return 0;
}