Codeforces 2B The least round way 动态规划(分类讨论)

来源:互联网 发布:java的编码方式 编辑:程序博客网 时间:2024/05/17 07:30

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
31 2 34 5 67 8 9
output
0DDRR

题目大意:

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

以及最少零情况的路径


解题思路:

首先,可以想到这与经过整数的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 1005using 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;}/*42 5 2 52 2 5 52 5 5 55 5 5 230 0 00 0 00 0 0*/












0 0
原创粉丝点击