Codeforces 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

题目大意:

给出一个非负整数方阵和方阵阶数,问从左上角走到右下角,所经过的所有数乘积末尾最少可以有几个零

以及最少零情况的路径


解题思路:

首先,可以想到这与经过整数的2,5因子数有关。

接着,两遍DP,第一遍以求出最少的2因子为标准,求出其可以的最少的零m2,第二遍则以5为标准求出m5。

 如果没有0的话,答案就是min(m2,m5)

如果有0的话,需要讨论一下

(1)g[0][0]为0,最少的0个数就是1,直接输出

(2)存在0区域将起点和终点完全隔离,那么最终答案也一定是1

(3)其他存在零的情况,则观察是否有Min(m2,m5)=0,如果有,则应该输出m2 or m5。否则,输出1,也就是选择经过零。

 

下面是ac代码,老是打补丁,写乱了:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 1005

using namespace std;
int g[maxn][maxn];
int min2[maxn][maxn];
int min5[maxn][maxn];
int n;

struct P{
    int first,second;
};

int cal(int m,int k)
{
    if(!m) return 0;
    int ccount=0;
    while(m%k==0){
        m/=k;
        ccount+=1;
    }
    return ccount;
}

int main()
{
    int x,y;
    string s2,s5;
    bool if_0=false;
    scanf("%d",&n);
    for(int i=0;i<n;i+=1){
        for(int j=0;j<n;j+=1){
            scanf("%d",&g[i][j]);
            if(!g[i][j]){
                x=i,y=j;
                if_0=true;
                continue;
            }
        }
    }

    int m2,m5;
    P pre[maxn][maxn];
    pre[0][0].first=-1;
    pre[0][0].second=-1;
    min2[0][0]=0;
    min5[0][0]=0;
    if(!g[0][0]){
        printf("1\n");
        int dx=n-1,dy=n-1;
        while(dx) printf("D"),dx--;
        while(dy) printf("R"),dy--;
        printf("\n");
        return 0;
    }
    else{
        min2[0][0]=cal(g[0][0],2);
        min5[0][0]=cal(g[0][0],5);
        bool flag1=true,flag2=true;
        for(int i=1;i<n;i+=1){
            if(flag1&&!g[0][i]){
                flag1=false;
            }
            if(flag1){
                min2[0][i]=min2[0][i-1]+cal(g[0][i],2);
                min5[0][i]=min5[0][i-1]+cal(g[0][i],5);
                pre[0][i].first=0,pre[0][i].second=i-1;
            }
            else g[0][i]=-1;

            if(flag2&&!g[i][0]){
                flag2=false;
            }
            if(flag2){
                min2[i][0]=min2[i-1][0]+cal(g[i][0],2);
                min5[i][0]=min5[i-1][0]+cal(g[i][0],5);
                pre[i][0].first=i-1,pre[i][0].second=0;
            }
            else g[i][0]=-1;
        }
        for(int i=1;i<n;i+=1){
            for(int j=1;j<n;j+=1){
                min2[i][j]=cal(g[i][j],2);
                min5[i][j]=cal(g[i][j],5);
                if((!g[i-1][j]||g[i-1][j]==-1)&&(!g[i][j-1]||g[i][j-1]==-1)){
                    g[i][j]=-1;
                }
                else if(min2[i-1][j]<min2[i][j-1]){
                    if(g[i-1][j]!=0&&g[i-1][j]){
                        min2[i][j]+=min2[i-1][j];
                        min5[i][j]+=min5[i-1][j];
                        pre[i][j].first=i-1;
                        pre[i][j].second=j;
                    }
                    else{
                        min2[i][j]+=min2[i][j-1];
                        min5[i][j]+=min5[i][j-1];
                        pre[i][j].first=i;
                        pre[i][j].second=j-1;
                    }
                }
                else if(g[i][j-1]&&g[i][j-1]!=-1){
                    min2[i][j]+=min2[i][j-1];
                    min5[i][j]+=min5[i][j-1];
                    pre[i][j].first=i;
                    pre[i][j].second=j-1;
                }
                else{
                    min2[i][j]+=min2[i-1][j];
                    min5[i][j]+=min5[i-1][j];
                    pre[i][j].first=i-1;
                    pre[i][j].second=j;
                }
            }
        }
        m2=min(min2[n-1][n-1],min5[n-1][n-1]);
        int a=n-1,b=n-1;
        while(a||b){
            if(pre[a][b].first!=a) s2=s2+'D';
            else s2=s2+'R';
            int tmpa=a,tmpb=b;
            a=pre[tmpa][tmpb].first,b=pre[tmpa][tmpb].second;
            //cout<<tmpa<<" "<<tmpb<<" s2:"<<s2<<endl;
        }

        min5[0][0]=cal(g[0][0],5);
        min2[0][0]=cal(g[0][0],2);
        for(int i=1;i<n;i+=1){
            if(flag1&&!g[0][i]){
                flag1=false;
            }
            if(flag1){
                min5[0][i]=min5[0][i-1]+cal(g[0][i],5);
                min2[0][i]=min2[0][i-1]+cal(g[0][i],2);
                pre[0][i].first=0,pre[0][i].second=i-1;
            }
            else g[0][i]=-1;

            if(flag2&&!g[i][0]){
                flag2=false;
            }
            if(flag2){
                min2[i][0]=min2[i-1][0]+cal(g[i][0],2);
                min5[i][0]=min5[i-1][0]+cal(g[i][0],5);
                pre[i][0].first=i-1,pre[i][0].second=0;
            }
            else g[i][0]=-1;
        }
        for(int i=1;i<n;i+=1){
            for(int j=1;j<n;j+=1){
                min5[i][j]=cal(g[i][j],5);
                min2[i][j]=cal(g[i][j],2);
                if((!g[i-1][j]||g[i-1][j]==-1)&&(!g[i][j-1]||g[i][j-1]==-1)){
                    g[i][j]=-1;
                }
                else if(min5[i-1][j]<min5[i][j-1]){
                    if(g[i-1][j]!=0&&g[i-1][j]){
                        min2[i][j]+=min2[i-1][j];
                        min5[i][j]+=min5[i-1][j];
                        pre[i][j].first=i-1;
                        pre[i][j].second=j;
                    }
                    else{
                        min2[i][j]+=min2[i][j-1];
                        min5[i][j]+=min5[i][j-1];
                        pre[i][j].first=i;
                        pre[i][j].second=j-1;
                    }
                }
                else if(g[i][j-1]&&g[i][j-1]!=-1){
                    min2[i][j]+=min2[i][j-1];
                    min5[i][j]+=min5[i][j-1];
                    pre[i][j].first=i;
                    pre[i][j].second=j-1;
                }
                else{
                    min2[i][j]+=min2[i-1][j];
                    min5[i][j]+=min5[i-1][j];
                    pre[i][j].first=i-1;
                    pre[i][j].second=j;
                }
            }
        }

        m5=min5[n-1][n-1];
        a=n-1,b=n-1;
        while(a||b){
            if(pre[a][b].first!=a) s5=s5+'D';
            else s5=s5+'R';
            int tmpa=a,tmpb=b;
            a=pre[tmpa][tmpb].first,b=pre[tmpa][tmpb].second;
            //cout<<tmpa<<" "<<tmpb<<" s5:"<<s5<<endl;
        }
    }

    if(g[n-1][n-1]!=-1&&(!if_0||(if_0&&(!m5||!m2)))){
        printf("%d\n",min(m5,m2));
        if(m5<m2){
            int sz=s5.length();
            for(int i=sz-1;i>=0;i-=1) cout<<s5[i];
            printf("\n");
        }
        else {
            int sz=s2.length();
            for(int i=sz-1;i>=0;i-=1) cout<<s2[i];
            printf("\n");
        }
    }
    else{
        printf("1\n");
        int dx=x,dy=y;
        while(dx) printf("D"),dx--;
        while(dy) printf("R"),dy--;
        dx=n-x-1,dy=n-y-1;
        while(dx) printf("D"),dx--;
        while(dy) printf("R"),dy--;
    }

    return 0;
}

/*

4
2 5 2 5
2 2 5 5
2 5 5 5
5 5 5 2

3
0 0 0
0 0 0
0 0 0
*/












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值