CodeForces B. The least round way(dp)
题目链接:http://codeforces.com/problemset/problem/2/B
5 seconds
64 megabytes
standard input
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.
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).
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
3
1 2 3
4 5 6
7 8 9
0
DDRR
题意:
从左上到右下!
选出一条能让走过的路径的数字的积中含有最少的零!
PS:
积要含有零仅仅能是2 或者5形成。寻找一条含有2或者5 最少的路径就可以!
注意推断矩阵中是否有零的情况!
代码例如以下:
//http://blog.csdn.net/azheng51714/article/details/8240390
#include <cstdio>
#include <cstring>
const int maxn = 1017;
int m[2][maxn][maxn];
int dp[2][maxn][maxn];
//dp[0][i][j]到i、j时有多少个2;
//dp[1][i][j]到i、j时有多少个5。
int vis[2][maxn][maxn];
int n; int solve(int mark)
{
vis[mark][1][1] = 0;
dp[mark][1][1] = m[mark][1][1];
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(i==1 && j==1)
continue;
if(i == 1)
{
dp[mark][i][j] = dp[mark][i][j-1] + m[mark][i][j];
vis[mark][i][j] = 1;//向右
}
else if(j == 1)
{
dp[mark][i][j] = dp[mark][i-1][j] + m[mark][i][j];
vis[mark][i][j] = 0;//向下
}
else
{
int tt1 = dp[mark][i-1][j];
int tt2 = dp[mark][i][j-1];
if(tt1 < tt2)
{
dp[mark][i][j] = tt1 + m[mark][i][j];
vis[mark][i][j] = 0;
}
else
{
dp[mark][i][j] = tt2 + m[mark][i][j];
vis[mark][i][j] = 1;
}
}
}
}
return dp[mark][n][n];
} void print(int mark, int x, int y)
{
if(x==1 && y==1)
return ;
if(vis[mark][x][y] == 0)
{
print(mark,x-1,y);
printf("D");
}
else
{
print(mark,x,y-1);
printf("R");
}
} int main()
{
int x, y;
while(~scanf("%d",&n))
{
int tt, t1, t2;
memset(vis,0,sizeof(vis));
memset(m,0,sizeof(m));
int flag = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d",&tt);
if(tt == 0)
{
flag = 1;
x = i;
y = j;
continue;
}
t1 = t2 = tt;
while(t1%2 == 0)
{
t1/=2;
m[0][i][j]++;
}
while(t2%5 == 0)
{
t2/=5;
m[1][i][j]++;
}
}
} int ans1 = solve(0);//路径存在最少的2的个数
int ans2 = solve(1);//路径存在最少的5的个数
//printf("ans1:%d ans2:%d\n",ans1,ans2);
int mark = 0;
int ans = 0;
if(ans1 < ans2)
{
ans = ans1;
mark = 0;
}
else
{
ans = ans2;
mark = 1;
}
if(flag && ans > 1)
{
printf("1\n");//有零存在那么终于结果就仅仅有一个零
for(int i = 2; i <= x; i++)
{
//向下到有零的那一行
printf("D");
}
for(int j = 2; j <= n; j++)
{
//走到最右边
printf("R");
}
for(int i = x+1; i <= n; i++)
{
//走到最下边
printf("D");
}
printf("\n");
continue;
}
printf("%d\n",ans);
print(mark, n, n);
printf("\n");
}
return 0;
} /*
3
4 10 5
10 9 4
6 5 3
3
4 10 5
6 0 2
7 8 9
*/
CodeForces B. The least round way(dp)的更多相关文章
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Codeforces 837D - Round Subset(dp)
837D - Round Subset 思路:dp.0是由2*5产生的. ①dp[i][j]表示选i个数,因子2的个数为j时因子5的个数. 状态转移方程:dp[i][j]=max(dp[i][j],d ...
- Codeforces 2B The least round way(dp求最小末尾0)
题目链接:http://codeforces.com/problemset/problem/2/B 题目大意: 给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0 ...
- 【Codeforces】CF 2 B The least round way(dp)
题目 传送门:QWQ 分析 求结尾0的数量QwQ. 10只能是$ 2 \times 5 $,我们预处理出每个数因子中2和5的数量. 我们接着dp出从左上到右下的经过的最少的2的数量和最少的5的数量.两 ...
- codeforces 425C Sereja and Two Sequences(DP)
题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...
- Codeforces 629C Famil Door and Brackets(DP)
题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...
- codeforces #267 C George and Job(DP)
职务地址:http://codeforces.com/contest/467/problem/C 太弱了..这题当时都没做出来..思路是有的,可是自己出的几组数组总是过不去..今天又又一次写了一遍.才 ...
- Codeforces 403D: Beautiful Pairs of Numbers(DP)
题意:转换模型之后,就是1~n个数中选k个,放到一个容量为n的背包中,这个背包还特别神奇,相同的物品摆放的位置不同时,算不同的放法(想象背包空间就是一个长度为n的数组,然后容量为1的物体放一个格子,容 ...
- codeforces 459 E. Pashmak and Graph(dp)
题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...
随机推荐
- 洛谷P3834 【模板】可持久化线段树 1 主席树
Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 2000000 ...
- Django综合基础知识
Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...
- python 比较数字大小按从大到小输出
主要用到的python 的知识点 1: 内置函数max 2: 列表的操作 3: while 循环 4 : 错误处理 代码如下: #!/usr/bin/python #coding=u ...
- [Typescript] Promise based delay function using async / await
Learn how to write a promise based delay function and then use it in async await to see how much it ...
- android mvp高速开发框架介绍(dileber使用之小工具使用)
android mvp框架:dileber(https://github.com/dileber/dileber.git) 继续为大家介绍android mvp开源框架 dileber 官方交流qq群 ...
- OpenCASCADE直线与平面求交
OpenCASCADE直线与平面求交 在<解析几何>相关的书中都给出了直线和平面的一般方程和参数方程.其中直线的一般方程有点向式形式的. 由于过空间一点可作且只能作一条直线平行于已知直线, ...
- BZOJ 3280 费用流
思路: 同BZOJ 1221 //By SiriusRen #include <queue> #include <cstdio> #include <cstring> ...
- CSS的flex布局和Grid布局
一.什么是 flex 布局 2009年,W3C 提出了一种新的方案----Flex 布局,可以简便.完整.响应式地实现各种页面布局.目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这 ...
- Linux启动用户空间-init初始化进程
- 【Git 二】Windows|Mac 安装 Git
Windows 或 Mac 上安装 Git 相对于 Linux 上安装来说步骤是简便一些的.Linux 安装步骤见:[Git 一]Linux安装Git 一.Windows 安装 Git 直接下载对应 ...