Codeforces beta round #2 B. The least round Way

Source: Internet
Author: User

This 2B question is okay ~~


Question:

A matrix is provided, from top left to bottom right. It can only go to the right or bottom. Each grid in the path has a number. These numbers are multiplied to produce a number.

Find a path with zero at the end of the number.


Solution:


Find a path. The product gets the least number of prime factor 2, and then finds the minimum number of prime factor 5 to compare the two outputs.

The number of errors is zero. In this case, run zero as 10. If the prime factor is at least 0, the output path goes through the zero path. If not, the output goes through the zero path.



The following code is used:

#include <set>#include <map>#include <queue>#include <math.h>#include <vector>#include <string>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <cctype>#include <algorithm>#define eps 1e-10#define pi acos(-1.0)#define inf 107374182#define inf64 1152921504606846976#define lc l,m,tr<<1#define rc m + 1,r,tr<<1|1#define zero(a) fabs(a)<eps#define iabs(x)  ((x) > 0 ? (x) : -(x))#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))#define clearall(A, X) memset(A, X, sizeof(A))#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))#define memcopyall(A, X) memcpy(A , X ,sizeof(X))#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )using namespace std;int dp[1005][1005][2];int cnt[1005][1005][2];int pre[1005][1005][2];void output(int x,int y,int num){    if(x==0&&y==0)return ;    if(pre[x][y][num]==0)    {        output(x,y-1,num);        printf("R");    }    else    {        output(x-1,y,num);        printf("D");    }}int main(){    int input,n,x=-1,y=-1;    scanf("%d",&n);    for(int i=0; i<n; i++)    {        for(int j=0; j<n; j++)        {            scanf("%d",&input);            if(input==0)            {                cnt[i][j][0]=1;                cnt[i][j][1]=1;                x=i;                y=j;                continue;            }            cnt[i][j][0]=0;            while(input%2==0)            {                cnt[i][j][0]++;                input/=2;            }            cnt[i][j][1]=0;            while(input%5==0)            {                cnt[i][j][1]++;                input/=5;            }        }    }    clearall(pre,-1);    for(int i=0; i<n; i++)    {        for(int j=0; j<n; j++)        {            if(i==0)            {                if(j==0)                {                    dp[0][0][0]=cnt[0][0][0];                    dp[0][0][1]=cnt[0][0][1];                }                else                {                    dp[0][j][0]=cnt[0][j][0]+dp[0][j-1][0];                    dp[0][j][1]=cnt[0][j][1]+dp[0][j-1][1];                    pre[0][j][0]=0;                    pre[0][j][1]=0;                }            }            else if(j==0)            {                dp[i][0][0]=dp[i-1][0][0]+cnt[i][0][0];                dp[i][0][1]=dp[i-1][0][1]+cnt[i][0][1];                pre[i][0][0]=1;                pre[i][0][1]=1;            }            else            {                if(dp[i][j-1][0]>dp[i-1][j][0])                {                    dp[i][j][0]=dp[i-1][j][0]+cnt[i][j][0];                    pre[i][j][0]=1;                }                else                {                    dp[i][j][0]=dp[i][j-1][0]+cnt[i][j][0];                    pre[i][j][0]=0;                }                if(dp[i][j-1][1]>dp[i-1][j][1])                {                    dp[i][j][1]=dp[i-1][j][1]+cnt[i][j][1];                    pre[i][j][1]=1;                }                else                {                    dp[i][j][1]=dp[i][j-1][1]+cnt[i][j][1];                    pre[i][j][1]=0;                }            }        }    }    if(x!=-1)    {        if(min(dp[n-1][n-1][0],dp[n-1][n-1][1])==0)        {            printf("0\n");            if(dp[n-1][n-1][0]==0)output(n-1,n-1,0);            else output(n-1,n-1,1);        }        else        {            printf("1\n");            for(int i=0;i<n-1;i++)            {                if(i==x)                {                    for(int j=0;j<n-1;j++)                    {                        printf("R");                    }                }                printf("D");            }        }    }    else    {        printf("%d\n",min(dp[n-1][n-1][0],dp[n-1][n-1][1]));        if(dp[n-1][n-1][0]<dp[n-1][n-1][1])        {            output(n-1,n-1,0);        }        else output(n-1,n-1,1);    }    return 0;}


Codeforces beta round #2 B. The least round Way

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.