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枪都没打到,问你最少再开几枪至少能打到一艘船 ...
随机推荐
- Beta集合
Beta冲刺day1 Beta冲刺day2 Beta冲刺day3 Beta冲刺day4 Beta冲刺day5 Beta冲刺day6 Beta冲刺day7 测试总结 总结合集 Beta预备
- 《招一个靠谱的移动开发》iOS面试题及详解(上篇)
以下问题主要用于技术的总结与回顾 主要问题总结 单例的写法.在单利中创建数组应该注意些什么. NSString 的时候用copy和strong的区别. 多线程.特别是NSOperation 和 GCD ...
- css变化代码
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- 爬虫系列(1)-----python爬取猫眼电影top100榜
对于Python初学者来说,爬虫技能是应该是最好入门,也是最能够有让自己有成就感的,今天在整理代码时,整理了一下之前自己学习爬虫的一些代码,今天先上一个简单的例子,手把手教你入门Python爬虫,爬取 ...
- Python内置函数(28)——iter
英文文档: iter(object[, sentinel]) Return an iterator object. The first argument is interpreted very dif ...
- 赛码网算法: 上台阶 ( python3实现 、c实现)
上台阶 题目描述 有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法?注:规定从一级到一级有0种走法. 输入输入数据首先包含一个整数n(1<=n<=1 ...
- Django form表单
Form介绍 之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来.与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入, ...
- [转]安卓新一代多渠道打包工具Walle 解决渠道包V2签名问题
转自https://www.jianshu.com/p/572b59829a08 为什么要打多个渠道的包? 大家都知道,android应用商店大大小小有几百个,作为一个有志向的app,就需要做到统计各 ...
- mybatis批量插入
<insert id="insertBatch" parameterType="java.util.List" > insert into biz_ ...
- 拿来主义:layPage分页插件的使用
布衣之谈 所谓插件,大概就是项目中可插可拔的比较小功能化的组件:这些功能组件若能力可及,自己也可以完成——也即自己造轮子,但翻看各种技术社区,相关领域的神人们往往会有更好的实现方案贡献出来,这个时候你 ...