zoukankan      html  css  js  c++  java
  • codeforces 2B The least round way

    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.

    Example

    Input
    3
    1 2 3
    4 5 6
    7 8 9
    Output
    0
    DDRR

    解题思路:
    题意: 从左上角开始走,走到右下角,只能向右或向下走,将经过的数字相乘,求最后结果0最少的路径
    思路:若要使相乘后尾部有0,有两种情况:1.其中包含2和5,记录2和5的个数分别为a,b要求尾部的0最少那么只要求经过的2或5最少的路径即可,也就是取a和b中较小值,
    2.当经过0时,不论其他数为多少,最后都为1个0,如果1情况大于1,则优先选择2.


    实现代码:
    #include<bits/stdc++.h>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    int f[1001][1001][2],m,x,k;
    char g[1001][1001][2];
    
    void fuck(int x,int y)
    {
        if(x==1&&y==1)
            return ;
        if(g[x][y][k]){
            fuck(x-1,y),putchar('D');
        }
        else{
            fuck(x,y-1),putchar('R');
        }
    }
    int main()
    {
        int i;
        cin>>m;
        memset(f,0,sizeof(f));
        for(i=2;i<=m;i++){
            f[i][0][0]=f[0][i][0]=f[i][0][1]=f[0][i][1] = inf;
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=m;j++){
                scanf("%d",&k);
                if(!k)
                    x = i;
                else{
                    while(k%2==0) ++f[i][j][0],k/=2;
                    while(k%5==0) ++f[i][j][1],k/=5;
                }
                for(k=0;k<2;k++){
                    if(g[i][j][k]=f[i-1][j][k]<f[i][j-1][k])
                        f[i][j][k] += f[i-1][j][k];
                    else
                        f[i][j][k] += f[i][j-1][k];
                }
            }
        }
        k = f[m][m][1] < f[m][m][0];
        if(x&&f[m][m][k]>1){
            cout<<"1
    ";
            for(i=2;i<=x;i++)
                putchar('D');
            for(i=2;i<=m;i++)
                putchar('R');
            for(i=x+1;i<=m;i++)
                putchar('D');
        }
        else{
            cout<<f[m][m][k]<<endl;
            fuck(m,m);
        }
        return 0;
    }
  • 相关阅读:
    scrapy框架(一)
    selenium爬取京东商品信息
    自动化测试工具selenium的使用
    xpath选择器的使用
    爬取网页数据实例
    爬虫实操
    爬虫之`beautifulsoup4`模块
    DNS学习笔记
    MongoDB集群部署
    MongoDB单机部署
  • 原文地址:https://www.cnblogs.com/kls123/p/6837368.html
Copyright © 2011-2022 走看看