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 ...
随机推荐
- 连载《一个程序猿的生命周期》-《发展篇》 - 3.农民与软件工程师,农业与IT业
相关文章:随笔<一个程序猿的生命周期>- 逆潮流而动的“叛逆者” 15年前,依稀记得走出大山,进城求学的场景.尽管一路有父亲的陪伴,但是内心仍然畏惧.当父亲转身离去.准备回到 ...
- 如何快速清除ZBrush画布中多余图像
ZBrush是一款数字雕刻与绘画软件,它以强大的功能和直观的工作流程彻底改变了整个三维行业.它的简洁化.智能化和人性化的设计无不让众多用户所折服.刚接触它的用户可能会因为找不到相关命令或不熟悉而觉得它 ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- linux基础-磁盘阵列(RAID)实例详解
磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 R ...
- Django框架
一.首先,到底什么是框架? 想要回答这个问题,我们要慢慢来. ①首先从DRY原则开始说起 Don't Repeat Yourself,不要重复你的代码. DRY原则的重要性怎么提都不过分,很多人说编程 ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- AC自动机 HDU 3065
大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...
- 冰冻三尺非一日之寒--web框架Django(三)
第二十章: django(三,多对多) 1.Django请求的生命周期 路由系统 -> 视图函数(获取模板+数据-->渲染) -> 字符串返回给用户 2. ...
- Oracle时间戳(毫秒)转为Date
1.SQL ) + TO_DATE('1970-01-01 08:00:00', 'YYYY-MM-DD HH:MI:SS'), 'YYYY-MM-DD HH:MI:SS') AS CDATE FRO ...
- 学记:spring boot使用官网推荐以外的其他数据源druid
虽然spring boot提供了4种数据源的配置,但是如果要使用其他的数据源怎么办?例如,有人就是喜欢druid可以监控的强大功能,有些人项目的需要使用c3p0,那么,我们就没办法了吗?我们就要编程式 ...