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.

解题报告:

google翻译是什么东西啊?multiply居然是相加.......

然后就比较简单了:多少个\(0\)可以看成有多少个\(10\),多少个\(10\)可以看成有多少个\(2\)和\(5\)因子,这样就可以分开处理了,实际上就是分别统计最少经过的\(2\)因子的个数和\(5\)因子的个数,然后取Min就行了,DP方程如同方格取数: \(f[i][j]=Min(f[i-1][j],f[i][j-1])+a[i][j]\) a[i][j]为\((i,j)\)这个位置目前处理的因子的个数

但是注意存在0的情况答案最多为1,如果不存在答案为0的路径,那么就直接走经过0的路径即可

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=1005;
int f[2][N][N],a[N][N],n,p[2][N][N],pre[2][N][N];
char s[N<<1];
void work()
{
int cnt=0,x;bool flag=false;
int pi,pj;
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
x=a[i][j];
if(!x){
flag=true,pi=i,pj=j;
p[0][i][j]++;p[1][i][j]++;
continue;
}
cnt=0;
while(x%2==0)x>>=1,cnt++;
p[0][i][j]=cnt;cnt=0;
while(x%5==0)x/=5,cnt++;
p[1][i][j]=cnt;
}
for(int k=0;k<=1;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(i-1 && j-1)
{
if(f[k][i-1][j]<f[k][i][j-1]){
f[k][i][j]=f[k][i-1][j];
pre[k][i][j]=1;
}
else{
f[k][i][j]=f[k][i][j-1];
pre[k][i][j]=2;
}
}
else{
if(i-1)f[k][i][j]=f[k][i-1][j],pre[k][i][j]=1;
else if(j-1)f[k][i][j]=f[k][i][j-1],pre[k][i][j]=2;
}
f[k][i][j]+=p[k][i][j];
}
int ans=Min(f[0][n][n],f[1][n][n]);
if(flag && ans>1){
puts("1");
for(int i=1;i<pi;i++)putchar('D');
for(int i=1;i<pj;i++)putchar('R');
for(int i=pi;i<n;i++)putchar('D');
for(int i=pj;i<n;i++)putchar('R');
return ;
}
printf("%d\n",ans);
pi=n;pj=n;int k=(ans==f[0][n][n]?0:1);
for(int i=1;i<=n+n-2;i++){
if(pre[k][pi][pj]==1)s[i]='D',pi--;
else if(pre[k][pi][pj]==2)s[i]='R',pj--;
}
for(int i=n+n-2;i>=1;i--)putchar(s[i]);
} int main()
{
work();
return 0;
}

Codeforces 2B. The least round way的更多相关文章

  1. Codeforces #2B The least round way(DP)

    Description 有一个n*n的正整数矩阵,要你求一条从第一行第一列的格子到第n行第n列的路,使得你走过的格子里面的数乘起来的值末尾的零的个数最小.输出最小个数. Input 第一行包括1个数n ...

  2. codeforces 2B The least round way(DP+数学)

    The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...

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

    题目链接:http://codeforces.com/problemset/problem/2/B 题目大意: 给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0 ...

  4. codeforces 2B The least round way 【DP】

    VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...

  5. 最小较小codeforces 2B The least round way

    查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧! 求从左上角到右下角所经过的数字之积末端所含0最小的个数 终究的积可以当作A*2^x*5^y, ...

  6. CodeForces B. The least round way(dp)

    题目链接:http://codeforces.com/problemset/problem/2/B B. The least round way time limit per test 5 secon ...

  7. Codeforces VK Cup 2012 Round 3 A. Variable, or There and Back Again(dfs)

    题目链接:http://codeforces.com/problemset/problem/164/A 思路:用vector分别保留原图和发图,然后分别从val值为1的点正向遍历,va值为2的点反向遍 ...

  8. codeforces B. George and Round 解题报告

    题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...

  9. Codeforces Technocup 2017 - Elimination Round 2 D. Sea Battle(贪心)

    题目链接 http://codeforces.com/contest/729/problem/D 题意:给你一个1*n的区域有a艘船,每艘船宽b,已经开了k枪都没打到,问你最少再开几枪至少能打到一艘船 ...

随机推荐

  1. github上传时出现error: src refspec master does not match any解决办法

    github上传时出现error: src refspec master does not match any解决办法 这个问题,我之前也遇到过,这次又遇到了只是时间间隔比较长了,为了防止以后再遇到类 ...

  2. 201621123057 《Java程序设计》第2周学习总结

    一.本周学习总结 基本数据类型 char实质属于整型.boolean类型取值只有true和false两种. 引用数据类型 包装类:自动装箱 与 自动拆箱 数组:一维数组遍历数组用foreach循环:多 ...

  3. CPP 栈 示例

    #include<iostream> #include<stdlib.h> using namespace std; typedef struct node { int dat ...

  4. SENet

     \(\bf F_{tr}\) 为标准卷积操作 \(\bf F_{sq}\) 为 Global Average Pooling \(\bf F_{ex}\) 为两层全连接网络(可以看做两个1×1卷积 ...

  5. LeetCode & Q28-Implement strStr-Easy

    String Two Pointers Description: Implement strStr(). Returns the index of the first occurrence of ne ...

  6. VMware vCenter Server 6.5.0 U1

    VMware vCenter Server 6.5.0 U1gName: VMware-VCSA-all-6.5.0-8024368.iso Release Date: 2018-03-20 Buil ...

  7. SpringBoot应用的启动方式

    一:IDE 运行Application这个类的main方法 二:在SpringBoot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...

  8. bootstrap 之下拉多选

    效果如图: 一.HTML代码 <label class="col-sm-1 control-label text-right" for="ds_host" ...

  9. Linux将端口设置进防火墙的白名单

    1.先检查linux服务器的端口是否被防火墙拦住 `telnet 172.168.1.101 8080后面跟端口号,如果连接上证明是防火墙白名单.如果没有配置 vi /etc/sysconfig/ip ...

  10. 判断一个字符串是不是一个合法的IP地址

    最近在笔试的时候遇到碰一道算法题, 要求判断一个字符串是不是合法的ip地址. 将我的思路发出来分享一下,不一定正确,也不一定是最优的方法.希望能分享一些交流 要求用java或者c来实现,我的java代 ...