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个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...
随机推荐
- SpringCloud学习笔记(9)----Spring Cloud Netflix之声明式 REST客户端 -Feign的使用
1. 什么是Feign? Feign是一种声明式.模板化的HTTP客户端,在SpringCloud中使用Feign.可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到 ...
- hibernate N+1
http://www.cnblogs.com/sy270321/p/4769198.html
- redis做成windows服务
打开cmd切换到redis根目录 执行安装命令 redis-server.exe --service-install redis.windows.conf --loglevel verbose 卸载 ...
- Hibernate简单的保存操作
1.这里面我想先说一下session对象的创建,这个是我们操纵数据库的核心对象,因此首先我们应该获取相应的session对象. public static Configuration cfg; pub ...
- OpenResty.spec
Name: openresty Version: 1.13.6.1 Release: 2%{?dist} Summary: OpenResty, scalable web platform by ex ...
- 2016 10 27 考试 dp 向量 乱搞
目录 20161027考试 T1: T2: T3: 20161027考试 考试时间 7:50 AM to 11:15 AM 题目 考试包 据说这是一套比较正常的考卷,,,嗯,,或许吧, 而且,,整个小 ...
- 2.Maven特点,Maven约定,建立第一个Maven项目
1 Maven是跨平台的项目管理工具.主要服务于基于java平台的项目构建,依赖管理和项目信息管理. 项目构建 清理à编译à測试à报告à打包à部署 理想的项目构建: 高度自己主动化 跨平台 可重 ...
- cocos2d_x_01_环境搭建
终于效果图: Cocos2d-x-3.3 Mac 安装 下载地址: 參考文档: 在线API列表: Cocos2d-x-3.3 版本号 从配置安装到创建项目都是命令行 官网下载最新版本号Cocos2 ...
- 【通信框架】Google的开源通信框架protobuf概述
在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com QQ:1494713801 一.作用 protobuf(Protocol Buffers)是Google内部 ...
- ConfigurationSection
https://msdn.microsoft.com/en-us/library/system.configuration.configurationsection(v=vs.110).aspx Re ...