Codeforces 2B. The least round way
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的更多相关文章
- Codeforces #2B The least round way(DP)
Description 有一个n*n的正整数矩阵,要你求一条从第一行第一列的格子到第n行第n列的路,使得你走过的格子里面的数乘起来的值末尾的零的个数最小.输出最小个数. Input 第一行包括1个数n ...
- codeforces 2B The least round way(DP+数学)
The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...
- Codeforces 2B The least round way(dp求最小末尾0)
题目链接:http://codeforces.com/problemset/problem/2/B 题目大意: 给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0 ...
- codeforces 2B The least round way 【DP】
VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...
- 最小较小codeforces 2B The least round way
查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧! 求从左上角到右下角所经过的数字之积末端所含0最小的个数 终究的积可以当作A*2^x*5^y, ...
- 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 ...
- 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的点反向遍 ...
- codeforces B. George and Round 解题报告
题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...
- Codeforces Technocup 2017 - Elimination Round 2 D. Sea Battle(贪心)
题目链接 http://codeforces.com/contest/729/problem/D 题意:给你一个1*n的区域有a艘船,每艘船宽b,已经开了k枪都没打到,问你最少再开几枪至少能打到一艘船 ...
随机推荐
- 团队作业7——第二次项目冲刺(Beta版本12.04)
1.当天站立式会议照片 本次会议内容:1:每个人汇报自己完成的工作.2:组长分配各自要完成的任务. 2.每个人的工作 黄进勇:项目整合,后台代码. 李勇:前台界面优化. 何忠鹏:数据库模块. 郑希彬: ...
- 消除ExtJS6的extjs-trila字样
- JavaScript-Jquery实现全选反选
html: <dl> <dt><input type="checkbox" id="checkAll" /><labe ...
- BizTalk Server 2016配置 WCF SAP Adapter
BizTalk Server 2016配置 WCF SAP Adapter 最近公司内部需要使用BizTalk与SAP 系统进行对接,虽然SAP/PI可以以发布WebService 的方式实现与外部系 ...
- 第1章 什么是TCP-IP
第1章 什么是TCP-IP 什么是网络 网络是计算机或类似计算机的设备之间通过常用传输介质进行通信的集合.通常情况下,传输介质是绝缘的金属导线, 它用来在计算机之间携带电脉冲,介质也可以是电话线,甚至 ...
- 《深入实践Spring Boot》阅读笔记之二:分布式应用开发
上篇文章总结了<深入实践Spring Boot>的第一部分,这篇文章介绍第二部分:分布式应用开发,以及怎么构建一个高性能的服务平台. 主要从以下几个方面总结: Spring Boot SS ...
- jmeter入门(02)测试报告各项指标含义
一.名词定义(时间单位ms) 1.聚合报告 Sample:本次测试场景共运行多少个请求: Average:平均响应时间: Median:统计意义上的响应时间中值: 90% line:所有线程中90%的 ...
- 实现一个网易云音乐的 BottomSheetDialog
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- shell:正则表达式和文本处理器
1.什么是正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 生活中处处都是正则: 比如我们描述:4条腿 你可能会想 ...
- linux下的mysql安装
写在开头的小故事: 很久以前小石头我在一家公司做运维工程师,当时我们有一台认证服务器安装了mysql5.5版本.有一天领导说防止它挂掉,做个主从复制吧,我开心的接受任务,但是让某同事听到了, 此同事代 ...