题目链接:http://codeforces.com/problemset/problem/2/B

题目大意:

给你一个nxn的矩形,找到一条从左上角到右下角的路径,使得该路径上所有数字的乘积的末尾0最少。
解题思路:
我们设k为2的因子数,m为5的因子数,那么一个数的末尾0的个数就是min(k,m)。
我们设dp[i][j][0]为从左上角到点(i,j)的乘积的最少2因子数,dp[i][j][1]为从左上角到点(i,j)的乘积的最少5因子数。
那么ans=min(dp[i][j][0],dp[i][j][1]),如果ans=dp[i][j][0]就按path[i][j][0]输出,若ans=dp[i][j][1]也同理。
注意存在0的情况,若果路径中有一个0那么末尾0为1,若ans>1,则构造一条经过0的路径输出。

代码:

 #include<bits/stdc++.h>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=1e3+;
const int INF=0x3f3f3f3f;
const double eps=1e-; int n;
int mp[N][N],dp[N][N][],path[N][N][]; struct node{
int f,s;
node(int f,int s):f(f),s(s){}
}; bool judge(int x,int y){
if(x>&&x<=n&&y>&&y<=n) return true;
return false;
} void print(int x,int y,int type){
if(path[x][y][type]==-)
return;
if(path[x][y][type]==)
print(x-,y,type);
else
print(x,y-,type);
printf("%c",path[x][y][type]==?'D':'R');
} node cal(int x){
int f=,s=;
while(x){
if(x%==){
f++;
x/=;
}
else break;
}
while(x){
if(x%==){
s++;
x/=;
}
else break;
}
return node(f,s);
} int main(){
scanf("%d",&n);
int idx,idy;
bool flag=false;
memset(path,-,sizeof(path));
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]==){
idx=i;
idy=j;
flag=true;
}
}
}
dp[][][]=dp[][][]=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
node t=cal(mp[i][j]);
if(judge(i-,j)){
dp[i][j][]=dp[i-][j][];
dp[i][j][]=dp[i-][j][];
path[i][j][]=path[i][j][]=;
}
if(judge(i,j-)){
if(dp[i][j][]>dp[i][j-][]){
dp[i][j][]=dp[i][j-][];
path[i][j][]=;
}
if(dp[i][j][]>dp[i][j-][]){
dp[i][j][]=dp[i][j-][];
path[i][j][]=;
}
}
dp[i][j][]+=t.f;
dp[i][j][]+=t.s;
}
}
int ans=min(dp[n][n][],dp[n][n][]);
if(ans>&&flag){
puts("");
for(int i=;i<=idx;i++){
printf("D");
}
for(int j=;j<=n;j++){
printf("R");
}
for(int i=idx+;i<=n;i++){
printf("D");
}
return ;
}
printf("%d\n",ans);
if(ans==dp[n][n][])
print(n,n,);
else
print(n,n,);
return ;
}

Codeforces 2B The least round way(dp求最小末尾0)的更多相关文章

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

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

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

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

  3. Algorithm --> 求阶乘末尾0的个数

    求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...

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

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

  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. cell_phone_network(树形dp求最小支配集)

    Cell Phone Network Farmer John has decided to give each of his cows a cell phone in hopes to encoura ...

  8. LightOJ-1138-Trailing Zeroes (III)-二分+求N!末尾0

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  9. codeforces 789 C. Functions again(dp求区间和最大)

    题目链接:http://codeforces.com/contest/789/problem/C 题意:就是给出一个公式 然后给出一串数求一个区间使得f(l,r)最大. 这题需要一个小小的处理 可以设 ...

随机推荐

  1. [Codeforces Gym] 100162B Circle of Stones

    题意: 桌子上有 n 个石头围成一个环.每个石头都有一种颜色.每种颜色可以由小写英文字母表示.如果每一对相邻的石头都是不同颜色的,则称这 n 个石头构成的环是美丽的.现在,你可以从这 n 个石头中拿走 ...

  2. CMake 案例

    单个源文件 # CMake 最低版本号要求 cmake_minimum_required (VERSION 3.11) # 项目信息 project (Demo) # 指定生成目标 add_execu ...

  3. Java-异常机制详解以及开发时异常设计的原则要求

    Java-异常机制详解以及开发时异常设计的原则要求 http://blog.csdn.net/Jack__Frost/article/details/52760930?locationNum=6

  4. struts的问题

    将SSH框架进行整合的时候,将三者的jar包加入到lib下面,然后测试struts,结果页面显示不出来报404错误,可是路径没有问题 找到罪魁祸首是:原因两个:(1)在未用到spring的时候,先不要 ...

  5. Java入门:绘制简单图形

    在上一节,我们学习了如何使用swing和awt工具创建一个空的窗口,本节学习如何绘制简单图形. 基本绘图介绍 Java中绘制基本图形,可以使用Java类库中的Graphics类,此类位于java.aw ...

  6. centos7 U盘安装卡在 starting dracut initqueue hook解决办法

    U盘安装centos7启动过程中出现: [ok] Reached target Basic System 或者 [ok] starting dracut initqueue hook   到下一行就不 ...

  7. python的内置模块time和datetime的方法详解以及使用(python内的time和datetime时间格式)

    time内置模块的方法 1.time() 时间戳 time() -> floating point number  浮点数 Return the current time in seconds ...

  8. mysql 同步数据到 ElasticSearch 的方案

    MySQL Binlog 要通过 MySQL binlog 将 MySQL 的数据同步给 ES, 我们只能使用 row 模式的 binlog.如果使用 statement 或者 mixed forma ...

  9. servlet拦截器

    servlet拦截未登录的用户请求 java代码: package com.gavin.filter; import java.io.IOException; import javax.servlet ...

  10. memcmp 和 memcpy使用

    #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...