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枪都没打到,问你最少再开几枪至少能打到一艘船 ...
随机推荐
- Django 个性化管理员站点
from django.contrib import admin # Register your models here. from .models import Moment class Momen ...
- Flask 学习 七 用户认证
使用werkzeug 实现密码散列 from werkzeug.security import generate_password_hash,check_password_hash class Use ...
- Struts2之配置文件中Action的详细配置(续)
承接上一篇 4.处理结果的配置 Action类的实例对象调用某个方法,处理完用户请求之后,将返回一个逻辑视图名的字符串.核心Filter收到返回的逻辑视图名字符串,根据struts.xml中的逻辑视图 ...
- 【learning】多项式相关(求逆、开根、除法、取模)
(首先要%miskcoo,这位dalao写的博客(这里)实在是太强啦qwq大部分多项式相关的知识都是从这位dalao博客里面学的,下面这篇东西是自己对其博客学习后的一些总结和想法,大部分是按照其博客里 ...
- [2]十道算法题【Java实现】
前言 清明不小心就拖了两天没更了-- 这是十道算法题的第二篇了-上一篇回顾:十道简单算法题 最近在回顾以前使用C写过的数据结构和算法的东西,发现自己的算法和数据结构是真的薄弱,现在用Java改写一下, ...
- Dojo API中文 Dojo内容模块概览,初学者
官网:http://dojotoolkit.org/reference-guide/1.10/dojo/index.html#dojo-dojo的翻译 dojo 内容: dojo dojo/dojo ...
- Netty事件监听和处理(下)
上一篇 介绍了事件监听.责任链模型.socket接口和IO模型.线程模型等基本概念,以及Netty的整体结构,这篇就来说下Netty三大核心模块之一:事件监听和处理. 前面提到,Netty是一个NIO ...
- angular2 学习笔记 の 移动端开发 ( 手势 )
更新 : 2018-01-31 (hammer 的坑) hammer 的 pinch 在某种情况下会自动触发 panEnd,很奇葩. 解决方法就是记入时间呗 refer : https://githu ...
- SpringCloud的服务网关zuul
演示如何使用api网关屏蔽各服务来源 一.概念和定义 1.zuul最终还是使用Ribbon的,顺便测试一下Hystrix断路保护2.zuul也是一个EurekaClient,访问服务注册中心,获取元数 ...
- Django 相关
Web框架本质 其实所有的Web应用本质就是一个socket服务端,而用户的浏览器就是一个socket客户端.简单的socket代码如下: import socket sk = socket.sock ...