2012Chhengdu K - Yet Another Multiple Problem
Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
System Crawler (2014-10-16)
Description
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
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
Sample Input
7 8 9
100 1
0
Sample Output
Case 2: -1
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #define M(a,b) memset(a,b,sizeof(a))
- #define INF 0x3f3f3f3f
- using namespace std;
- int n,m;
- int pre[],num[];
- int can[];
- int que[];
- int vis[];
- int res[];
- int bfs()
- {
- int head = -;
- int tail = ;
- M(vis,);
- que[] = ;
- for(int i = ;i<;i++)
- {
- if(!can[i])
- {
- if(i%n==) {num[i] = i,pre[i] = ; return i;}
- else num[i] = i,pre[i] = , vis[i] = , que[tail] = i,tail++;
- }
- }
- while(head<tail)
- {
- head++;
- int tmp = que[head];
- //cout<<tmp<<endl;
- for(int i = ;i<;i++)
- {
- if(i==&&tmp==) continue;
- //cout<<i<<endl;
- if(!can[i])
- {
- int u = tmp*+i;
- int t = u%n;
- if(!vis[t])
- {
- if(t==) {pre[u] = tmp, num[u] = i;return u;}
- else{
- //cout<<t<<' '<<i<<endl;
- pre[t] = tmp, num[t] = i;
- vis[t] = ;
- que[tail] = t;
- tail++;
- }
- }
- }
- }
- }
- return -;
- }
- int main()
- {
- int cas = ;
- while(scanf("%d%d",&n,&m)==)
- {
- M(pre,);
- M(num,);
- M(can,);
- for(int i = ;i<m;i++)
- {
- int a;
- scanf("%d",&a);
- can[a] = ;
- }
- pre[] = -;
- int ans = bfs();
- if(m==) {printf("Case %d: -1\n",cas++); continue;}
- printf("Case %d: ",cas++);
- if(ans == -) puts("-1");
- else{
- int cnt = ;
- for(int i = ans;pre[i]!=-;i = pre[i])
- res[cnt++] = num[i];
- for(int i = cnt-;i>;i--)
- printf("%d",res[i]);
- printf("%d\n",res[]);
- }
- }
- return ;
- }
queue+struct:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<queue>
- #define M(a,b) memset(a,b,sizeof(a))
- #define INF 0x3f3f3f3f
- using namespace std;
- int n,m;
- struct node{
- int num;
- string c;
- };
- queue<node> que;
- int can[];
- int vis[];
- int res;
- node bfs()
- {
- while(!que.empty()) que.pop();
- M(vis,);
- for(int i = ;i<;i++)
- {
- if(!can[i])
- {
- node tp;
- tp.c = "";
- tp.num = ;
- if(i%n==) {char ch = i+''; tp.c += ch; return tp;}
- else {
- tp.num = i%n;
- char ch = i+'';
- tp.c += ch;
- vis[i] = ;
- que.push(tp);
- //cout<<tp.c<<endl;
- }
- }
- }
- while(!que.empty())
- {
- node tmp = que.front();
- que.pop();
- //cout<<tmp.c<<endl;
- for(int i = ;i<;i++)
- {
- if(!can[i])
- {
- int t = (tmp.num*+i)%n;
- if(!vis[t])
- {
- if(t==) {char ch = i+''; tmp.c+=ch; return tmp;}
- else
- {
- node tp;
- tp.num = t;
- char ch = i+'';
- tp.c = tmp.c+ch;
- //cout<<tp.num<<endl;
- vis[t] = ;
- que.push(tp);
- }
- }
- }
- }
- }
- res = -;
- node none;
- return none;
- }
- int main()
- {
- int cas = ;
- while(scanf("%d%d",&n,&m)==)
- {
- res = ;
- M(can,);
- for(int i = ;i<m;i++)
- {
- int a;
- scanf("%d",&a);
- can[a] = ;
- }
- if(m==) {printf("Case %d: -1\n",cas++); continue;}
- node ans = bfs();
- printf("Case %d: ",cas++);
- if(res == -) puts("-1");
- else cout<<ans.c<<endl;
- }
- return ;
- }
queue+pair:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<queue>
- #define M(a,b) memset(a,b,sizeof(a))
- #define INF 0x3f3f3f3f
- using namespace std;
- int n,m;
- int can[];
- int vis[];
- int res;
- queue<pair<string,int> > rec;
- string bfs()
- {
- while (!rec.empty()) rec.pop();
- pair<string,int>init;
- init.first="";init.second=;
- rec.push(init);
- int i;
- while (!rec.empty())
- {
- pair<string,int> curr=rec.front();
- for (i=;i<;i++)
- {
- if (curr.first.length()==&&i==) continue;
- if (can[i]) continue;
- char ch=''+i;
- string ss=curr.first+ch;
- int x=(curr.second*+i)%n;
- if (!vis[x])
- {
- if (x==) return ss;
- pair<string,int>u;
- u.first=ss;u.second=x;
- rec.push(u);
- vis[x]=;
- }
- }
- rec.pop();
- }
- return "-1";
- }
- int main()
- {
- int cas = ;
- while(scanf("%d%d",&n,&m)==)
- {
- M(can,);
- M(vis,);
- for(int i = ;i<m;i++)
- {
- int a;
- scanf("%d",&a);
- can[a] = ;
- }
- string ans = bfs();
- printf("Case %d: ",cas++);
- cout<<ans<<endl;
- }
- return ;
- }
2012Chhengdu K - Yet Another Multiple Problem的更多相关文章
- 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 ...
- K - Least Common Multiple
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K ( ...
- hdu4474 Yet Another Multiple Problem
Yet Another Multiple Problem Description There are tons of problems about integer multiples. Despite ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- HDU4474_Yet Another Multiple Problem
题意很简单,要你用一些数字,组成一个数的倍数,且那个数最小. 比赛的时候没能做出来,深坑啊. 其实我只想说我以前就做过这种类型的题目了,诶. 题目的解法是数位宽搜. 首先把可用的数位提取出来,从小到大 ...
- HDU 4474 Yet Another Multiple Problem BFS
题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0 1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...
- hdu 4474 Yet Another Multiple Problem
题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
随机推荐
- 刷新页面时 select值保持不变
刷新页面时,要使下拉菜单(select).raido保持不变,用ajax是无法实现的.我想只能通过cookies才能实现.刷新前先把select或radio的值保存在cookies中,刷新后再填回去. ...
- 个人CTF资源聚合
i春秋 幻泉 CTF入门课程笔记 视频地址 能力 思维能力 快速学习能力 技术能力 基础 编程基础 (c语言 汇编语言 脚本语言) 数学基础 (算法 密码学) 脑洞 (天马行空的想象推理) 体力耐力( ...
- 基于pcDuino-V2的无线视频智能小车
这段时间抽空做了个智能视频小车.包含了pid电机控制.socket网络编程.多线程编程.epoll机制.gtk图形界面编程. 这是界面: 小车的底层是用的stm32f405系列的单片机+电机驱动做的一 ...
- SimpleDateFormat使用详解——日期、字符串应用
public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [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 ...
- Docker实践--部署Nodejs应用
这个例子的目标是为了向大家展示如何在Docker的container里运行Node.js程序.我会先创建一个简单的Node.js web app,来构建一个镜像.然后基于这个Image运行一个cont ...
- 基于C/S架构的3D对战网络游戏C++框架_【不定期更新通知】
由于笔者最近有比赛项目要赶,这个基于C/S架构的3D对战网络游戏C++框架也遇到了一点瓶颈需要点时间沉淀,所以近一段时间不能保证每天更新了,会保持不定期更新.同时近期笔者也会多分享一些已经做过学过的C ...
- Linux CGroup之freezer分析与应用
Linux Kernel:4.4.17 CGroup的freezer子系统对于成批作业管理系统很有用,可以成批启动/停止任务,以达到及其资源的调度. freezer子系统也有助于针对运行一组任务设置检 ...