2B The least round way
题目大意:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
typedef long long LL;
const LL INF = 0xffffff;
const int maxn = ;
const LL MOD = 1e9+;
struct node
{
int num2, num5;
} dp2[maxn][maxn], dp5[maxn][maxn], a[maxn][maxn];
int K[maxn][maxn], n; bool HaveZero(int& x,int& y)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(K[i][j] == )
{
x = i;
y = j;
return true;
}
}
}
return false;
} void PutPath5(int x,int y)
{
if(x == && y == )
return;
if(dp5[x][y].num2 == dp5[x-][y].num2 + a[x][y].num2 && dp5[x][y].num5 == dp5[x-][y].num5 + a[x][y].num5)
{
PutPath5(x-, y);
printf("D");
}
else
{
PutPath5(x, y-);
printf("R"); }
} void PutPath2(int x,int y)
{
if(x == && y == )
return;
if( dp2[x][y].num2 == dp2[x-][y].num2 + a[x][y].num2 && dp2[x][y].num5 == dp2[x-][y].num5 + a[x][y].num5)
{
PutPath2(x-, y);
printf("D");
}
else
{
PutPath2(x, y-);
printf("R");
}
} void PutPath()
{
int x, y;
int ans = min( min(dp2[n][n].num2,dp2[n][n].num5) , min(dp5[n][n].num2,dp5[n][n].num5) );
if( HaveZero(x,y) && ans > )
{
printf("1\n");
for(int i=; i<=x; i++)
printf("D");
for(int i=; i<=y; i++)
printf("R");
for(int i=x+; i<=n; i++)
printf("D");
for(int i=y+; i<=n; i++)
printf("R");
return ;
}
if( min(dp2[n][n].num2,dp2[n][n].num5) < min(dp5[n][n].num2,dp5[n][n].num5) )
{
printf("%d\n", min(dp2[n][n].num2,dp2[n][n].num5));
PutPath2(n, n);
}
else
{
printf("%d\n", min(dp5[n][n].num2,dp5[n][n].num5));
PutPath5(n, n);
} } void Process(int i,int j,int p)
{
int k = p;
while(k)
{
if(k% == )
a[i][j].num2 ++;
else
break;
k /= ;
}
k = p;
while(k)
{
if(k% == )
a[i][j].num5 ++;
else
break;
k /= ;
}
}
int main()
{
cin >> n;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
scanf("%d", &K[i][j]);
Process(i, j, K[i][j]);
} for(int i=; i<=n; i++)
{
dp2[i][].num5 = dp2[i][].num2 = dp2[][i].num2 = dp2[][i].num5 = INF;
dp5[i][].num5 = dp5[i][].num2 = dp5[][i].num2 = dp5[][i].num5 = INF;
a[][i].num2 = a[][i].num5 = a[i][].num2 = a[i][].num5 = INF;
}
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
dp2[i][j] = a[i][j];
dp5[i][j] = a[i][j];
if(dp2[i-][j].num2 > dp2[i][j-].num2 && j->)
{
dp2[i][j].num2 += dp2[i][j-].num2 ;
dp2[i][j].num5 += dp2[i][j-].num5 ;
}
else if(i- > )
{
dp2[i][j].num2 += dp2[i-][j].num2;
dp2[i][j].num5 += dp2[i-][j].num5;
} if(dp5[i-][j].num5 > dp5[i][j-].num5 && j->)
{
dp5[i][j].num2 += dp5[i][j-].num2;
dp5[i][j].num5 += dp5[i][j-].num5;
}
else if(i- > )
{
dp5[i][j].num2 += dp5[i-][j].num2 ;
dp5[i][j].num5 += dp5[i-][j].num5 ;
}
} PutPath();
printf("\n");
return ;
}
2B The least round way的更多相关文章
- Codeforces #2B The least round way(DP)
Description 有一个n*n的正整数矩阵,要你求一条从第一行第一列的格子到第n行第n列的路,使得你走过的格子里面的数乘起来的值末尾的零的个数最小.输出最小个数. Input 第一行包括1个数n ...
- Codeforces 2B. The least round way
There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a w ...
- codeforces 2B The least round way(DP+数学)
The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...
- CF 2B.The least round way
题目链接 很久以前就见过此题,以前看了题解,然后今天写了写,写的真搓. #include <cstdio> #include <cstring> #include <st ...
- codeforces 2B The least round way 【DP】
VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...
- 最小较小codeforces 2B The least round way
查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧! 求从左上角到右下角所经过的数字之积末端所含0最小的个数 终究的积可以当作A*2^x*5^y, ...
- CF 2B The least round way DP+Math
题意: 找出一条路, 使每个节点相乘,得到的数末尾 0 最少 每次移动只能向右或者向下, 找到后打印路径 ///按照题目要求,就是找出一条从左上角到右下角中每个数含2 or 5 最少的路 ///可以用 ...
- Codeforces 2B The least round way(dp求最小末尾0)
题目链接:http://codeforces.com/problemset/problem/2/B 题目大意: 给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0 ...
- CF dp 题(1500-2000难度)
前言 从后往前刷 update 新增 \(\text{\color{red}{Mark}}\) 标记功能,有一定难度的题标记为 \(\text{\color{red}{红}}\) 色. 题单 (刷过的 ...
随机推荐
- Java基础知识强化之集合框架笔记23:ArrayList的实现原理
1. ArrayList的实现原理: 这个可以直接参考网友的博客:http://www.cnblogs.com/ITtangtang/p/3948555.html
- 安装jdk后出现bash: ./java: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
用sudo yum install glibc.i686命令安装好glibc之后问题就解决了
- SQL语句优化(分享)
一.操作符优化 1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用 ...
- google的西联汇款可以使用工行代收
- linux设置中文环境
确认当前环境 [root@Oracle11g ~]# df –h Filesystem Size Used Avail Use% Mounted on /dev/sda1 ...
- OpenSSL安装及目录介绍
1. 下载安装OpenSSL 2. 根据需要下载安装Visual C++ Redistributable 3. 安装完成OpenSSL后,目录结构如下所示 其中,bin文件夹下是OpenSSL主程序的 ...
- Spring中@Autowired注解与自动装配
1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...
- SGU 249.Matrix(Gray码)
题意: 用0到2^(n+m-1)这2^(n+m-1)个数填在一个2^n*2^m的矩阵里,使得所有相邻的数的二进制表示只有一位不同. Solution: Gray码.对于第i行第j列的数,由i的Gray ...
- jQuery慢慢啃之特效(八)
1.show([speed,[easing],[fn]])\\显示隐藏的匹配元素 //speed:三种预定速度之一的字符串("slow","normal", o ...
- 关于Asp.Net中避免用户连续多次点击按钮,重复提交表单的处理
Web页面中经常碰到这类问题,就是客户端多次点击一个按钮或者链接,导致程序出现不可预知的麻烦. 客户就是上帝,他们也不是有意要给你的系统造成破坏,这么做的原因很大一部分是因为网络慢,点击一个操作之后, ...