K - Yet Another Multiple Problem

Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2014-10-16)

Description

There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”. 
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
 

Input

There are several test cases. 
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 10 4). The second line contains m decimal digits separated by spaces. 
Input is terminated by EOF.
 

Output

For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
 

Sample Input

2345 3
7 8 9
100 1
0
 

Sample Output

Case 1: 2345
Case 2: -1
 
题意:以样例为例,2345的最小倍数,不包含给出的三个数7 8 9
思路:bfs,以0-9中能够使用的数字bfs,一位一位的加在后面,当第一个出现数字x%n==0时,则x为解。
这里需要的知识点是,(x*10+i)%n == (x%n)*10+i,所以只需要存(x%n)所有可能,根据抽屉原理,节点数不超过n,这样就可以很快搜到了。
存结果的话,因为结果有可能很长很长,可以在结构体里面加一个字符串,从前面的点的字符串更新过来,也就是在最后加一个'i',或者开数组存这个数的结尾num[i],然后和前面更新过来的节点pre[i].
 
注意:只有0为可行数字的情况的特殊处理
错在一开始vis数组在第一个数时没置1,只有0为可行数字的情况的特殊处理处理错,还有新的数没%n就放越界。
数组bfs:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #define M(a,b) memset(a,b,sizeof(a))
  6. #define INF 0x3f3f3f3f
  7.  
  8. using namespace std;
  9.  
  10. int n,m;
  11.  
  12. int pre[],num[];
  13. int can[];
  14. int que[];
  15. int vis[];
  16. int res[];
  17.  
  18. int bfs()
  19. {
  20. int head = -;
  21. int tail = ;
  22. M(vis,);
  23. que[] = ;
  24. for(int i = ;i<;i++)
  25. {
  26. if(!can[i])
  27. {
  28. if(i%n==) {num[i] = i,pre[i] = ; return i;}
  29. else num[i] = i,pre[i] = , vis[i] = , que[tail] = i,tail++;
  30. }
  31. }
  32. while(head<tail)
  33. {
  34. head++;
  35. int tmp = que[head];
  36. //cout<<tmp<<endl;
  37. for(int i = ;i<;i++)
  38. {
  39. if(i==&&tmp==) continue;
  40. //cout<<i<<endl;
  41. if(!can[i])
  42. {
  43. int u = tmp*+i;
  44. int t = u%n;
  45. if(!vis[t])
  46. {
  47. if(t==) {pre[u] = tmp, num[u] = i;return u;}
  48. else{
  49. //cout<<t<<' '<<i<<endl;
  50. pre[t] = tmp, num[t] = i;
  51. vis[t] = ;
  52. que[tail] = t;
  53. tail++;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. return -;
  60. }
  61.  
  62. int main()
  63. {
  64. int cas = ;
  65. while(scanf("%d%d",&n,&m)==)
  66. {
  67. M(pre,);
  68. M(num,);
  69. M(can,);
  70. for(int i = ;i<m;i++)
  71. {
  72. int a;
  73. scanf("%d",&a);
  74. can[a] = ;
  75. }
  76. pre[] = -;
  77. int ans = bfs();
  78. if(m==) {printf("Case %d: -1\n",cas++); continue;}
  79. printf("Case %d: ",cas++);
  80. if(ans == -) puts("-1");
  81. else{
  82. int cnt = ;
  83. for(int i = ans;pre[i]!=-;i = pre[i])
  84. res[cnt++] = num[i];
  85. for(int i = cnt-;i>;i--)
  86. printf("%d",res[i]);
  87. printf("%d\n",res[]);
  88. }
  89. }
  90. return ;
  91. }

queue+struct:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<queue>
  6. #define M(a,b) memset(a,b,sizeof(a))
  7. #define INF 0x3f3f3f3f
  8.  
  9. using namespace std;
  10.  
  11. int n,m;
  12.  
  13. struct node{
  14. int num;
  15. string c;
  16. };
  17. queue<node> que;
  18. int can[];
  19. int vis[];
  20. int res;
  21.  
  22. node bfs()
  23. {
  24. while(!que.empty()) que.pop();
  25. M(vis,);
  26. for(int i = ;i<;i++)
  27. {
  28. if(!can[i])
  29. {
  30. node tp;
  31. tp.c = "";
  32. tp.num = ;
  33. if(i%n==) {char ch = i+''; tp.c += ch; return tp;}
  34. else {
  35. tp.num = i%n;
  36. char ch = i+'';
  37. tp.c += ch;
  38. vis[i] = ;
  39. que.push(tp);
  40. //cout<<tp.c<<endl;
  41. }
  42. }
  43. }
  44. while(!que.empty())
  45. {
  46. node tmp = que.front();
  47. que.pop();
  48. //cout<<tmp.c<<endl;
  49. for(int i = ;i<;i++)
  50. {
  51. if(!can[i])
  52. {
  53. int t = (tmp.num*+i)%n;
  54. if(!vis[t])
  55. {
  56. if(t==) {char ch = i+''; tmp.c+=ch; return tmp;}
  57. else
  58. {
  59. node tp;
  60. tp.num = t;
  61. char ch = i+'';
  62. tp.c = tmp.c+ch;
  63. //cout<<tp.num<<endl;
  64. vis[t] = ;
  65. que.push(tp);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. res = -;
  72. node none;
  73. return none;
  74. }
  75.  
  76. int main()
  77. {
  78. int cas = ;
  79. while(scanf("%d%d",&n,&m)==)
  80. {
  81. res = ;
  82. M(can,);
  83. for(int i = ;i<m;i++)
  84. {
  85. int a;
  86. scanf("%d",&a);
  87. can[a] = ;
  88. }
  89. if(m==) {printf("Case %d: -1\n",cas++); continue;}
  90. node ans = bfs();
  91. printf("Case %d: ",cas++);
  92. if(res == -) puts("-1");
  93. else cout<<ans.c<<endl;
  94. }
  95. return ;
  96. }

queue+pair:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<queue>
  6. #define M(a,b) memset(a,b,sizeof(a))
  7. #define INF 0x3f3f3f3f
  8.  
  9. using namespace std;
  10.  
  11. int n,m;
  12.  
  13. int can[];
  14. int vis[];
  15. int res;
  16.  
  17. queue<pair<string,int> > rec;
  18.  
  19. string bfs()
  20. {
  21. while (!rec.empty()) rec.pop();
  22. pair<string,int>init;
  23. init.first="";init.second=;
  24. rec.push(init);
  25. int i;
  26. while (!rec.empty())
  27. {
  28. pair<string,int> curr=rec.front();
  29. for (i=;i<;i++)
  30. {
  31. if (curr.first.length()==&&i==) continue;
  32. if (can[i]) continue;
  33. char ch=''+i;
  34. string ss=curr.first+ch;
  35. int x=(curr.second*+i)%n;
  36. if (!vis[x])
  37. {
  38. if (x==) return ss;
  39. pair<string,int>u;
  40. u.first=ss;u.second=x;
  41. rec.push(u);
  42. vis[x]=;
  43. }
  44. }
  45. rec.pop();
  46. }
  47. return "-1";
  48. }
  49.  
  50. int main()
  51. {
  52. int cas = ;
  53. while(scanf("%d%d",&n,&m)==)
  54. {
  55. M(can,);
  56. M(vis,);
  57. for(int i = ;i<m;i++)
  58. {
  59. int a;
  60. scanf("%d",&a);
  61. can[a] = ;
  62. }
  63. string ans = bfs();
  64. printf("Case %d: ",cas++);
  65. cout<<ans<<endl;
  66. }
  67. return ;
  68. }
 

2012Chhengdu K - Yet Another Multiple Problem的更多相关文章

  1. 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 ...

  2. K - Least Common Multiple

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  3. Yet Another Multiple Problem(bfs好题)

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

  4. hdu4474 Yet Another Multiple Problem

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

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

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

  6. HDU4474_Yet Another Multiple Problem

    题意很简单,要你用一些数字,组成一个数的倍数,且那个数最小. 比赛的时候没能做出来,深坑啊. 其实我只想说我以前就做过这种类型的题目了,诶. 题目的解法是数位宽搜. 首先把可用的数位提取出来,从小到大 ...

  7. HDU 4474 Yet Another Multiple Problem BFS

    题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0  1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...

  8. hdu 4474 Yet Another Multiple Problem

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

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

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

随机推荐

  1. 刷新页面时 select值保持不变

    刷新页面时,要使下拉菜单(select).raido保持不变,用ajax是无法实现的.我想只能通过cookies才能实现.刷新前先把select或radio的值保存在cookies中,刷新后再填回去. ...

  2. 个人CTF资源聚合

    i春秋 幻泉 CTF入门课程笔记 视频地址 能力 思维能力 快速学习能力 技术能力 基础 编程基础 (c语言 汇编语言 脚本语言) 数学基础 (算法 密码学) 脑洞 (天马行空的想象推理) 体力耐力( ...

  3. 基于pcDuino-V2的无线视频智能小车

    这段时间抽空做了个智能视频小车.包含了pid电机控制.socket网络编程.多线程编程.epoll机制.gtk图形界面编程. 这是界面: 小车的底层是用的stm32f405系列的单片机+电机驱动做的一 ...

  4. SimpleDateFormat使用详解——日期、字符串应用

    public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...

  5. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  6. [LeetCode] Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  7. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. Docker实践--部署Nodejs应用

    这个例子的目标是为了向大家展示如何在Docker的container里运行Node.js程序.我会先创建一个简单的Node.js web app,来构建一个镜像.然后基于这个Image运行一个cont ...

  9. 基于C/S架构的3D对战网络游戏C++框架 _【不定期更新通知】

    由于笔者最近有比赛项目要赶,这个基于C/S架构的3D对战网络游戏C++框架也遇到了一点瓶颈需要点时间沉淀,所以近一段时间不能保证每天更新了,会保持不定期更新.同时近期笔者也会多分享一些已经做过学过的C ...

  10. Linux CGroup之freezer分析与应用

    Linux Kernel:4.4.17 CGroup的freezer子系统对于成批作业管理系统很有用,可以成批启动/停止任务,以达到及其资源的调度. freezer子系统也有助于针对运行一组任务设置检 ...