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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章