codeforces 2B The least round way DP因子路径

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

要从0.0到n,n之间的数乘起来,最后的结果0的个数最少,对每个数分解2,5因子(只有2*5会产生10)

分别对2,5找一条从从0.0到n,n经过2,5最少的路径,谁最少最后就走谁,反方向回去即可

注意乘数包含0的情况

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
int mp[N][N][2];
#define TWO 0
#define FIVE 1
int getfactor(int num, int base)
{
    if (num == 0)
        return 1;
    int ret = 0;
    while (num % base==0)
    {
        num /= base;
        ret++;
    }
    return ret;
}
int dp[N][N][2];
char direct[N][N][2];//记录转移方向
char path[2010];//记录路径
int path_len;
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n;
    while (scanf("%d", &n) + 1)
    {
        int num;
        bool haszero = false;
        int x0, y0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                scanf("%d", &num);
                if (num == 0)
                {
                    haszero = true;
                    x0 = i;
                    y0 = j;
                }
                mp[i][j][TWO] = getfactor(num, 2);
                mp[i][j][FIVE] = getfactor(num, 5);
            }
        }
        memset(dp, 0, sizeof(dp));
        dp[0][0][TWO] = mp[0][0][TWO];
        dp[0][0][FIVE] = mp[0][0][FIVE];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    if (i == 0 && j == 0)
                        continue;
                    if (i == 0)
                    {
                        dp[i][j][k] = dp[i][j - 1][k] + mp[i][j][k];
                        direct[i][j][k] = 'R';
                    }
                    else if (j == 0)
                    {
                        dp[i][j][k] = dp[i - 1][j][k] + mp[i][j][k];
                        direct[i][j][k] = 'D';
                    }
                    else
                    {
                        dp[i][j][k] = min(dp[i - 1][j][k], dp[i][j - 1][k]) + mp[i][j][k];
                        direct[i][j][k] = dp[i - 1][j][k] < dp[i][j - 1][k] ? 'D' : 'R';
                    }
                }
            }
        }
        if (haszero && min(dp[n - 1][n - 1][TWO], dp[n - 1][n - 1][FIVE]) > 1)
        {
            printf("1\n");
            for (int i = 0; i < y0; i++)
                printf("R");
            for (int j = 0; j < n-1; j++)
                printf("D");
            for (int i = y0; i < n-1; i++)
                printf("R");
        }
        else
        {
            printf("%d\n", min(dp[n - 1][n - 1][TWO], dp[n - 1][n - 1][FIVE]));
            int k = TWO;
            if (dp[n - 1][n - 1][TWO] > dp[n - 1][n - 1][FIVE])
                k = FIVE;
            path_len=0;
            for (int i = n - 1, j = n - 1; i != 0 || j != 0;)
            {
                path[path_len++] = direct[i][j][k];
                if (direct[i][j][k] == 'D')
                    i--;
                else
                    j--;
            }
            for (int i = 2 * (n - 1) - 1; i >= 0; i--)
                printf("%c", path[i]);
        }
        printf("\n");
    }

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值