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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值