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. Java面向对象的三个特征与含义

    封装 1.英文为 encapsulation,实现信息隐藏: 2.把同一类事物的特性归纳到一个类中(属性和行为),隐藏对象的内部实现: 继承 1.英文为 inheritance: 2.继承的过程,是从 ...

  2. Linux 入门记录:三、Linux 文件基本操作管理

    一.复制文件.目录 使用 cp 命令复制文件或目录: $ cp 源文件(夹)目标文件(夹) 常用参数: -r 递归复制整个目录树 -v 显示复制过程的详细信息 二.移动.重命名文件或目录 通过 mv  ...

  3. OpenRCT2-ext

    https://github.com/RollingStar/RCT-Music-Patch https://github.com/seanfisk/rct2-game-objects https:/ ...

  4. codevs 3287 货车运输 NOIP2013提高组

    题目链接:http://codevs.cn/problem/3287/ 题解: 和bzoj3732一毛一样,只不过是找最大生成树和最小值罢了,具体参见我的bzoj3732的博客 #include< ...

  5. HighGUI图形图像界面初步——鼠标操作

    OpenCV中的鼠标操作和滑动条的消息映射方式很类似,都是通过一个中介函数配合一个回调函数来实现的,创建和指定滑动条回调函数为createTrackbar, 而指定鼠标操作消息回调函数的函数为setM ...

  6. signal, sigaction,信号集合操作

    信号是与一定的进程相联系的,而建立其信号和进程的对应关系,这就是信号的安装登记. Linux主要有两个函数实现信号的安装登记:signal和sigaction.其中signal在系统调用的基础上实现, ...

  7. HDU 2829 Lawrence(四边形优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

  8. hdu 2768(建图,最大点独立集)

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. bzoj1030 AC自动机+dp

    思路:建状态图,在状态图上dp. #include<bits/stdc++.h> #define LL long long #define ll long long #define fi ...

  10. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...