B. The least round way
time limit per test

2 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.

Examples
input
3
1 2 3
4 5 6
7 8 9
output
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的更多相关文章

  1. SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等

    在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...

  2. SQL中Round(),Floor(),Ceiling()函数的浅析

    项目中的一个功能模块上用到了标量值函数,函数中又有ceiling()函数的用法,自己找了一些资料,对SQL中这几个函数做一个简单的记录,方便自己学习.有不足之处欢迎拍砖补充 1.round()函数遵循 ...

  3. oracle的round函数和trunc函数

    --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013- ...

  4. 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 ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

  7. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  8. mathlab之floor,ceil,round,int以及fix函数

    建议自己动手敲敲,网上很多人自己都没搞清楚然后好多错的.毕竟自己亲眼看到结果才有说服力. 以下是我亲眼见到的结果. 1.double floor(double)函数 floor()函数是常用的取整函数 ...

  9. SQL SERVER四舍五入你除了用ROUND还有其他方法吗?

    引言 今天和测试沟通一个百分比计算方式时遇到一个问题, 我在存储过程里用到了强转CAST(32.678 AS DECIMAL(5,1))  我认为该方式只会保留一位小数,我给测试的回复是我并没有用到四 ...

随机推荐

  1. Linux内核project导论——前言

    想要研究linux内核.使用linux内核,首先要知道linux内核能做到什么,提供了什么.我看过非常多刚開始学习的人一进入公司就開始使用linux内核开发内核模块.使用的不管是通信方式.内存接口还是 ...

  2. Shell细小问题汇总

    Shell细小问题汇总 本文原文出处: http://blog.csdn.net/bluishglc/article/details/44276607 严禁不论什么形式的转载,否则将托付CSDN官方维 ...

  3. AutoCAD如何移动坐标原点

    通常在CAD画图设计时,坐标原点都默认在左下角,下面就来分享一下在CAD如何把左下角的坐标原点移动到我们画的图形中心点: 1.输入坐标原点移动命令UCS: 按回车确认后,再输入M(就是移动的意思): ...

  4. 从打击App刷榜看苹果的底线

    这两天苹果打击App刷榜者的消息刷屏了,从腾讯科技.appying多个媒体渠道看到,<安居客>.<友秘>.<微在>.<秦时明月2>.<悟空与貂蝉& ...

  5. scanner使用中遇见的问题

    近期在学习的过程中遇见一个问题,问题不难但还是须要去认真对待. 先看看我写的源码 public static void main(String[] args){ for(;;){ Scanner in ...

  6. 避免死锁的银行家算法C++程序实现

     本篇博文为追忆以前写过的算法系列第二篇(20081021) 温故知新 目的:具有代表性的死锁避免算法是Dijskstra给出的银行家算法.本实验是基于银行家算法的思想通过编写C++程序实现银行家 ...

  7. [译]使用AssetBundle Manader

    AssetBundle and the AssetBundle Manager 介绍 AssetBundle允许从本地或者远程服务器加载Assets资源,利用AssetBundles技术,Assets ...

  8. C# MySql 连接

    1.将MySql.Data.dll引用到你的项目中 右键工程去完成. 2.using MySql.Data;  using MySql.Data.MySqlClient; 3. MySqlConnec ...

  9. Kubernetes 集群中使用 Helm 搭建 Spinnaker

    在我们部署Spinnaker之前,我们需要一个YAML格式的配置文件,它会包含了一些配置信息.可以从Spinnaker Helm Chart repository[2]获得这个文件. $curl -L ...

  10. poj1611 并查集 (路径压缩)

    http://poj.org/problem?id=1611 题目大意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社 ...