Codeforces Beta Round #2B. The least round way

本文介绍了一种算法,用于解决在一个非负整数矩阵中找到从左上角到右下角的一条路径,使该路径上的数值乘积产生的零尾数最少的问题。通过分析路径上数字的因子来确定最优路径。
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

题意:给你一个n*n的矩阵,让你找一条路径使得路径上的数乘积零的个数最少,求出最少零的个数以及输出路径。

思路:零只能有2和5相乘得到,判断每个数的因子中2和5的个数,最后零的个数为2,5个数最小的一个;注意考虑有数为零的情况(只需将它记录,并把它看成10处理)

代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 99999999
#define N 1005
#define LL long long

int xn;
int dp2[N][N],ma[N][N],dp5[N][N];
char s2[N][N],s5[N][N],ss[2*N];
int slove2(int num)
{
    int ans=0;
    if(num==0)
    return 1;
    while(num%2==0)
    {
        ans++;
        num=num/2;
    }
    return ans;
}
int slove5(int num)
{
    int ans=0;
    if(num==0)
    return 1;
    while(num%5==0)
    {
        ans++;
        num=num/5;
    }
    return ans;
}
int main()
{
    int n;
    xn=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for( int j=1;j<=n;j++)
        {
            scanf("%d",&ma[i][j]);
            if(ma[i][j]==0)
            xn=i;
        }
    }
    memset(s2,0,sizeof(s2));
    memset(s5,0,sizeof(s5));
    memset(ss,0,sizeof(ss));
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dp5[i][j]=inf;
            dp2[i][j]=inf;
        }
    }
    dp2[1][1]=slove2(ma[1][1]);
    dp5[1][1]=slove5(ma[1][1]);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i+1<=n)
            {
                int xx=dp2[i][j]+slove2(ma[i+1][j]);
                if(dp2[i+1][j]>xx)
                {
                    dp2[i+1][j]=xx;
                    s2[i+1][j]='D';
                }
                xx=dp5[i][j]+slove5(ma[i+1][j]);
                if(dp5[i+1][j]>xx)
                {
                    dp5[i+1][j]=xx;
                    s5[i+1][j]='D';
                }
            }
            if(j+1<=n)
            {
                int xx=slove2(ma[i][j+1])+dp2[i][j];
                if(dp2[i][j+1]>xx)
                {
                    dp2[i][j+1]=xx;
                    s2[i][j+1]='R';
                }
                xx=slove5(ma[i][j+1])+dp5[i][j];
                if(dp5[i][j+1]>xx)
                {
                    dp5[i][j+1]=xx;
                    s5[i][j+1]='R';
                }
            }
        }
    }
    int ans=min(dp2[n][n],dp5[n][n]);
    if(xn!=0&&ans>=1)
    {
        int i;
        printf("1\n");
        for(i=2;i<=xn;i++)
            printf("D");
        for(i=2;i<=n;i++)
            printf("R");
        for(i=xn+1;i<=n;i++)
            printf("D");
        printf("\n");
    }
    else
    {
        printf("%d\n",ans);
        int k;
        if(ans==dp2[n][n])
        {
            int i=n,j=n;
            k=0;
            while(i!=1||j!=1)
            {
                ss[k]=s2[i][j];
                k++;
                if(s2[i][j]=='D')
                i--;
                else
                j--;
            }
        }
        else
        {
            int i=n,j=n;
            k=0;
            while(i!=1||j!=1)
            {
                ss[k]=s5[i][j];
                k++;
                if(s5[i][j]=='D')
                i--;
                else
                j--;
            }
        }
        for(int i=k-1;i>=0;i--)
        {
            printf("%c",ss[i]);
        }
        printf("\n");
    }
    return 0;
}


### 关于 Codeforces Round 839 Div 3 的题目与解答 #### 题目概述 Codeforces Round 839 Div 3 是一场面向不同编程水平参赛者的竞赛活动。这类比赛通常包含多个难度层次分明的问题,旨在测试选手的基础算法知识以及解决问题的能力。 对于特定的比赛问题及其解决方案,虽然没有直接提及 Codeforces Round 839 Div 3 的具体细节[^1],但是可以根据以往类似的赛事结构来推测该轮次可能涉及的内容类型: - **输入处理**:给定一组参数作为输入条件,这些参数定义了待解决的任务范围。 - **逻辑实现**:基于输入构建满足一定约束条件的结果集。 - **输出格式化**:按照指定的方式呈现最终答案。 考虑到提供的参考资料中提到的其他几场赛事的信息[^2][^3],可以推断出 Codeforces 圆桌会议的一般模式是围绕着组合数学、图论、动态规划等领域展开挑战性的编程任务。 #### 示例解析 以一个假设的例子说明如何应对此类竞赛中的一个问题。假设有如下描述的一个简单排列生成问题: > 对于每一个测试案例,输出一个符合条件的排列——即一系列数字组成的集合。如果有多种可行方案,则任选其一给出即可。 针对上述要求的一种潜在解法可能是通过随机打乱顺序的方式来获得不同的合法排列形式之一。下面是一个 Python 实现示例: ```python import random def generate_permutation(n, m, k): # 创建初始序列 sequence = list(range(1, n + 1)) # 执行洗牌操作得到新的排列 random.shuffle(sequence) return " ".join(map(str, sequence[:k])) # 测试函数调用 print(generate_permutation(5, 2, 5)) # 输出类似于 "4 1 5 2 3" ``` 此代码片段展示了怎样创建并返回一个长度为 `k` 的随机整数列表,其中元素取自 `[1..n]` 这个区间内,并且保证所有成员都是唯一的。需要注意的是,在实际比赛中应当仔细阅读官方文档所提供的精确规格说明,因为这里仅提供了一个简化版的方法用于解释概念。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值