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. Eclipse创建Maven多模块工程

    一.创建父项目 [New]->[Maven Project] 在弹出界面中选择[Create a simple project...] 二.创建子项目 选中刚建的父项目,在弹出菜单中点击[New ...

  2. vue2源码浏览分析01

    1.构造函数  Vue$3 function Vue$3 (options) { if ("development" !== 'production' && !(t ...

  3. Oracle中的 row_number() over (partition by order by ) 用法

    oracle 里面经常这样用 select col1,col2..., row_number() over (partition by colx order by coly) from table_n ...

  4. 【Cocos2dx游戏开发】CCNotificationCenter传递消息和数据

    在开发游戏的时候我们经常需要在层与层之间.场景与场景之间传递数据和消息,Cocos2dx框架应用观察者模式为我们封装了一个CCNotificationCenter类,也叫消息通知中心,它也是一个单例类 ...

  5. java使用poi读取word(简单,简约,直观)

    java使用poi读取word(简单,简约,直观) 说明 其实poi的官网上面都是有接口和样例的,只是都是英文 例如网址:http://poi.apache.org/spreadsheet/quick ...

  6. 【Bzoj2456】mode

    Position: http://www.lydsy.com/JudgeOnline/problem.php?id=2456 List Bzoj2456 mode List Description S ...

  7. ajax接收处理json格式数据

    ajax在前后端的交互中应用非常广泛,通过请求后台接口接收处理json格式数据展现在前端页面. 下面我们来简单用 ajax在本地做一个接收并处理json的小例子 首先我们要新建一个叫做data的jso ...

  8. PCB Winform中的WebBrowser扩展拖放(拖拽)功能 实现方法

    我们在Winform支持网页通常增加WebBrowser控件实现,相当于内嵌浏览器浏览网页使用, 而此WebBrowser默认情况是文件拖入功能是不支持的, 如何才能支持呢.在这里介绍如何实现方法 一 ...

  9. E20171015-hm

    quirk   n. 怪癖; 奇事,巧合; 突然的弯曲; propagation  n. 宣传; 传播,传输,蔓延,扩展,波及深度; [生]繁殖法,[地]传导; 培养; immediate  adj. ...

  10. cookie应用(一周内免登陆)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...