题目链接: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. 使用 swoole 加速你的 laravel

    在此前的另外一篇文章讨论过 opcache:php 性能优化之opcache - 让你的php性能提升 50% 再来复习一下吧,导致 php 慢的各种因素中解析性语言的特性可以说是罪魁祸首,再加上,每 ...

  2. 使用traefik作为kubernetes的ingress

    目录 说明 部署 创建一个独立的命名空间 配置rbac授权 配置secret 创建一个configmap用于存放traefik的配置文件 配置traefik的deployment文件 配置服务 通过p ...

  3. Go_19: Golang 中错误与异常需要重新认识

    如何进行错误处理,这是一个Go程序员之间,特别是一些新的Go程序员,会经常讨论的问题.讨论到最后往往由于以下代码的多次出现而变成了抱怨. if err != nil { return err } 我们 ...

  4. linux下安装shellinabox实现web登录服务器

    GitHub地址(含有文件下载和详细安装流程):https://github.com/shellinabox/shellinabox 这里我们使用的是redhat安装方法如下: 1.配置安装依赖环境 ...

  5. linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime

    time()提供了秒级的精确度 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在的秒 ...

  6. H5页面中唤起native app

    现在各类app,分享出去的H5页面中,一般都会带着一个立即打开的按钮,如果本地安装了app,那么就直接唤起本地的app,如果没有安装,则跳转到下载.这是一个很正常的推广和导流量的策略,最近产品经理就提 ...

  7. 八卦Minsky打压神经网络始末

    八卦Minsky打压神经网络始末 谈下Minsky造成的神经网络冰河事件:57年一个叫弗兰克的大概只有二流水平的学者搞出了感知机,理论和实践证明了对线性可分问题的有效性,引起一阵轰动,特别是非科学圈类 ...

  8. 张鑫旭:Promise异步编程模式

    参考文章: http://www.zhangxinxu.com/wordpress/2014/02/es6-javascript-promise-%E6%84%9F%E6%80%A7%E8%AE%A4 ...

  9. 【leetcode 简单】 第五十八题 计数质数

    统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . class Solution: def cou ...

  10. 用threading和Queue模块实现多线程的端口扫描器

    一.Queue模块基础 q = Queue.Queue()    q.qsize()           返回队列的大小  q.empty()         如果队列为空,返回True,反之Fals ...