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. Android实验报告

    实验名称:Android程序设计 实验时间:2017.5.24 实验人员:20162309邢天岳(结对同学20162313苑洪铭) 实验目的:使用android stuidio开发工具进行基本安卓软件 ...

  2. android context获取目录详解

    获取 sqlite系统数据库路径 方式1: ApkInfo apkInfo = new ResourceUtil(context).getApkInfo(); APP_PATH = new Strin ...

  3. LeetCode & Q167-Two Sum II - Input array is sorted-Easy

    Array Two Pointers Binary Search Description: Given an array of integers that is already sorted in a ...

  4. EasyUI DataGrid - 嵌套的DataGrid

    实现效果: 一.在页面头部引用视图脚本JS文件 <script src="@Url.Content("~/Resources/EasyUI/plugins/datagrid- ...

  5. Django ORM那些相关操作

    一般操作 https://docs.djangoproject.com/en/1.11/ref/models/querysets/         官网文档 常用的操作 <1> all() ...

  6. yagmail让发邮件更简单

    这是我迄今为止碰到的最良心的库,真tm简单啊 import yagmail # 连接邮箱服务器 yag = yagmail.SMTP(user="wuyongqiang2012@163.co ...

  7. 收藏:视频网站(JavaEE+FFmpeg)/Nginx+ffmpeg实现流媒体直播点播系统

    FFmpeg安装(windows环境)http://www.cnblogs.com/xiezhidong/p/6924775.html 最简单的视频网站(JavaEE+FFmpeg)http://bl ...

  8. POJ-1068 Parencodings---模拟括号的配对

    题目链接: https://vjudge.net/problem/POJ-1068 题目大意: 给出一种括号序列的表示形式名叫P序列,规则是统计出每个右括号之前的左括号个数作为序列每项的值.然后要求你 ...

  9. PhantomJS命令行选项

    支持命令行选项有: --help或-h列出所有可能的命令行选项.立即停止,不会运行一个脚本作为参数传递. --version或-v打印的版本PhantomJS.立即停止,不会运行一个脚本作为参数传递. ...

  10. 谈mysql优化

    公司订单系统每日订单量庞大,有很多表数据超千万.公司SQL优化这块做的很不好,可以说是没有做,所以导致查询很慢. 节选某个功能中的一句SQL EXPLAIN查看执行计划,EXPLAIN + SQL 查 ...