Description

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

Input

第一行包括1个数n。

接下来n行每行n个数字。

Output

一个数字表示末尾零最小个数。

Sample Input

3
1 2 3
4 5 6
7 8 9

Sample Output

0

因为都是正数,对这个来说仅仅需统计最少的2或5就可以。相对简单。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF=0x3f3f3f3f;
const int maxn=1010;
int dp[maxn][maxn][2];
int path[maxn][maxn][2];
int mp[maxn][maxn][2];
int n;
void print(int i,int j)
{
if(i==1&&j==1)
return ;
print(path[i][j][0],path[i][j][1]);
if(i-path[i][j][0]==1&&j==path[i][j][1]) printf("%c",'D');
else printf("%c",'R');
}
int main()
{
int x,cnt1,cnt2,temp;
while(~scanf("%d",&n))
{
REPF(i,1,n)
{
REPF(j,1,n)
{
scanf("%d",&x);
cnt1=cnt2=0;temp=x;
while(temp%2==0)
{
temp/=2;
cnt1++;
}
while(x%5==0)
{
x/=5;
cnt2++;
}
mp[i][j][0]=cnt1;
mp[i][j][1]=cnt2;
}
}
CLEAR(dp,INF);
dp[1][1][0]=mp[1][1][0];
dp[1][1][1]=mp[1][1][1];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=0;k<2;k++)//0:2 1:5
{
if(i>1&&dp[i][j][k]>dp[i-1][j][k]+mp[i][j][k])
{
dp[i][j][k]=dp[i-1][j][k]+mp[i][j][k];
path[i][j][0]=i-1;path[i][j][1]=j;
}
if(j>1&&dp[i][j][k]>dp[i][j-1][k]+mp[i][j][k])
{
dp[i][j][k]=dp[i][j-1][k]+mp[i][j][k];
path[i][j][0]=i;path[i][j][1]=j-1;
}
}
}
}
printf("%d\n",min(dp[n][n][0],dp[n][n][1]));
// print(n,n);
// puts("");
}
return 0;
}
/*

再看Codeforces 2B:

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 exceeding109).

Output

In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.

Sample Input

Input
3
1 2 3
4 5 6
7 8 9
Output
0
DDRR

Source

不仅要输出路径。并且矩阵中还带了0,这就是麻烦的地方。

题解:对于要输出的路径。记录前面的一个状态就可以。对于0的处理,假设到终点的2或

5的个数大于等于1了,而矩阵中含0。这时候就是直接答案就是1个0。路径仅仅需找到随意

一个0所在的行列输出就可以。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF=0x3f3f3f3f;
const int maxn=1010;
int dp[maxn][maxn][2];
int path1[maxn][maxn][2];
int path2[maxn][maxn][2];
int mp[maxn][maxn][2];
int n;
void print1(int i,int j)
{
if(i==1&&j==1)
return ;
print1(path1[i][j][0],path1[i][j][1]);
if(i-path1[i][j][0]==1&&j==path1[i][j][1]) printf("%c",'D');
else printf("%c",'R');
}
void print2(int i,int j)
{
if(i==1&&j==1)
return ;
print2(path2[i][j][0],path2[i][j][1]);
if(i-path2[i][j][0]==1&&j==path2[i][j][1]) printf("%c",'D');
else printf("%c",'R');
}
int main()
{
int x,cnt1,cnt2,temp,ans;
int sx,sy;
while(~scanf("%d",&n))
{
int flag=1;
CLEAR(mp,0);
REPF(i,1,n)
{
REPF(j,1,n)
{
scanf("%d",&x);
cnt1=cnt2=0;temp=x;
if(x==0)
{
mp[i][j][0]=mp[i][j][1]=1;
sx=i;sy=j;flag=0;continue;
}
while(temp%2==0)
{
temp/=2;
cnt1++;
}
while(x%5==0)
{
x/=5;
cnt2++;
}
mp[i][j][0]=cnt1;
mp[i][j][1]=cnt2;
}
}
CLEAR(dp,INF);
dp[1][1][0]=mp[1][1][0];
dp[1][1][1]=mp[1][1][1];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=0;k<2;k++)//0:2 1:5
{
if(i>1&&dp[i][j][k]>dp[i-1][j][k]+mp[i][j][k])
{
dp[i][j][k]=dp[i-1][j][k]+mp[i][j][k];
if(!k)
{
path1[i][j][0]=i-1;
path1[i][j][1]=j;
}
else
{
path2[i][j][0]=i-1;
path2[i][j][1]=j;
}
}
if(j>1&&dp[i][j][k]>dp[i][j-1][k]+mp[i][j][k])
{
dp[i][j][k]=dp[i][j-1][k]+mp[i][j][k];
if(!k)
{
path1[i][j][0]=i;
path1[i][j][1]=j-1;
}
else
{
path2[i][j][0]=i;
path2[i][j][1]=j-1;
}
}
}
}
}
ans=min(dp[n][n][0],dp[n][n][1]);
if(ans>=1&&!flag)
{
printf("%d\n",1);
for(int i=0;i<sx-1;i++)
printf("%c",'D');
for(int i=0;i<sy-1;i++)
printf("%c",'R');
for(int i=sx;i<n;i++)
printf("%c",'D');
for(int i=sy;i<n;i++)
printf("%c",'R');
puts("");
continue;
}
printf("%d\n",ans);
if(ans==dp[n][n][0]) print1(n,n);
else print2(n,n);
puts("");
}
return 0;
}
/*
3
2 2 2
2 2 2
5 5 5
*/

Codeforces #2B The least round way(DP)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  7. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  8. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  9. [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)

    [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...

随机推荐

  1. 【PostgreSQL-9.6.3】物化视图

    PostgreSQL 9.3 以后出现了物化视图.物化视图是由实实在在的数据组成,这是和一般视图的根本区别. 1. 物化视图创建语法如下: --创建语法 CREATE MATERIALIZED VIE ...

  2. 【笔记JS/HTML/CSS】web中的HTTP协议(1)

    最近都在coursera刷课,加上自己课业也忙起来了,总是忘记写学习笔记ORZ 自省ing... 在写HTML的时候,form表单需要通过HTTP协议向服务器提交.查询数据(如下图) 客户端通过HTT ...

  3. 文件和打印共享资源(IP地址)处于联机状态,但未对连接尝试做出响应。

    文件和打印共享资源(IP地址)处于联机状态,但未对连接尝试做出响应. 检测到 远程计算机不接受端口 445 上的连接,这可能是由于防火墙或安全策略设置,或因为服务可能暂时不可用.Windows 在计算 ...

  4. Introduction of Version Control/Git, SVN

    什么是版本控制? 你可以把一个版本控制系统(缩写VCS)理解为一个“数据库”,在需要的时候,它可以帮你完整地保存一个项目的快照.当你需要查看一个之前的快照(称之为“版本”)时,版本控制系统可以显示出当 ...

  5. PHP填坑

    这里记录下最近PHP踩过的坑,很多都是语法性错误 (1)函数结尾忘记加: 例如匿名函数 <?php $show = function($value){ echo $value."你好& ...

  6. ThinkPHP---TP拓展之获取IP信息

    [概论] (1)简述 在所有网站里,特别是用户管理系统,都喜欢记录用户访问的IP地址.对后期的业务开展有很大的意义,可以通过IP地址的记录访问出国内或全球范围内,哪一块用户比较多. 在后期做产品时,可 ...

  7. 【Redis】三、Redis安装及简单示例

    (四)Redis安装及使用   Redis的安装比较简单,仍然和大多数的Apache开源软件一样,只需要下载,解压,配置环境变量即可.具体安装过程参考:菜鸟教程Redis安装.   安装完成后,通过r ...

  8. LeetCode141LinkedListCycle和142LinkedListCycleII

    141题:判断链表是不是存在环! // 不能使用额外的存储空间 public boolean hasCycle(ListNode head) { // 如果存在环的 两个指针用不一样的速度 会相遇 L ...

  9. java基础学习日志--String、StringBuffer方法案例

    package StringDemo; import java.util.Arrays; /* * 常用String.StringBufer类的方法 */ public class Demo1 { p ...

  10. Codeforces 280C - Game on Tree

    传送门:280C - Game on Tree 不知道期望是啥的请自行Baidu或Google,(溜了 题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数 ...