Codeforces_The least round way
2 seconds
64 megabytes
standard input
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.
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).
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
3
1 2 3
4 5 6
7 8 9
0
DDRR
题意:从左上到右下,选择一条路,使得路上每个格子中的数相乘末尾的0最少。
思路:一般情况下,只有2和5的组合才会在末尾形成0。之前一直想在dp过程中同时推2和5,然后就wa了,其实分别推2和5并记录两个路径就行,因为一般情况下,末尾0等于min(dp2[n][n],dp5[n][n]),但是还有特殊情况,就是路径中遇到0,经过0的路径最终只有一个0,但还有可能一个0都没有,要讨论全面。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
#define N 1005
#define INF 1000000
int num2[N][N],num5[N][N];
int dp2[N][N],dp5[N][N];
char dir2[N][N],dir5[N][N]; int getNum(int num,int a)
{
if(num==)
return ;
int cnt=;
while(num%a==)
{
cnt++;
num/=a;
}
return cnt;
} void dfs(char dir[][N], int x,int y)
{
if(x==&&y==)
return;
if(dir[x][y]=='R')
dfs(dir,x,y-);
else if(dir[x][y]=='D')
dfs(dir,x-,y);
printf("%c",dir[x][y]);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int z_flag=,z_x,z_y;
memset(num2,,sizeof(num2));
memset(num5,,sizeof(num5));
memset(dp2,,sizeof(dp2));
memset(dp5,,sizeof(dp5));
//memset(dp,0,sizeof(dp));
/*for(int i=0; i<=n; i++)
{
dp2[0][i]=dp2[i][0]=INF;
dp5[0][i]=dp5[i][0]=INF;
}
dp2[0][1]=dp2[1][0]=0;
dp5[0][1]=dp5[1][0]=0;*/
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
int num;
scanf("%d",&num);
if(num)
{
num2[i][j]=getNum(num,);
num5[i][j]=getNum(num,);
}
else
{
z_flag=;
z_x=i;
z_y=j;
}
}
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
if(i==&&j==)
{
dir2[i][j]=' ';
dp2[i][j]=num2[i][j];
}
else if(i==)
{
dir2[i][j]='R';
dp2[i][j]=dp2[i][j-]+num2[i][j];
}
else if(j==)
{
dir2[i][j]='D';
dp2[i][j]=dp2[i-][j]+num2[i][j];
}
else
{
if(dp2[i-][j]<dp2[i][j-])
{
dir2[i][j]='D';
dp2[i][j]=dp2[i-][j]+num2[i][j];
}
else
{
dir2[i][j]='R';
dp2[i][j]=dp2[i][j-]+num2[i][j];
}
}
}
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
if(i==&&j==)
{
dir5[i][j]=' ';
dp5[i][j]=num5[i][j];
}
else if(i==)
{
dir5[i][j]='R';
dp5[i][j]=dp5[i][j-]+num5[i][j];
}
else if(j==)
{
dir5[i][j]='D';
dp5[i][j]=dp5[i-][j]+num5[i][j];
}
else
{
if(dp5[i-][j]<dp5[i][j-])
{
dir5[i][j]='D';
dp5[i][j]=dp5[i-][j]+num5[i][j];
}
else
{
dir5[i][j]='R';
dp5[i][j]=dp5[i][j-]+num5[i][j];
}
}
}
if(z_flag)
{
if(dp2[n][n]==)
{
printf("0\n");
dfs(dir2,n,n);
printf("\n");
}
else if(dp5[n][n]==)
{
printf("0\n");
dfs(dir5,n,n);
printf("\n");
}
else
{
printf("1\n");
for(int i=;i<z_x;i++)
printf("D");
for(int i=;i<n;i++)
printf("R");
for(int i=z_x;i<n;i++)
printf("D");
printf("\n");
}
}
else
{
if(dp2[n][n]<dp5[n][n])
{
printf("%d\n",dp2[n][n]);
dfs(dir2,n,n);
printf("\n");
}
else
{
printf("%d\n",dp5[n][n]);
dfs(dir5,n,n);
printf("\n");
}
}
}
return ;
}
Codeforces_The least round way的更多相关文章
- SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...
- SQL中Round(),Floor(),Ceiling()函数的浅析
项目中的一个功能模块上用到了标量值函数,函数中又有ceiling()函数的用法,自己找了一些资料,对SQL中这几个函数做一个简单的记录,方便自己学习.有不足之处欢迎拍砖补充 1.round()函数遵循 ...
- oracle的round函数和trunc函数
--Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013- ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- mathlab之floor,ceil,round,int以及fix函数
建议自己动手敲敲,网上很多人自己都没搞清楚然后好多错的.毕竟自己亲眼看到结果才有说服力. 以下是我亲眼见到的结果. 1.double floor(double)函数 floor()函数是常用的取整函数 ...
- SQL SERVER四舍五入你除了用ROUND还有其他方法吗?
引言 今天和测试沟通一个百分比计算方式时遇到一个问题, 我在存储过程里用到了强转CAST(32.678 AS DECIMAL(5,1)) 我认为该方式只会保留一位小数,我给测试的回复是我并没有用到四 ...
随机推荐
- Ubuntu下非常规方法安装绿色软件(压缩包)
继上一篇http://www.cnblogs.com/EasonJim/p/7117567.html文章中说的常规方式安装的软件,都会自动在命令行及Dash Home中体现. 但是如果是使用压缩包进行 ...
- _deque实现
/* deque是一种双向开口的连续线性空间,可以在头尾两端分别做元素的插入和删除操作 常用接口:back(), front(), push_back(), pop_back(), push_fron ...
- Java序列化之readObjectNoData、readResolve方法
Java序列化之readObjectNoData.readResolve方法 学习了:http://vyloy.iteye.com/blog/1240663 readResolve方法会在Object ...
- android学习笔记(9)android程序调试学习
相应若水老师的第十四课 一,Log日志输出 Log.v(tag,message); //verbose模式,打印最具体的日志 Log.d(tag,message); // ...
- Spring Web Flow 入门demo(二)与业务结合 附源代码
第一部分demo仅仅介绍了简单的页面跳转,接下来我们要实现与业务逻辑相关的功能. 业务的逻辑涉及到数据的获取.传递.保存.相关的业务功能函数的调用等内容,这些功能的实现都可用Java 代码来完毕,但定 ...
- 【Linux命令】--(1)文件文件夹操作命令15条
文件文件夹操作命令++++++++++++++++++++++++++++++++++++++++ 列出进入显示文件夹 ls cd pwd 创建移动删除文件 mkdir rm rmd ...
- Js跨一级域名同步cookie
1. 纯Js同步两个域名下的cookie document.cookie = "name=" + "value;" + "expires=" ...
- C#程序猿学习 Python
孙广东 2016.1.1 交互: C# 运行Python代码: http://stackoverflow.com/questions/11779143/run-a-python-script-fro ...
- LeetCode 941. Valid Mountain Array (有效的山脉数组)
题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...
- cocos2d的armature绑定到其它armature骨骼上的bug
在cocos2dx中,rmature的骨骼上能够绑定另外的armature,在我的项目中使用了该功能来完毕骑乘功能,可是在使用过程发现了例如以下的bug,特写在这里做一下记录. </span&g ...