题目链接:hihocoder 1241

题意:

n*n的格阵,每个方格内有一个数字.蚂蚁从左上角走到右下角,数字是零的方格不能走,只能向右向下走.蚂蚁走的路径上全部方格的的乘积为s,要使s低位0的个数尽量少.问,最少s的末尾包含几个0.

分析:

10=2*5,所以只要统计蚂蚁路径上2的个数和5的个数,二者之中的较小者即为s末尾0的个数.

假设2的个数为x,5的个数为y.

对于路径(x,y),答案是min(x,y).

"路径(p,q)比路径(x,y)好"的充要条件"min(p,q)<min(x,y)".

最优路径(x,y)中x为最小值或者y为最小值.

这个问题可以进行延伸:将路径上数字的乘积用k进制来表示,使得末尾0的个数尽量少.对k进行因子分解,当k是若干个质数的乘积时,k=p1*p2*p3*p4...,对于每一个pi进行一次动归.当上边的主键等于左边时,就应该比较剩余的值,有点不好整了,还是两个质数之积比较简单.当k=p1^m1*p2^m2......时,又该怎么做呢?

代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<queue>
  5. #include<math.h>
  6. #include<string.h>
  7. #include<stdlib.h>
  8. using namespace std;
  9. typedef long long ll;
  10. typedef unsigned long long ull;
  11. #define re(i,n) ;i<n;i++)
  12. int n;
  13. ;
  14. int a[maxn][maxn];
  15. int two[maxn][maxn], five[maxn][maxn];
  16. int s[maxn][maxn][];
  17. void go(int c[maxn][maxn], int x){
  18. for (int i = 1; i <= n; i++){
  19. for (int j = 1; j <= n; j++){
  20. if (a[i][j] == 0){
  21. c[i][j] = 1e6; continue;
  22. }
  23. int cnt = ;
  24. ; k /= x)cnt++;
  25. c[i][j] = cnt;
  26. }
  27. }
  28. }
  29. void work(int c[maxn][maxn], int cc[maxn][maxn]){
  30. re(i, n + 1)s[0][i][0] = s[0][i][1] = s[i][0][0] = s[i][0][1] = 1e6;
  31. s[0][1][0] = s[0][1][1] = s[1][0][0] = s[1][0][1] = 0;
  32. for (int i = 1; i <= n; i++){
  33. for (int j = 1; j <= n; j++){
  34. if (s[i - 1][j][0] == s[i][j - 1][0]){
  35. s[i][j][0] = c[i][j] + s[i][j - 1][0];
  36. s[i][j][1] = cc[i][j]+min(s[i][j - 1][1], s[i - 1][j][1]);
  37. }
  38. ][j][]< s[i][j - ][]){
  39. s[i][j][0] = s[i - 1][j][0] + c[i][j];
  40. s[i][j][1] = s[i - 1][j][1] + cc[i][j];
  41. }
  42. else{
  43. s[i][j][0] = s[i][j-1][0] + c[i][j];
  44. s[i][j][1] = s[i][j-1][1] + cc[i][j];
  45. }
  46. }
  47. }
  48. }
  49. int main(){
  50. freopen("in.txt", "r", stdin);
  51. cin >> n;
  52. re(i, n)re(j, n)scanf("%d", &a[i + 1][j + 1]);
  53. go(two, 2), go(five, 5);
  54. work(two, five); int p = min(s[n][n][0], s[n][n][1]);
  55. work(five, two); int q = min(s[n][n][0], s[n][n][1]);
  56. cout << min(p, q) << endl;
  57. return 0;
  58. }

hihocoder1241 Best Route in a Grid的更多相关文章

  1. hihocoder 1241:Best Route in a Grid

    #1241 : Best Route in a Grid 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下 ...

  2. #1241 : Best Route in a Grid

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下或向右走,且不能到达值为0的方格,求出一条到达右下角的最佳 ...

  3. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  4. [4]Telerik Grid 简单使用方法

    1.columns <% Html.Telerik().Grid(Model) .Name("Orders") .Columns(columns => { //绑定列名 ...

  5. Oracle Grid control 11g及Active DataGuard 11g安装部署

    Oracle Grid control 11g及Active DataGuard 11g安装部署(一) 原贴 http://blog.csdn.net/lichangzai/article/detai ...

  6. MatterTrack Route Of Network Traffic :: Matter

    Python 1.1 基础 while语句 字符串边缘填充 列出文件夹中的指定文件类型 All Combinations For A List Of Objects Apply Operations ...

  7. ExtJS 4.2 Grid组件的单元格合并

    ExtJS 4.2 Grid组件本身并没有提供单元格合并功能,需要自己实现这个功能. 目录 1. 原理 2. 多列合并 3. 代码与在线演示 1. 原理 1.1 HTML代码分析 首先创建一个Grid ...

  8. WPF中Grid实现网格,表格样式通用类

    /// <summary> /// 给Grid添加边框线 /// </summary> /// <param name="grid"></ ...

  9. Application Request Route实现IIS Server Farms集群负载详解

    序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一样,时不时的汹涌而至,让我们防不胜防.那么后台的高可用,以及服务器的处理能力就要做一个横向扩展的方案,以使后台业务持续的稳定 ...

随机推荐

  1. JQuery中的extend函数

    1.jQuery.fn.extend(object) 扩展 jQuery 元素集来提供新的方法(通常用来制作插件). 例如:增加两个插件方法. jQuery.fn.extend({ check: fu ...

  2. 双核CPU,跑程序会报rcu_sched_state detected stalls on CPUs/tasks 错误

    有一份SDK,之前跑在PPC405EX上没问题。最近换平台,CPU使用了PowerPC的P1020,双核。linux版本也升级到了3.0.48版本。升级之后出现了一个问题:SDK里面的程序跑一段时间之 ...

  3. STM32之USART-RS485

    转载自:http://www.cnblogs.com/itloverhpu/p/3278014.html 1.今天调试HDMI8X8背板和板卡的通信,一直有问题:背板可以和PC正常通信,背板可以发命令 ...

  4. checkbox勾选判断

    var xieYi=document.getElementById("xieYi"); if(!xieYi.checked){ alert("请先阅读并勾选购买协议!&q ...

  5. HDU2191悼念512汶川大地震遇难同胞——珍惜现在,感恩生活[多重背包]

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. openjudge1768 最大子矩阵[二维前缀和or递推|DP]

    总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...

  7. XBOX ONE游戏开发之登陆服务器(一)

    XBOX ONE游戏开发之登陆服务器(一) XBOX LIVE是微软自已的认证服务器, 当我们开发游戏时,如果是联网游戏,需要自已架设单点登陆(SSO)服务器 这个需要微软提供Relying Part ...

  8. excel技巧

    1. 使文字自动适配方格大小. 自动调整行高(但是合并后的方格不行)

  9. [转] 如何设置Eclipse的上网代理

    from: http://blog.csdn.net/qq635785620/article/details/8191799 不同版本的eclipse有不同的设置方法 方式一:   默认的Eclips ...

  10. Mysql占用过高CPU时的优化手段

    Mysql占用CPU过高的时候,该从哪些方面下手进行优化?占用CPU过高,可以做如下考虑:1)一般来讲,排除高并发的因素,还是要找到导致你CPU过高的哪几条在执行的SQL,show processli ...