题意:求m的倍数中不包含一些数码的最小倍数数码是多少。比如15 ,不包含0  1 3,答案是45.

BFS过程:用b[]记录可用的数码。设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是所有可用的数码。这样从根到叶子节点这条路径所组成的数表示一个可行的数。

__                 __

剪枝:(A % m ==  B % m)  =>  (AX % m ==  BX % m)   即如果搜索到一个数X, X%m == a (a !=0) , 则以后如果搜索到Y , Y%m == a,则不必继续搜索。 这样一棵树是有尽头的。

所有这棵树的节点最多才m-1个。

这个剪枝在逻辑电路课上突然灵光突现,昨天模拟赛就怎么也想不来了,额,熬夜要变傻子了。 还有碰到两个陷阱,m==0的时候要特判。不能用节点来直接保存答案,long long 不够用,递归输出答案吧。

  1. #include <stdio.h>
  2. #include <string.h>
  3. //#include <queue>
  4. using namespace std;
  5. #define MAXN 100005
  6. int N,m;
  7. bool vis[MAXN];
  8. int pre[MAXN];
  9. int a[];
  10. int b[];
  11. int num[MAXN];
  12. int que[MAXN];
  13. int front,rear;
  14. void print(int x) //递归从前往后输出答案
  15. {
  16. if (pre[x] != - )
  17. print(pre[x]);
  18. printf("%d",num[x]);
  19. return;
  20. }
  21. int main()
  22. {
  23. int i,j,k,x,cas=;
  24. int casb,ans;
  25. while (scanf("%d%d",&N,&m)!=EOF)
  26. {
  27. printf("Case %d: ",++cas);
  28. int x;
  29. int casb=;
  30. memset(a, , sizeof(a));
  31. memset(b, , sizeof(b));
  32. memset(vis, , sizeof(vis));
  33. for (i=; i<m; i++)
  34. {
  35. scanf("%d",&x);
  36. a[x]=;
  37. }
  38. for (i = ; i <= ; i++)
  39. if (a[i] == )
  40. b[casb++]=i;
  41. if (N == ) //特判
  42. {
  43. if (b[] == )
  44. printf("0\n");
  45. else
  46. printf("-1\n");
  47. continue;
  48. }
  49.  
  50. int flag=;
  51. int ans;
  52. front=;rear=;
  53. for (i = ; i < casb; i++) //最高位
  54. if (b[i]!=)
  55. {
  56. que[rear++]=b[i]%N;
  57. vis[b[i]%N]=true;
  58. num[rear-]=b[i];
  59. pre[rear-]=-;
  60. if (b[i] % N == )
  61. {
  62. ans=b[i];
  63. flag=;
  64. break;
  65. }
  66. }
  67. if (flag == )
  68. {
  69. printf("%d\n",ans);
  70. continue;
  71. }
  72. int tmp;
  73. ans=-;
  74. flag=;
  75. while (front != rear) //bfs
  76. {
  77. tmp=que[front];
  78.  
  79. for (i = ; i < casb; i++)
  80. if (!vis[(tmp*+b[i]) % N]) //剪枝
  81. {
  82. que[rear]=(tmp*+b[i])% N;
  83. vis[(tmp*+b[i])% N ]=true;
  84. pre[rear] = front;//保存父节点
  85. num[rear] = b[i]; //保存本节点这个位数的数字
  86. if ((tmp*+b[i]) % N == )
  87. {
  88. print(rear);
  89. printf("\n");
  90. flag=;
  91. break;
  92. }
  93. rear++;
  94. }
  95. if (flag)
  96. break;
  97. front++;
  98. }
  99. if (flag == )
  100. printf("-1\n");
  101.  
  102. }
  103. return ;
  104. }

HDU 4474 Yet Another Multiple Problem BFS的更多相关文章

  1. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  2. HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

    Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K ...

  3. hdu 4474 Yet Another Multiple Problem

    题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...

  4. Yet Another Multiple Problem(bfs好题)

    Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K ( ...

  5. hdu 4474 大整数取模+bfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...

  6. HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

    Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...

  7. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  8. hdu4474 Yet Another Multiple Problem

    Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...

  9. HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】

    Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

随机推荐

  1. vim三种模式

    一般模式 以vi打开一个文件就直接进入一般模式了.一般模式下可以移动光标查看内容,通过ESC回到一般模式. 一般模式下常用的操作: 撤销与重做 命令 说明 u 复原上一个操作 . 小数点 重复上一个操 ...

  2. Self-Attetion

    四.self-attention 1.是什么? attention机制通常用在encode与decode之间,但是self-attention则是输入序列与输出序列相同,寻找序列内部元素的关系即 K= ...

  3. jquery动态生成二维码添加自定义logo

    动态生成二维码中间带logo. jquery.qrcode.js 动态生成二维码api很简单. 引入jquer(版本任意),引入jquery.qrcode.js 不需要中间带logo这样就可以了.带l ...

  4. vue中的父组件及子组件生命周期的执行顺序

    一.没有任何任何显示与隐藏限制条件的情况下: 1.运行的顺序依次是: 父组件created→父组件beforeMounted→子组件created→子组件beforeMounted→子组件mounte ...

  5. CentOS6.8 安装python2.7,pip以及yum

    由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...

  6. javascript 闭包笔记

      先来解释一下闭包: 1.闭包就是函数嵌套函数 2.内部函数可以引用外部函数的参数和变量 3.参数和变量不会被垃圾回收机制所收回( 垃圾回收机制就是用完变量之后就在内存中释放 ) 使用闭包的好处: ...

  7. 清北学堂模拟赛d3t4 a

    分析:很水的一道题,就是用栈来看看是不是匹配就好了,只是最后没有判断栈是否为空而WA了一个点,以后做题要注意了. #include <bits/stdc++.h> using namesp ...

  8. hdu 2527哈夫曼树(二叉树的运用)

    #include<stdio.h> #include<string.h> #define N  100 #define INF  2000000000  int b[N]; c ...

  9. hdu_1065_I Think I Need a Houseboat_201311160023

    I Think I Need a Houseboat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  10. 学习webpack过程并上传到github

    使用工具:sublimeText+node+git 1,一个包的文件结构,生成初始文件 在node 命令行窗口中创建demo_a文件夹 使用命令 npm init 初始化包,生成package.jso ...