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

31 2 34 5 67 8 9

Output

0DDRR

解题思路:题意: 从左上角开始走,走到右下角,只能向右或向下走,将经过的数字相乘,求最后结果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\n";
        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;
}
时间: 2024-10-19 03:13:10

codeforces 2B The least round way的相关文章

Codeforces #2B The least round way(DP)

Description 有一个n*n的正整数矩阵,要你求一条从第一行第一列的格子到第n行第n列的路,使得你走过的格子里面的数乘起来的值末尾的零的个数最小.输出最小个数. Input 第一行包含1个数n. 接下来n行每行n个数字. Output 一个数字表示末尾零最小个数. Sample Input 3 1 2 3 4 5 6 7 8 9 Sample Output 0 由于都是正数,对这个来说只需统计最少的2或5即可.相对简单. #include<cstdio> #include<cst

Codeforces 2B The least round way(dp求最小末尾0)

题目链接:http://codeforces.com/problemset/problem/2/B 题目大意: 给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0最少.解题思路:我们设k为2的因子数,m为5的因子数,那么一个数的末尾0的个数就是min(k,m).我们设dp[i][j][0]为从左上角到点(i,j)的乘积的最少2因子数,dp[i][j][1]为从左上角到点(i,j)的乘积的最少5因子数.那么ans=min(dp[i][j][0],dp[i][j

codeforces 2B The least round way 【DP】

VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情况,要判断你的ans是不是大于1如果大于1那么输出一条经过0的路径即可. 2.当根据2的数量贪心进行dp的时候,如果可以转移的两个来源的2的数量是相同的,需要找到5的数量较小的状态转移过来. 代码较挫. #include<bits/stdc++.h> using namespace std; in

CodeForces B. The least round way(dp)

题目链接:http://codeforces.com/problemset/problem/2/B 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 n

code force 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 bott

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 t

CodeForces - 504A &amp;&amp; CodeForces - 624C &amp;&amp; CodeForces - 2B

Points 1. 关键要看到以度数为1的点作为突破口. 2. 关键是发现两者不同只能是a-c,而剩余的点必须为b 3. 注意0的情况.

codeforces Technocup 2017 - Elimination Round 2/Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) 题解

久违的下午场,打了一场状态不错一下rank12涨了207~~~ A. Interview with Oleg 题意: 给你一个长度不超过100的串,把其中ago开头后面不接或者接gogogo...的部分全部变成*** 思路: 水水,扫 /* *********************************************** Author :devil ************************************************ */ #include <cstdi

CF 2B The least round way DP+Math

题意: 找出一条路, 使每个节点相乘,得到的数末尾 0 最少 每次移动只能向右或者向下, 找到后打印路径 ///按照题目要求,就是找出一条从左上角到右下角中每个数含2 or 5 最少的路 ///可以用Dp的思想, 然后把每个节点该走的方向记下来 ///再从终点回溯,把路径存入栈,再输出 ///数据会有0的情况, 这时候我们应该记录离终点最近的0 #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef