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:
 #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的更多相关文章

  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. 连载《一个程序猿的生命周期》-《发展篇》 - 3.农民与软件工程师,农业与IT业

    相关文章:随笔<一个程序猿的生命周期>- 逆潮流而动的“叛逆者”        15年前,依稀记得走出大山,进城求学的场景.尽管一路有父亲的陪伴,但是内心仍然畏惧.当父亲转身离去.准备回到 ...

  2. 如何快速清除ZBrush画布中多余图像

    ZBrush是一款数字雕刻与绘画软件,它以强大的功能和直观的工作流程彻底改变了整个三维行业.它的简洁化.智能化和人性化的设计无不让众多用户所折服.刚接触它的用户可能会因为找不到相关命令或不熟悉而觉得它 ...

  3. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  4. linux基础-磁盘阵列(RAID)实例详解

    磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 R ...

  5. Django框架

    一.首先,到底什么是框架? 想要回答这个问题,我们要慢慢来. ①首先从DRY原则开始说起 Don't Repeat Yourself,不要重复你的代码. DRY原则的重要性怎么提都不过分,很多人说编程 ...

  6. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

  7. AC自动机 HDU 3065

    大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...

  8. 冰冻三尺非一日之寒--web框架Django(三)

      第二十章: django(三,多对多)   1.Django请求的生命周期         路由系统 -> 视图函数(获取模板+数据-->渲染) -> 字符串返回给用户   2. ...

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

  10. 学记:spring boot使用官网推荐以外的其他数据源druid

    虽然spring boot提供了4种数据源的配置,但是如果要使用其他的数据源怎么办?例如,有人就是喜欢druid可以监控的强大功能,有些人项目的需要使用c3p0,那么,我们就没办法了吗?我们就要编程式 ...