B. The least round way

题目连接:

http://www.codeforces.com/contest/2/problem/B

Description

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.

Sample Input

3

1 2 3

4 5 6

7 8 9

Sample Output

0

DDRR

Hint

题意

给你一个n*n的矩阵

然后这个矩阵你需要从左上角走到右下角

只能走右或者向下。

你要使得你经过的数,乘积起来后面的0的个数最少。

题解:

dp[i][j][0]表示到i,j位置,2的因子最少多少个

dp[i][j][1]表示到i,j位置,5的因子最少多少个

然后答案显然就是min(dp[n][n][0],dp[n][n][1])了,然后倒着dfs输出答案就好了。

但是这儿有一个hack点,就是如果有一个位置是0的话,答案就最多为1了,这个是显然的嘛。

然后就没了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1100;
const int inf = 1e9;
int dp[maxn][maxn][2];
int cnt[maxn][maxn][2];
int g[maxn][maxn][2];
int n,a[maxn][maxn];
void solve(int x,int y,int now)
{
if(x==1&&y==1)return;
if(g[x][y][now]==1)solve(x-1,y,now),printf("D");
else solve(x,y-1,now),printf("R");
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
int flagx=0,flagy=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]==0)
{
flagx=i,flagy=j;
break;
}
int pre = a[i][j];
while(a[i][j]%2==0)cnt[i][j][0]++,a[i][j]/=2;
while(a[i][j]%5==0)cnt[i][j][1]++,a[i][j]/=5;
}
}
memset(dp,0x3f,sizeof(dp));
memset(g,0,sizeof(g));
dp[1][1][0]=cnt[1][1][0],dp[1][1][1]=cnt[1][1][1];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==1&&j==1)continue;
for(int k=0;k<2;k++)
{
dp[i][j][k]=cnt[i][j][k]+min(dp[i-1][j][k],dp[i][j-1][k]);
if(dp[i-1][j][k]<dp[i][j-1][k])g[i][j][k]=1;
}
}
}
int ans = min(dp[n][n][0],dp[n][n][1]);
int st;
if(dp[n][n][0]<dp[n][n][1])st=0;
else st=1;
if(ans==0)puts("0");
else if(flagx&&flagy)
{
puts("1");
int nowx=1,nowy=1;
while(nowx<flagx)nowx++,printf("D");
while(nowy<flagy)nowy++,printf("R");
while(nowx<n)nowx++,printf("D");
while(nowy<n)nowy++,printf("R");
return 0;
}
else printf("%d\n",ans);
solve(n,n,st);
}

Codeforces Beta Round #2 B. The least round way dp的更多相关文章

  1. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  2. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  3. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  4. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  5. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  6. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  10. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

随机推荐

  1. mysql 复制表结构 / 从结果中导入数据到新表

    这只会复制结构: mysql> create table a like mysql1; Query OK, 0 rows affected (0.03 sec) mysql> desc a ...

  2. 【DataScience学习笔记】Coursera课程《数据科学家的工具箱》 约翰霍普金斯大学——Week3 Conceptual Issues课堂笔记

    Coursera课程<数据科学家的工具箱> 约翰霍普金斯大学 Week3 Conceptual Issues Types of Questions Types of Data Scienc ...

  3. Bookmarks_www2

    Bookmarks Bookmarks alexis- (Alex Incogito) - Repositories · GitHub GitHub - aetcnc-Arduino_DeltaHMI ...

  4. STL之顺序容器 deque 动态数组

    deque是一个动态数组,deque与vector非常类似,vector是一个单向开口的连续线性空间,deque则是双向开口的连续线性空间.两者唯一的区别是deque可以在数组的开头和末尾插入和删除数 ...

  5. System.getProperty方法中输出路径的方法

    package codegenerator;/** *@author Eilen *@date 2017年9月27日---下午3:15:09 *@描述: *@answer */public class ...

  6. 3.Python3标准库--数据结构

    (一)enum:枚举类型 import enum ''' enum模块定义了一个提供迭代和比较功能的枚举类型.可以用这个为值创建明确定义的符号,而不是使用字面量整数或字符串 ''' 1.创建枚举 im ...

  7. 502 bad gateway,ngix

    1.ftm对nginx的解析出现问题:20171228

  8. mycncart 前台代码跟踪

    1.进入根目录的入口文件,index.php require_once(DIR_SYSTEM . 'startup.php');//最为重要的一步 start('catalog');//执行了这个方法 ...

  9. 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记6——四大变换&光照与材质

    第13章 四大变换 在Direct3D中,如果为进行任何空间坐标变换而直接绘图的话,图形将始终处于应用程序窗口的中心位置,默认这个位置就成为世界坐标系的原点(0,0,0).而且我们也不能改变观察图形的 ...

  10. canvas时钟效果

    话不多说,直接上代码 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/x ...