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)最大. 这题需要一个小小的处理 可以设 ...
随机推荐
- std::async
https://www.cnblogs.com/qicosmos/p/3534211.html https://bobjin.com/blog/c_cpp_docs/reference/en/cpp/ ...
- 2018 “百度之星”程序设计大赛 - 初赛(A)度度熊学队列 list rope
c++ list使用 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstr ...
- MySQL修改端口号操作
在C盘下的program Files下找到MySQL文件夹 - my.ini配置文件有个port=3306 修改即可
- 【Asp.net入门05】第一个ASP.NET 应用程序-测试Asp.net程序
测试示例应用程序 本部分内容: ASP.NET应用程序测试方法 web窗体访问过程 Visual Studio工具栏上有一个下拉列表,其中列出了工作站上已安装的浏览器的名称(单击浏览器名称右侧的向下箭 ...
- CentOS7.3 部署Haproxy 1.7.2
haproxy:http://www.haproxy.org/ 本文涉及haproxy的安装,并做简单配置. 一.环境准备 1. 操作系统 CentOS-7-x86_64-Everything-151 ...
- Java基础-IO流对象之打印流(PrintStream与PrintWriter)
Java基础-IO流对象之打印流(PrintStream与PrintWriter) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.打印流的特性 打印对象有两个,即字节打印流(P ...
- java基础-IO流对象之Properties集合
java基础-IO流对象之Properties集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Properties集合的特点 Properties类表示了一个持久的属性集. ...
- Linux入门,这七大习惯得有!
对于很多Linux初学者来说,在刚开始使用Linux系统时会感到很多不适.这里为大家整理了自己以前Linux入门时别人告诉我的七个习惯.我相信如果你运用了这七个习惯,在你使用Linux时你会感觉更安全 ...
- 互斥锁 pthread_mutex_init()函数
Linux下为了多线程同步,通常用到锁的概念.posix下抽象了一个锁类型的结构:ptread_mutex_t.通过对该结构的操作,来判断资源是否可以访问.顾名思义,加锁(lock)后,别人就无法打开 ...
- bzoj千题计划138:bzoj1432: [ZJOI2009]Function
http://www.lydsy.com/JudgeOnline/problem.php?id=1432 http://blog.sina.com.cn/s/blog_86942b1401014bd2 ...