B. The least round way
time limit per test

2 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.

Examples
input

Copy

3
1 2 3
4 5 6
7 8 9

output

Copy

0
DDRR

题意:给你一个n*n的矩阵 只能向下或者向右 问你从左上角走到左下角 你走过路径上数字的乘积尾部0个数最少的走法

思路:我们知道只有2和5的组合可以让尾部为0 所以我们可以预处理每个数字有多少个2和5 然后很容易得出方程dp[i][j][0/1]=min(dp[i-1][j][0/1],dp[i][j-1][0/1])+a[i][j][0/1]

值得注意的是 如果其中有出现0这个数字 那么就表示 只要经过这个点 那么尾部0的个数必然是1 那么答案就只能在0或者1中产生

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<time.h>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int dp[][][]; //0表示2的个数 1表示5的个数
int a[][]; //存矩阵
bool f=;
struct node{
int x;int y;
};
node path2[][]; //记录2的个数最少时的路径
node path1[][]; //记录5的个数最少时的路径
node ans[];
int cnt=;
void output1(node x){ //记录答案
if(x.x==&&x.y==) return ;
node t; t=path1[x.x][x.y];
output1(t);
ans[++cnt]=x;
}
void output2(node x){ //记录答案
if(x.x==&&x.y==) return ;
node t; t=path2[x.x][x.y];
output2(t);
ans[++cnt]=x;
}
int main(){
ios::sync_with_stdio(false);
int n;
cin>>n;
int posx,posy;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
cin>>a[i][j];
int t=a[i][j];
if(t==){ //一但出现零我们需要标记一下
f=;
dp[i][j][]++;
dp[i][j][]++;
posx=i;
posy=j;
continue;
}
while(){ //统计2 和 5 的个数
if(t%==){
t/=;
dp[i][j][]++;
}else if(t%==){
t/=;
dp[i][j][]++;
}else{
break;
}
}
}
for(int i=;i<=n;i++){ // 预处理边界
dp[][i][]+=dp[][i-][];
dp[][i][]+=dp[][i-][];
node t; t.x=; t.y=i-;
path1[][i]=path2[][i]=t;
dp[i][][]+=dp[i-][][];
dp[i][][]+=dp[i-][][];
t.x=i-; t.y=;
path1[i][]=path2[i][]=t;
}
path1[][].x=path2[][].x=;
path1[][].y=path2[][].y=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){ //递推
if(dp[i-][j][]<dp[i][j-][]){
dp[i][j][]=dp[i-][j][]+dp[i][j][];
node temp; temp.x=i-; temp.y=j;
path1[i][j]=temp;
}else{
dp[i][j][]=dp[i][j-][]+dp[i][j][];
node temp; temp.x=i; temp.y=j-;
path1[i][j]=temp;
}
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){ //递推
if(dp[i-][j][]<dp[i][j-][]){
dp[i][j][]=dp[i-][j][]+dp[i][j][];
node temp; temp.x=i-; temp.y=j;
path2[i][j]=temp;
}else{
dp[i][j][]=dp[i][j-][]+dp[i][j][];
node temp; temp.x=i; temp.y=j-;
path2[i][j]=temp;
}
}
int minn=min(dp[n][n][],dp[n][n][]);
if(minn>&&f){ //表示尾部零最小的个数就是1 那么我们就可以直接输出
cout<<""<<endl;
for(int i=;i<posx;i++)
cout<<"D";
for(int j=;j<posy;j++)
cout<<"R";
for(int i=posx;i<n;i++)
cout<<"D";
for(int j=posy;j<n;j++)
cout<<"R";
cout<<endl;
return ;
}
cout<<minn<<endl;
node e; e.x=n; e.y=n;
if(dp[n][n][]<dp[n][n][])
output1(e);
else{
output2(e);
}
for(int i=;i<=cnt;i++){
if(ans[i].x==ans[i-].x+)
cout<<"D";
else
cout<<"R";
}
cout<<endl;
return ;
}

CF2B The least round way(dp+记录路径)的更多相关文章

  1. PAT L3-001 凑零钱(01背包dp记录路径)

    韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...

  2. Codeforces Round #436 (Div. 2) E. Fire(dp 记录路径)

    E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  3. hdu 1074(状态压缩dp+记录路径)

    题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...

  4. [CF2B] The least round way - dp

    给定由非负整数组成的n×n 的正方形矩阵,你需要寻找一条路径: 以左上角为起点 每次只能向右或向下走 以右下角为终点 并且,如果我们把沿路遇到的数进行相乘,积应当是最小"round" ...

  5. uva 10453 - Make Palindrome(dp, 记录路径)

    题目 题意: 给一个字符串 ,判断最少插入多少个字符 使字符串成为回文串, 并输出回文串. 思路:先用dp判断需要个数, 再递归输出路径. #include <iostream> #inc ...

  6. codeforces 1272F dp+记录路径

    题意 给出两个括号序列 \(S\) 和 \(T\),让你构造一个最短的合法括号序列使 \(S\) 和 \(T\) 是它的子序列. 分析 设 \(dp[i][j][k]\) 为这个最短的合法括号序列的前 ...

  7. POJ 题目1141 Brackets Sequence(区间DP记录路径)

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27793   Accepted: 788 ...

  8. POJ 2111 DP+记录路径

    题意: 思路: 类似滑雪 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm& ...

  9. hdu1074 状压DP、栈实现记录路径

    题意:给了几门学科作业.它们的截止提交期限(天数).它们的需要完成的时间(天数),每项作业在截止日期后每拖延一天扣一学分,算最少扣的学分和其完成顺序. 一开始做的时候,只是听说过状态压缩这个神奇的东西 ...

随机推荐

  1. 红米手机4A怎么样刷入开发版获得ROOT权限

    小米的手机或平板不同手机型号一般情况官方都提供两个不同系统,可分为稳定版和开发版,稳定版没有提供root权限管理,开发版中就支持了root权限,在很多工作的时候我们需要使用的一些功能强大的app,都需 ...

  2. 探究高级的Kotlin Coroutines知识

    要说程序如何从简单走向复杂, 线程的引入必然功不可没, 当我们期望利用线程来提升程序效能的过程中, 处理线程的方式也发生了从原始时代向科技时代发生了一步一步的进化, 正如我们的Elisha大神所著文章 ...

  3. [aspnetcore.apidoc]一款很不错的api文档生成工具

    AspNetCore.ApiDoc 简单徐速一下为什么选用了aspnetcore.apidoc 而没有选用swagger 最初我们也有在试用swagger,但总是有些感觉,感觉有点不满意,就但从api ...

  4. .net向文件写入字符串流内存溢出的问题

    字符串过大导致抛出异常: exceptopm of type 'system.outOfmemoryexception' was thrown 解决方法:逐块写入可以避免这个问题

  5. 实验吧逆向catalyst-system Writeup

    下载之后查看知道为ELF文件,linux中执行之后发现很慢: 拖入ida中查看发现有循环调用 sleep 函数: 这是已经改过了,edit -> patch program -> chan ...

  6. Vue学习之路1-集成环境安装

    1.前言 Vue 是一款友好的.多用途且高性能的javascript框架,与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用,它能够帮你创建可维护性和可测试性更强的代码库,Vue是渐进式的j ...

  7. Flafka: Apache Flume Meets Apache Kafka for Event Processing

    The new integration between Flume and Kafka offers sub-second-latency event processing without the n ...

  8. nginx 499状态码

    Web服务器在用着nginx,在日志中偶尔会看到有499这个错误. rfc2616中,400-500间的错误码仅定义到了417,所以499应该是nginx自己定义的.后来想到读读nginx代码,疑问立 ...

  9. 5000量子位支持量子编程,D-Wave推出下一代量子计算平台计划

    5000量子位支持量子编程,D-Wave推出下一代量子计算平台计划 近日,全球量子商用化重要参与者 D-Wave 公司又有大动作:推出其5000量子比特量子计算的发展蓝图.D-Wave 下一代量子计算 ...

  10. 前端学习-基础部分-css(一)

    开始今日份整理 1.CSS的导入方式 CSS的导入方式主要是有内联模式,行内模式,外部样式表 1.1 内联模式 内联模式:直接在<head>中直接写css,例如 p{ color:rgb( ...