题意: 找出一条路, 使每个节点相乘,得到的数末尾 0 最少

每次移动只能向右或者向下, 找到后打印路径

///按照题目要求,就是找出一条从左上角到右下角中每个数含2 or 5 最少的路
///可以用Dp的思想, 然后把每个节点该走的方向记下来
///再从终点回溯,把路径存入栈,再输出
///数据会有0的情况, 这时候我们应该记录离终点最近的0
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = + ;
LL Map[maxn][maxn];
LL Dp[maxn][maxn][];
char Step[maxn][maxn][];
stack<char> Mesure; int Factor(int Num, int Base) ///得到因子 2 和 5 的个数
{
if(Num == ) return ;
int ret = ;
while(Num % Base == )
{
ret++;
Num /= Base;
}
return ret;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin >> n;
bool Zero = false;
int Zero_Pos;
for(int i = ; i < n; ++i)
{
for(int j = ; j < n; ++j)
{
cin >> Map[i][j];
if(Map[i][j] == )
{
Zero = true;
Zero_Pos = i;
}
Dp[i][j][] = Factor(Map[i][j],);
Dp[i][j][] = Factor(Map[i][j],);
} } for(int i = ; i < n; ++i)
{
for(int j = ; j < n; ++j)
{
for(int k = ; k < ; ++k)
{
if(i == && j == ) continue;
if(i == )
{
Dp[i][j][k] += Dp[i][j-][k];
Step[i][j][k] = 'R';
}
else if(j == )
{
Dp[i][j][k] += Dp[i-][j][k];
Step[i][j][k] = 'D';
}
else
{
Dp[i][j][k] += min(Dp[i-][j][k],Dp[i][j-][k]);
Step[i][j][k] = Dp[i-][j][k] < Dp[i][j-][k] ? 'D' : 'R';
}
}
}
} if(min(Dp[n-][n-][],Dp[n-][n-][]) > && Zero)
{
printf("1\n");
for(int i = ; i < Zero_Pos; ++i) putchar('D');
for(int i = ; i < n-; ++i) putchar('R');
for(int i = Zero_Pos; i < n-; ++i) putchar('D');
//
}
else
{
printf("%d\n",min(Dp[n-][n-][],Dp[n-][n-][]));
int k = ;
k = Dp[n-][n-][] < Dp[n-][n-][] ? : ;
for(int i = n-, j = n-; i != || j != ; )
{
Mesure.push(Step[i][j][k]);
if(Step[i][j][k] == 'D') i--;
else j--;
}
while( !Mesure.empty() ) putchar(Mesure.top()), Mesure.pop();
}
puts("");
return ;
}

CF 2B The least round way DP+Math的更多相关文章

  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. CF 2B.The least round way

    题目链接 很久以前就见过此题,以前看了题解,然后今天写了写,写的真搓. #include <cstdio> #include <cstring> #include <st ...

  4. [cf contest 893(edu round 33)] F - Subtree Minimum Query

    [cf contest 893(edu round 33)] F - Subtree Minimum Query time limit per test 6 seconds memory limit ...

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

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

  6. CF 375C Circling Round Treasures [DP(spfa) 状压 射线法]

    C - Circling Round Treasures 题意: 在一个$n*m$的地图上,有一些障碍,还有a个宝箱和b个炸弹.你从(sx,sy)出发,走四连通的格子.你需要走一条闭合的路径,可以自交 ...

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

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

  8. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  9. CF 148D. Bag of mice (可能性DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. 31.【微服务架构】SpringCloud之Feign(五)

    Feign简介 Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Fei ...

  2. 使用 CROSS APPLY 与 OUTER APPLY 连接查询

    Ø  前言 日常开发中遇到多表查询时,首先会想到 INNER JOIN 或 LEFT OUTER JOIN 等等,但是这两种查询有时候不能满足需求.比如,左表一条关联右表多条记录时,我需要控制右表的某 ...

  3. ROI Pooling层详解

    目标检测typical architecture 通常可以分为两个阶段: (1)region proposal:给定一张输入image找出objects可能存在的所有位置.这一阶段的输出应该是一系列o ...

  4. sum() over (order by )

    sum(x) over( partition by y ORDER BY z ) 分析 sum(x) over (partition by y order by z) 求安照y分区,然后按z排序,连续 ...

  5. 第27月第12天 webrtc ios openssl boost

    1. source 'https://github.com/CocoaPods/Specs.git' target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pla ...

  6. HashMap的源码,实现原理,底层结构

    转载一遍不错的文章,让你深入了解HashMap http://www.cnblogs.com/ITtangtang/p/3948406.html

  7. Fragment处理接口回调,网络请求数据

    03-06 19:57:46.138 8691-8691/com.retech.myapplication E/glz: onAttach03-06 19:57:46.138 8691-8691/co ...

  8. 使用Retrofit时常用到的注解

  9. 核心编程9 文件和文件的输入输出 (os模块)

    1  python内建函数open和file 文件打开方便读取:f = open('文件名','模式','缓冲模式')         #'r'读取,'w'写入(先清空后创建).'a'追加 详情文件模 ...

  10. 贝叶斯优化(Bayesian Optimization)深入理解

    目前在研究Automated Machine Learning,其中有一个子领域是实现网络超参数自动化搜索,而常见的搜索方法有Grid Search.Random Search以及贝叶斯优化搜索.前两 ...