题目链接:http://codeforces.com/problemset/problem/2/B

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

题意:

从左上到右下!

选出一条能让走过的路径的数字的积中含有最少的零!

PS:

积要含有零仅仅能是2 或者5形成。寻找一条含有2或者5 最少的路径就可以!

注意推断矩阵中是否有零的情况!

代码例如以下:

//http://blog.csdn.net/azheng51714/article/details/8240390
#include <cstdio>
#include <cstring>
const int maxn = 1017;
int m[2][maxn][maxn];
int dp[2][maxn][maxn];
//dp[0][i][j]到i、j时有多少个2;
//dp[1][i][j]到i、j时有多少个5。
int vis[2][maxn][maxn];
int n; int solve(int mark)
{
vis[mark][1][1] = 0;
dp[mark][1][1] = m[mark][1][1];
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i==1 && j==1)
continue;
if(i == 1)
{
dp[mark][i][j] = dp[mark][i][j-1] + m[mark][i][j];
vis[mark][i][j] = 1;//向右
}
else if(j == 1)
{
dp[mark][i][j] = dp[mark][i-1][j] + m[mark][i][j];
vis[mark][i][j] = 0;//向下
}
else
{
int tt1 = dp[mark][i-1][j];
int tt2 = dp[mark][i][j-1];
if(tt1 < tt2)
{
dp[mark][i][j] = tt1 + m[mark][i][j];
vis[mark][i][j] = 0;
}
else
{
dp[mark][i][j] = tt2 + m[mark][i][j];
vis[mark][i][j] = 1;
}
}
}
}
return dp[mark][n][n];
} void print(int mark, int x, int y)
{
if(x==1 && y==1)
return ;
if(vis[mark][x][y] == 0)
{
print(mark,x-1,y);
printf("D");
}
else
{
print(mark,x,y-1);
printf("R");
}
} int main()
{
int x, y;
while(~scanf("%d",&n))
{
int tt, t1, t2;
memset(vis,0,sizeof(vis));
memset(m,0,sizeof(m));
int flag = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d",&tt);
if(tt == 0)
{
flag = 1;
x = i;
y = j;
continue;
}
t1 = t2 = tt;
while(t1%2 == 0)
{
t1/=2;
m[0][i][j]++;
}
while(t2%5 == 0)
{
t2/=5;
m[1][i][j]++;
}
}
} int ans1 = solve(0);//路径存在最少的2的个数
int ans2 = solve(1);//路径存在最少的5的个数
//printf("ans1:%d ans2:%d\n",ans1,ans2);
int mark = 0;
int ans = 0;
if(ans1 < ans2)
{
ans = ans1;
mark = 0;
}
else
{
ans = ans2;
mark = 1;
}
if(flag && ans > 1)
{
printf("1\n");//有零存在那么终于结果就仅仅有一个零
for(int i = 2; i <= x; i++)
{
//向下到有零的那一行
printf("D");
}
for(int j = 2; j <= n; j++)
{
//走到最右边
printf("R");
}
for(int i = x+1; i <= n; i++)
{
//走到最下边
printf("D");
}
printf("\n");
continue;
}
printf("%d\n",ans);
print(mark, n, n);
printf("\n");
}
return 0;
} /*
3
4 10 5
10 9 4
6 5 3
3
4 10 5
6 0 2
7 8 9
*/

CodeForces B. The least round way(dp)的更多相关文章

  1. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  2. Codeforces 837D - Round Subset(dp)

    837D - Round Subset 思路:dp.0是由2*5产生的. ①dp[i][j]表示选i个数,因子2的个数为j时因子5的个数. 状态转移方程:dp[i][j]=max(dp[i][j],d ...

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

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

  4. 【Codeforces】CF 2 B The least round way(dp)

    题目 传送门:QWQ 分析 求结尾0的数量QwQ. 10只能是$ 2 \times 5 $,我们预处理出每个数因子中2和5的数量. 我们接着dp出从左上到右下的经过的最少的2的数量和最少的5的数量.两 ...

  5. codeforces 425C Sereja and Two Sequences(DP)

    题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...

  6. Codeforces 629C Famil Door and Brackets(DP)

    题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...

  7. codeforces #267 C George and Job(DP)

    职务地址:http://codeforces.com/contest/467/problem/C 太弱了..这题当时都没做出来..思路是有的,可是自己出的几组数组总是过不去..今天又又一次写了一遍.才 ...

  8. Codeforces 403D: Beautiful Pairs of Numbers(DP)

    题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...

  9. codeforces 459 E. Pashmak and Graph(dp)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...

随机推荐

  1. 路飞学城Python-Day14

    转载:python之路-路飞学城-python-book [25.常用模块-logging模块详解] [26.常用模块-logging模块详解2] [27.常用模块-logging模块日志过滤和日志文 ...

  2. selenium自动化(三).........................................框架篇

    三.Unittest框架介绍: 1.Unittest类似于java中的Junit,功能较为简单,逻辑简单,理解和使用起来比较简单 1)       安装:自带框架,无需安装 2)       使用:可 ...

  3. JavaScript设计模式(biaoyansu)(2)

    单例模式实例 (创建类模式): let elBalance = document.getElementById('balance') function init () { var a = new Di ...

  4. 添加ArcGIS数据

    加载arcgis server的rest服务瓦片数据:ol.layer.Tile+ol.source.TileArcGISRest 加载arcgis online的在线瓦片数据:ol.layer.Ti ...

  5. vue项目测试和打包发布

    在线测试:npm run dev 发布打包:npm run build  打包后,代码文件在dist文件夹下面,可以正式发布了,复制到其它web服务器下面,在浏览器用http访问.

  6. Python 线程高级篇 threading 模块的使用

    创建一个tread实例,给他传一个函数 #!/usr/bin/python import threading from time import * loops =[4,2] def loop (nlo ...

  7. spring中IOC的简单使用

    spring的ioc主要就是依赖注入,有基于构造器的依赖注入还有通过设值注入,这里我只简单的实现设值注入的方法,通过spring的依赖管理,我们可以很方便的了解各层之间的依赖关系,降低了各层之间的耦合 ...

  8. 和同事合作开发,使用局域网 git创建本地仓库

    转自原文 和同事合作开发,使用局域网 git创建本地仓库 1.仓库 建一个空文件夹来做仓库,例如建为 cangku 1.1 cd 到 cangku目录下 创建远程仓库容器 mkdir  mycangk ...

  9. linux系统调用表(system call table)

    系统调用号 函数名 入口点 源码 0 read sys_read fs/read_write.c 1 write sys_write fs/read_write.c 2 open sys_open f ...

  10. 关于DPM(Deformable Part Model)算法中模型可视化的解释

    搭建了自己的博客平台,本文地址:http://masikkk.com/blog/DPM-model-visualization/ DPM源代码(voc-release)中的模型可视化做的还算相当炫酷的 ...