这个2B题还好~~


题目大意:

给出一个矩阵。从左上走到右下,仅仅能往右或下走。路径中每一个格子有一个数。这些数相乘得出一个数。

求这个数末尾零最少的一条路径。


解题思路:


找出一条路径。乘积得数中素因子2的个数最少,再找出一个素因子5最少, 比較两个输出最小的。

有意外情况就是有数为零。这样的情况把零当成10跑一遍,假设素因子最少为0。输出路径,假设不是,输出经过零的路径。



以下是代码:

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm>#define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x)  ((x) > 0 ?

(x) : -(x)) #define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A)))) #define clearall(A, X) memset(A, X, sizeof(A)) #define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE)) #define memcopyall(A, X) memcpy(A , X ,sizeof(X)) #define max( x, y ) ( ((x) > (y)) ? (x) : (y) ) #define min( x, y ) ( ((x) < (y)) ?

(x) : (y) ) using namespace std; int dp[1005][1005][2]; int cnt[1005][1005][2]; int pre[1005][1005][2]; void output(int x,int y,int num) { if(x==0&&y==0)return ; if(pre[x][y][num]==0) { output(x,y-1,num); printf("R"); } else { output(x-1,y,num); printf("D"); } } int main() { int input,n,x=-1,y=-1; scanf("%d",&n); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { scanf("%d",&input); if(input==0) { cnt[i][j][0]=1; cnt[i][j][1]=1; x=i; y=j; continue; } cnt[i][j][0]=0; while(input%2==0) { cnt[i][j][0]++; input/=2; } cnt[i][j][1]=0; while(input%5==0) { cnt[i][j][1]++; input/=5; } } } clearall(pre,-1); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(i==0) { if(j==0) { dp[0][0][0]=cnt[0][0][0]; dp[0][0][1]=cnt[0][0][1]; } else { dp[0][j][0]=cnt[0][j][0]+dp[0][j-1][0]; dp[0][j][1]=cnt[0][j][1]+dp[0][j-1][1]; pre[0][j][0]=0; pre[0][j][1]=0; } } else if(j==0) { dp[i][0][0]=dp[i-1][0][0]+cnt[i][0][0]; dp[i][0][1]=dp[i-1][0][1]+cnt[i][0][1]; pre[i][0][0]=1; pre[i][0][1]=1; } else { if(dp[i][j-1][0]>dp[i-1][j][0]) { dp[i][j][0]=dp[i-1][j][0]+cnt[i][j][0]; pre[i][j][0]=1; } else { dp[i][j][0]=dp[i][j-1][0]+cnt[i][j][0]; pre[i][j][0]=0; } if(dp[i][j-1][1]>dp[i-1][j][1]) { dp[i][j][1]=dp[i-1][j][1]+cnt[i][j][1]; pre[i][j][1]=1; } else { dp[i][j][1]=dp[i][j-1][1]+cnt[i][j][1]; pre[i][j][1]=0; } } } } if(x!=-1) { if(min(dp[n-1][n-1][0],dp[n-1][n-1][1])==0) { printf("0\n"); if(dp[n-1][n-1][0]==0)output(n-1,n-1,0); else output(n-1,n-1,1); } else { printf("1\n"); for(int i=0;i<n-1;i++) { if(i==x) { for(int j=0;j<n-1;j++) { printf("R"); } } printf("D"); } } } else { printf("%d\n",min(dp[n-1][n-1][0],dp[n-1][n-1][1])); if(dp[n-1][n-1][0]<dp[n-1][n-1][1]) { output(n-1,n-1,0); } else output(n-1,n-1,1); } return 0; }

Codeforces Beta Round #2 B. The least round way相关推荐

  1. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  2. Codeforces Beta Round #5 B. Center Alignment 模拟题

    B. Center Alignment 题目连接: http://www.codeforces.com/contest/5/problem/B Description Almost every tex ...

  3. Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新

    http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...

  4. Codeforces Beta Round #51 D. Beautiful numbers 数位dp + 状态优化

    传送门 文章目录 题意: 思路: 题意: 思路: 数位dpdpdp挺经典的一个题辣,有一个很明显的状态就是f[pos][num][lcm]f[pos][num][lcm]f[pos][num][lcm ...

  5. Codeforces Beta Round #22 (Div. 2 Only) E. Scheme(DFS+强连通)

    题目大意 给了 n(2<=n<=105) 个点,从每个点 u 出发连向了一个点 v(共 n 条边) 现在要求添加最少的边使得整个图是一个强连通图 做法分析 这道题千万不要一般化:先求强连通 ...

  6. codeforces beta round 1

    codeforces beta round 1 A Theatre Square in the capital city of Berland has a rectangular shape with ...

  7. Codeforces Beta Round #4 (Div. 2 Only)

    Codeforces Beta Round #4 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...

  8. Codeforces Beta Round #7

    Codeforces Beta Round #7 http://codeforces.com/contest/7 A 水题 1 #include<bits/stdc++.h> 2 usin ...

  9. mysql round不四舍五入_MySQL中ROUND函数进行四舍五入操作陷阱分析

    本文实例讲述了MySQL中ROUND函数进行四舍五入操作陷阱.,具体如下: 在MySQL中, ROUND函数用于对查询结果进行四舍五入,不过最近使用ROUND函数四舍五入时意外发现并没有预期的那样,本 ...

  10. php round函数怎么用,excel round函数怎么用

    excel round函数怎么用? round函数是EXCEL中的一个基本函数,作用按指定的位数对数值进行四舍五入,语法是ROUND(number, num_digits). 语法ROUND(numb ...

最新文章

  1. idea 代码自动补全快捷键
  2. 操作系统:几种页面置换算法
  3. 转:SharePoint站点中用户信息与AD用户信息的“不一致”问题
  4. Mysql数据库的基本概念
  5. python3  循环输出当前时间。
  6. 光驱怎么挂载第二个光驱_电脑光驱经常自己打开自己关闭,怎么回事
  7. expandablelistview 折叠动画_这个机械手到底有几个自由度,31个机械原理、设计动画来了。。。...
  8. 业务异常 java_java – 具有业务异常的Hystrix断路器
  9. Python之Django之views中视图代码重复查询的优化
  10. 自动驾驶系统的学习笔记
  11. ios URLSchemes
  12. ubuntu防火墙关闭命令-转
  13. wamp3.1.0下载地址
  14. Go基础系列:接口类型断言和type-switch
  15. 【优化算法】动态粒子群算法的动态环境寻优算法【含Matlab源码 1125期】
  16. linux socket版本 can,linux socket can程序cantool
  17. lammps教程:restart重启计算命令用法详解
  18. 派森诺细菌完成图+转录组+蛋白组+实验端验证高分文章
  19. 搜pos不为了收单而收单,互联网是个神奇的动物
  20. 让cocos2d-x 3.0读取cocostudio中的csb文件

热门文章

  1. 锋利的jQuery-4--动画方法总结简表
  2. 地理信息系统控件GIS控件TatukGIS Developer Kernel 下载及介绍
  3. 重新定位开放策略 保卫中国经济版图
  4. (筆記) 如何安裝Altera USB Blaster? (SOC) (Quartus II) (DE2)
  5. [python] 3 、基于串口通信的嵌入式设备上位机自动测试程序框架(简陋框架)...
  6. 如何在一个领域内成为顶尖人才?
  7. 最最基本的Git入门 -- 本地仓库操作
  8. Spring JDBC详解
  9. PowerShell学习笔记(三)
  10. Jquery 常用方法经典总结