一道比较不错的BFS+DP题目

题意很简单,就是问一个刚好包含m(m<=10)个不同数字的n的最小倍数。

很明显如果直接枚举每一位是什么这样的话显然复杂度是没有上限的,所以需要找到一个状态表示方法:

令F[i][j] 表示已经用了 i (二进制压位表示)用了 i 这些数字,且余数j为的状态,枚举时直接枚举当前位,那么答案明显就是F[m][0]

我这里将状态i, j存在了一维空间里,即 i * 1000 + j表示,实际上用一个结构体存队列里的点,用二维数组标记状态也是可行的。

  1. #include <map>
  2. #include <set>
  3. #include <stack>
  4. #include <queue>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <vector>
  8. #include <cstdio>
  9. #include <cctype>
  10. #include <cstring>
  11. #include <cstdlib>
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f
  16. #define inf (-((LL)1<<40))
  17. #define lson k<<1, L, mid
  18. #define rson k<<1|1, mid+1, R
  19. #define mem0(a) memset(a,0,sizeof(a))
  20. #define mem1(a) memset(a,-1,sizeof(a))
  21. #define mem(a, b) memset(a, b, sizeof(a))
  22. #define FIN freopen("in.txt", "r", stdin)
  23. #define FOUT freopen("out.txt", "w", stdout)
  24. #define rep(i, a, b) for(int i = a; i <= b; i ++)
  25.  
  26. template<class T> T CMP_MIN(T a, T b) { return a < b; }
  27. template<class T> T CMP_MAX(T a, T b) { return a > b; }
  28. template<class T> T MAX(T a, T b) { return a > b ? a : b; }
  29. template<class T> T MIN(T a, T b) { return a < b ? a : b; }
  30. template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
  31. template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }
  32.  
  33. //typedef __int64 LL;
  34. typedef long long LL;
  35. const int MAXN = ;
  36. const int MAXM = ;
  37. const double eps = 1e-;
  38. //LL MOD = 987654321;
  39.  
  40. int t, n, m, x;
  41. struct Node {
  42. bool vis;
  43. char num;
  44. int pre, cnt;
  45. }s[(<<) * ];
  46. char ans[], d[];
  47.  
  48. int bfs()
  49. {
  50. queue<int>q;
  51. q.push();
  52. while(!q.empty()) {
  53. int head = q.front();q.pop();
  54. rep (i, , ) {
  55. if(head == && i == ) continue;
  56. int mod = (head % * + i) % n;
  57. int tail = ((head / ) | ( << i)) * + mod;
  58. if(s[tail].vis)
  59. continue;
  60. s[tail].vis = true;
  61. s[tail].num = i + '';
  62. s[tail].pre = head;
  63. s[tail].cnt = s[head].cnt + ((head / ) & ( << i) ? : );
  64. if(s[tail].cnt == m && mod == ) {
  65. return tail;
  66. }
  67. if(s[tail].cnt <= m) q.push(tail);
  68. }
  69. }
  70. return ;
  71. }
  72.  
  73. //calc a / b
  74. char* divide(char *a, int len, int b) {
  75. mem0(d);
  76. int i = , cur = , l = ;
  77. while(cur < b && i < len) {
  78. cur = cur * + a[i++] - '';
  79. }
  80. d[l++] = cur / b + '';
  81. while(i < len) {
  82. cur = cur % b * + a[i++] - '';
  83. d[l++] = cur / b + '';
  84. }
  85. return d;
  86. }
  87.  
  88. void print(int ed) {
  89. int len = ;
  90. mem0(ans);
  91. while(ed) {
  92. ans[len++] = s[ed].num;
  93. ed = s[ed].pre;
  94. }
  95. reverse(ans, ans + len);
  96. printf("%s=%d*%s\n", ans, n, divide(ans, len, n));
  97. }
  98.  
  99. int main()
  100. {
  101. //FIN;
  102. while(cin >> t) while(t--) {
  103. cin >> n >> m;
  104. mem0(s);
  105. if( !(x = bfs()) ) {
  106. puts("Impossible");
  107. }
  108. else {
  109. print(x);
  110. }
  111. }
  112. return ;
  113. }

ZOJ 3596Digit Number(BFS+DP)的更多相关文章

  1. HDU 3565 Bi-peak Number(数位DP)题解

    题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ...

  2. 【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)

    \(IDA^*\) 说实话,这道题我一开始没想出正解,于是写了一个\(IDA^*\)... 但神奇的是,这个\(IDA^*\)居然连字符串长度分别为\(2500,4000\)的数据都跑得飞快,不过数据 ...

  3. codeforces 295C Greg and Friends(BFS+DP)

    One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...

  4. 【HDU 3709】 Balanced Number (数位DP)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  5. HDU 5898:odd-even number(数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5898 题意:给出一个区间[l, r],问其中数位中连续的奇数长度为偶数并且连续的偶数长度为奇数的个数.(1< ...

  6. [HDOJ3709]Balanced Number(数位dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:求区间[L,R]内每一个数中是否存在一位,使得左边的各位数*距离=右边的各位数*距离(自己 ...

  7. ZOJ 3689 Digging(贪心+dp)

    Digging Time Limit: 2 Seconds      Memory Limit: 65536 KB When it comes to the Maya Civilization, we ...

  8. Codeforces Gym101201B:Buggy Robot(BFS + DP)

    题目链接 题意 给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作. 思路 和上次比赛做的 ...

  9. HDU3709 Balanced Number (数位dp)

     Balanced Number Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

随机推荐

  1. Windows下PhpStorm结合WAMP开发Phalcon应用的配置

    最近要利用Phalcon框架开发PHP应用,因为以前基本没接触过PHP更没用过PHP框架,结果整环境整IDE配置什么的花了好长时间 学习慕课网上的PHP入门教程安装了WAMP(windows+apac ...

  2. 对于fmri的hrf血液动力学响应函数的一个很直观的解释-by 西南大学xulei教授

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all;clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...

  3. mysql,多表的内外连接+子查询

    表: student house course 关系:student_course 多对多 student house 多对一 需求:查询房间1 的学生 都学习了什么课程 select s.s_nam ...

  4. php延迟加载的示例

    class a{ ; ; //public $d = 5; public function aa(){ echo self::$b; } public function cc(){ echo stat ...

  5. mysql 优化analyze table

    Analyze Table MySQL 的Optimizer(优化元件)在优化SQL语句时,首先需要收集一些相关信息,其中就包括表的cardinality(可以翻译为“散列程度”),它表示某个索引对应 ...

  6. IE6对CSS支持Bug收集

    1.IE6双外边距 在IE6下,如果对元素设置了浮动,同时又设置了margin-left或者margin-right,margin值会加倍. 例如,设置margin-left:10px在IE6下会显示 ...

  7. Symfony2学习笔记之数据库操作

    数据库和Doctrine让我们来面对这个对于任何应用程序来说最为普遍最具挑战性的任务,从数据库中读取和持久化数据信息.幸运的是,Symfony和Doctrine进行了集成,Doctrine类库全部目标 ...

  8. 看看JavaScript中void(0)的含义

    JavaScript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. void 操作符用法格式如下:1. javascript:void (expression)2. javascr ...

  9. WPF的控件Binding的ElementName/RelativeSource具体用法

    <TextBlock Name="_txtSickBedNo" FontStyle="Normal" Foreground="Black&quo ...

  10. AE 中的查找与定位,以城市查找为例

    在文本框输入一个城市,在地图上查找,当找到后让mapcontrol自动跳转到地图上该点. IQueryFilter filter = new QueryFilterClass(); filter.Wh ...