CF 2B The least round way

本文介绍了一种算法,用于在一个n×n的矩阵中找到一条从左上角到右下角的路径,使得该路径上所有数值相乘后的结果中尾随零的数量最少。文章详细解释了算法的实现思路,并提供了完整的代码示例。
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 的矩阵,他的每一步只能向下或向右走,问从左上角到右下角的路径中乘积中零最少的路径,输出路径和零的个数。
思路:由于每个位置只能有其上的位置或其右的位置得到,两者取min即可;
dp[i][j][k]表示从(0 , 0)到(i , j)位置的路径中乘积零最小的路径所包含的2数(k = 0)5数(k = 1)
特例是矩阵中有零且有2和5,则必走有零的那条路径。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1

#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 1000 + 50;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-4
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
int dp[MAXN][MAXN][2] , a[MAXN][MAXN][2] , n ;
int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    //freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#endif // Online_Judge
    int x ,  r;
    bool ok;
    while(~scanf("%d" , &n))
    {
        ok = false;
        FOR(i ,0 , n)FOR(j ,0 , n)
        {
            scanf("%d" , &x);
            if(x == 0)
            {
                ok = true;
                r = i ; x = 10;
            }
            while(x % 2 == 0)
            {
                x /= 2;
                dp[i][j][0] ++;
            }
            while(x % 5 == 0)
            {
                x /= 5;
                dp[i][j][1]++;
            }
            FOR(k , 0 , 2)a[i][j][k] = dp[i][j][k];
        }
        FOR(i , 1 , n)FOR(j , 0 , 2)
        {
            dp[0][i][j] += dp[0][i - 1][j];
            dp[i][0][j] += dp[i - 1][0][j];
        }
        FOR(i , 1 , n)FOR(j , 1 , n)FOR(k , 0 ,2)
        {
            dp[i][j][k] += min(dp[i - 1][j][k] , dp[i][j - 1][k]);
        }
        int k = dp[n - 1][n - 1][1] < dp[n - 1][n - 1][0] ;
        if(ok)
        {
            if(dp[n - 1][n - 1][k] > 1)
            {
                printf("1\n");
                FORR(i , 1 , r)printf("D");
                FOR(i , 0 , n - 1)printf("R");
                FOR(i , r + 1 , n)printf("D");
                continue;
            }
        }
        printf("%d\n" , dp[n - 1][n - 1][k]);
        int i = n - 1 , j = n - 1;
        char s[MAXN * 3];
        int len = 0;
        while(i != 0||j != 0)
        {
            if(i > 0&&dp[i - 1][j][k] + a[i][j][k] == dp[i][j][k])
            {
                s[len++] = 'D';
                i--;
            }
            else
            {
                s[len++] = 'R';
                j--;
            }
        }
        REPP(i ,len - 1 , 0)printf("%c" , s[i]);
        printf("\n");
    }

    return 0;
}



### Codeforces Round 980 (Div. 2) 比赛信息 Codeforces Round 980 (Div. 2) 是一场面向国际编程爱好者的在线竞赛,吸引了大量来自不同国家的参赛者参与。这场比赛通常包含多个难度级别的题目,旨在测试选手们的算法设计能力和编码技巧。 #### 比赛时间与平台 比赛于特定日期在Codeforces平台上举行,持续时间为两小时。在此期间内,参与者需解决尽可能多的问题来获得高分并争取更好的排名位置[^2]。 ### 题目解析:C - Concatenation of Arrays 对于给定的任务——数组连接问题,目标是从两个长度相等但元素可能不同的整数序列出发,在满足一定条件下构建一个新的组合形式。具体来说: - 输入描述了一个由正整数组成的列表`q[]`以及两个参数`n`和`m`。 - 要求通过某种方式调整这些数值的位置关系使得最终形成的结构能够达到预期效果。 为了实现上述目的,可以采用线性扫描的方法来进行处理而不是复杂的二分查找策略。以下是简化后的解决方案概述及其对应的伪代码表示: 当遍历整个数据集时计算累积值,并判断当前状态下能否形成合法配置;一旦发现符合条件的情况立即更新最优解直至完成全部迭代过程为止。 ```cpp #include <iostream> #include <algorithm> using namespace std; const int N = 1e5 + 7; int q[N]; bool check(int x, int n, int m) { int res = 0; for (int i = 1; i <= x; ++i) { if (i != x) res += q[i]; else res += (n - i + 1) * q[i]; } return res >= m; } void solve() { int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> q[i]; sort(q + 1, q + 1 + n); int l = 1, r = n; while (l < r) { int mid = l + r >> 1; if (check(mid, n, m)) r = mid; else l = mid + 1; } cout << l - 1 + m << '\n'; } ``` 此段程序实现了对输入数据的有效分析,并利用简单的逻辑运算得出正确答案。值得注意的是,尽管最初考虑过使用二分法优化性能,但实际上仅需一次完整的顺序访问就能解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值