Codeforces Beta Round #2B. The least round way

B. The least round way
time limit per test
5 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
3
1 2 3
4 5 6
7 8 9
output
0
DDRR

题意:给你一个n*n的矩阵,让你找一条路径使得路径上的数乘积零的个数最少,求出最少零的个数以及输出路径。

思路:零只能有2和5相乘得到,判断每个数的因子中2和5的个数,最后零的个数为2,5个数最小的一个;注意考虑有数为零的情况(只需将它记录,并把它看成10处理)

代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 99999999
#define N 1005
#define LL long long

int xn;
int dp2[N][N],ma[N][N],dp5[N][N];
char s2[N][N],s5[N][N],ss[2*N];
int slove2(int num)
{
    int ans=0;
    if(num==0)
    return 1;
    while(num%2==0)
    {
        ans++;
        num=num/2;
    }
    return ans;
}
int slove5(int num)
{
    int ans=0;
    if(num==0)
    return 1;
    while(num%5==0)
    {
        ans++;
        num=num/5;
    }
    return ans;
}
int main()
{
    int n;
    xn=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for( int j=1;j<=n;j++)
        {
            scanf("%d",&ma[i][j]);
            if(ma[i][j]==0)
            xn=i;
        }
    }
    memset(s2,0,sizeof(s2));
    memset(s5,0,sizeof(s5));
    memset(ss,0,sizeof(ss));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dp5[i][j]=inf;
            dp2[i][j]=inf;
        }
    }
    dp2[1][1]=slove2(ma[1][1]);
    dp5[1][1]=slove5(ma[1][1]);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i+1<=n)
            {
                int xx=dp2[i][j]+slove2(ma[i+1][j]);
                if(dp2[i+1][j]>xx)
                {
                    dp2[i+1][j]=xx;
                    s2[i+1][j]='D';
                }
                xx=dp5[i][j]+slove5(ma[i+1][j]);
                if(dp5[i+1][j]>xx)
                {
                    dp5[i+1][j]=xx;
                    s5[i+1][j]='D';
                }
            }
            if(j+1<=n)
            {
                int xx=slove2(ma[i][j+1])+dp2[i][j];
                if(dp2[i][j+1]>xx)
                {
                    dp2[i][j+1]=xx;
                    s2[i][j+1]='R';
                }
                xx=slove5(ma[i][j+1])+dp5[i][j];
                if(dp5[i][j+1]>xx)
                {
                    dp5[i][j+1]=xx;
                    s5[i][j+1]='R';
                }
            }
        }
    }
    int ans=min(dp2[n][n],dp5[n][n]);
    if(xn!=0&&ans>=1)
    {
        int i;
        printf("1\n");
        for(i=2;i<=xn;i++)
            printf("D");
        for(i=2;i<=n;i++)
            printf("R");
        for(i=xn+1;i<=n;i++)
            printf("D");
        printf("\n");
    }
    else
    {
        printf("%d\n",ans);
        int k;
        if(ans==dp2[n][n])
        {
            int i=n,j=n;
            k=0;
            while(i!=1||j!=1)
            {
                ss[k]=s2[i][j];
                k++;
                if(s2[i][j]=='D')
                i--;
                else
                j--;
            }
        }
        else
        {
            int i=n,j=n;
            k=0;
            while(i!=1||j!=1)
            {
                ss[k]=s5[i][j];
                k++;
                if(s5[i][j]=='D')
                i--;
                else
                j--;
            }
        }
        for(int i=k-1;i>=0;i--)
        {
            printf("%c",ss[i]);
        }
        printf("\n");
    }
    return 0;
}


发布了34 篇原创文章 · 获赞 0 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章