Codeforces 2B The least round way(dp求最小末尾0)
题目链接: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)的更多相关文章
- Codeforces #2B The least round way(DP)
Description 有一个n*n的正整数矩阵,要你求一条从第一行第一列的格子到第n行第n列的路,使得你走过的格子里面的数乘起来的值末尾的零的个数最小.输出最小个数. Input 第一行包括1个数n ...
- codeforces 2B The least round way(DP+数学)
The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...
- Algorithm --> 求阶乘末尾0的个数
求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...
- codeforces 2B The least round way 【DP】
VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...
- 最小较小codeforces 2B The least round way
查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧! 求从左上角到右下角所经过的数字之积末端所含0最小的个数 终究的积可以当作A*2^x*5^y, ...
- CF 2B The least round way DP+Math
题意: 找出一条路, 使每个节点相乘,得到的数末尾 0 最少 每次移动只能向右或者向下, 找到后打印路径 ///按照题目要求,就是找出一条从左上角到右下角中每个数含2 or 5 最少的路 ///可以用 ...
- cell_phone_network(树形dp求最小支配集)
Cell Phone Network Farmer John has decided to give each of his cows a cell phone in hopes to encoura ...
- 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 ...
- codeforces 789 C. Functions again(dp求区间和最大)
题目链接:http://codeforces.com/contest/789/problem/C 题意:就是给出一个公式 然后给出一串数求一个区间使得f(l,r)最大. 这题需要一个小小的处理 可以设 ...
随机推荐
- 解题:AHOI2017/HNOI2017 礼物
题面 先不管旋转操作,只考虑增加亮度这个操作.显然这个玩意的影响是相对于$x,y$固定的,所以可以枚举增加的亮度然后O(1)算出来.为了方便我们把这个操作换种方法表示,只让一个手环改变$[-m,m]$ ...
- postgresql 数据库无法启动
在数据库无法启动时,一般可以根据报错信息,采取对应措施即可,下面列出一些在数据库启动时报出错误比较严重而解决方式又不那么明显的处理方法. 模拟错误,查到pg_class系统表中一个索引在磁盘中的位置, ...
- noip2017考前整理(未完)
快考试了,把我以前写过的题回顾一下.Noip2007 树网的核:floyd,推出性质,暴力.Noip2008 笨小猴:模拟Noip2008 火柴棒等式:枚举Noip2008 传纸条:棋盘dpNoip2 ...
- c动态分配结构体二维数组
这个问题我纠结了蛮久了,因为前面一直忙(自己也懒了点),所以没有能好好研究这个.希望这篇文章能够帮助你们. #include <stdio.h> #include <stdlib.h ...
- weUI框架在github下载地址
1.公众号样式UI库的下载地址: https://github.com/Tencent/weui 2.微信小程序UI库的下载地址:https://github.com/Tencent/weui-wxs ...
- memcmp 和 memcpy使用
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...
- [Node.js] querystring类
和参数相关的帮助类,原生自带,直接 require('querystring') 即可使用. 此类一共包括4个方法: querystring.stringify(obj, [sep], [eq]) q ...
- .Net多线程之线程安全
ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单线程版本( ...
- JS对象中的原型
对象的原型:每个对象都连接一个原型对象,并且它可以从中继承属性.所有通过对象字面量创建的对象都连接到object.prototype.当你创建一个新对象时,你可以选择某个对象作为它的原型.原型连接在更 ...
- Javascript技术之详尽解析event对象
描述event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等.event对象只在事件发生的过程中才有效.event的某些属性只对特定的事件有意义.比如,fromEleme ...