题目大意:

一个n*n的矩阵,从矩阵的左上角开始,每次移动到下面或者右面,移动到右下角结束。 要求走的路径上的所有数字乘起来,乘积得到的值后面的0最少。
 
#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的更多相关文章

  1. Codeforces #2B The least round way(DP)

    Description 有一个n*n的正整数矩阵,要你求一条从第一行第一列的格子到第n行第n列的路,使得你走过的格子里面的数乘起来的值末尾的零的个数最小.输出最小个数. Input 第一行包括1个数n ...

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

  3. codeforces 2B The least round way(DP+数学)

    The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...

  4. CF 2B.The least round way

    题目链接 很久以前就见过此题,以前看了题解,然后今天写了写,写的真搓. #include <cstdio> #include <cstring> #include <st ...

  5. codeforces 2B The least round way 【DP】

    VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...

  6. 最小较小codeforces 2B The least round way

    查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧! 求从左上角到右下角所经过的数字之积末端所含0最小的个数 终究的积可以当作A*2^x*5^y, ...

  7. CF 2B The least round way DP+Math

    题意: 找出一条路, 使每个节点相乘,得到的数末尾 0 最少 每次移动只能向右或者向下, 找到后打印路径 ///按照题目要求,就是找出一条从左上角到右下角中每个数含2 or 5 最少的路 ///可以用 ...

  8. Codeforces 2B The least round way(dp求最小末尾0)

    题目链接:http://codeforces.com/problemset/problem/2/B 题目大意: 给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0 ...

  9. CF dp 题(1500-2000难度)

    前言 从后往前刷 update 新增 \(\text{\color{red}{Mark}}\) 标记功能,有一定难度的题标记为 \(\text{\color{red}{红}}\) 色. 题单 (刷过的 ...

随机推荐

  1. Java基础知识强化之集合框架笔记25:Vector的特有功能

    1. Vector的特有功能: (1)添加功能         public void addElement(Object obj)       -- add() (2)获取功能         pu ...

  2. 9.29noip模拟试题

    环上的游戏(cycle) 有一个取数的游戏.初始时,给出一个环,环上的每条边上都有一个非负整数.这些整数中至少有一个0.然后,将一枚硬币放在环上的一个节点上.两个玩家就是以这个放硬币的节点为起点开始这 ...

  3. JAVA 安装与配置

    JDK是整个java的核心,包括java的运行环境.java工具和java基础类库. 一.安装JDK 获得JDK,登录oracle网站http://www.oracle.com/technetwork ...

  4. Stream流的读取使用

    这个是在现在的项目里,用到的知识点,一般用不到这些..所以想着还是记下来以后用. 针对文本流 //StreamReader sr = new StreamReader(mystream,Encodin ...

  5. 如何配置visual studio 2013进行负载测试-万事开头难

    声明:工作比较忙,文章写得不好,有时间再整理. 起因:最近众包平台因迁移到azure之后一直有网站慢的情况,让老板挨批了,但是测试环境一切正常,而且生产环境也没发现有卡顿和慢的情况,所以干脆来一次负载 ...

  6. 02.[WPF]如何固定窗口的大小

    在WPF开发过程中碰到一个需求,要求保证窗口大小不变,即便是双击 titlebar 也不能改变窗口大小和位置.要实现这样的效果,需要执行如下步骤: 1,分别设置窗口的 Width/MaxWidth/M ...

  7. 设置tabbar的角标与第三方库Masonry的基本使用

    // 设置tabbar的角标 [[[[[self tabBarController] viewControllers] objectAtIndex: 0] tabBarItem] setBadgeVa ...

  8. IOS DLNA PlatinumKit库的使用

    前段时间进行了IOS DLNA的开发,使用的是PlatinumKit库.网上查了很多资料都未果,经过自己的摸索,遂将如何使用PlatinumKit进行DLNA的开发分享给大家. 1.PlatinumK ...

  9. POJ 2942.Knights of the Round Table (双连通)

    简要题解: 意在判断哪些点在一个图的  奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...

  10. CSS负边距自适应布局三例

    单列定宽单列自适应布局: <!DOCTYPE HTML> <html> <head> <meta charset=”UTF-8″> <title& ...