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. redis键命令

    1.ping命令用于检测redis是否启动 成功返回pong表示链接成功 2.在远程redis服务上执行命令 Redis-cli -h host -p port -a password 如果是连接本机 ...

  2. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  3. 【swift学习笔记】六.访facebook登录页面

    代码最下边有下载地址. 做这个demo的主要心得就是自适应所有的屏幕,要先布局大的框架,再一步一步设置小的细节. 看一下效果 再看一下自动适应所有屏幕的效果: keyboard打开时整个frame上移 ...

  4. leecode系列--Two Sum

    学习这件事在任何时间都不能停下.准备坚持刷leecode来提高自己,也会把自己的解答过程记录下来,希望能进步. Two Sum Given an array of integers, return i ...

  5. .Net Core Linux centos7行—发布程序到生产环境

    实验demo现在需要发布到生产环境,发现在发布的时候要考虑到不一致的几个地方. 1.各类配置文件线下,线上不一致. 2.绑定的url不一致,可能是域名不一致,也可能是schema不一致(http,ht ...

  6. thinkcmf无法使用config.html中的配置量

    在模版中引入 <tc_include file=":config" />

  7. 全局变量都是window对象的属性

    var x = "haha"; var test  = function(){ alert(this.x); } 上述,则会弹出 haha的值. 因为在JavaScript的变量作 ...

  8. SqlServer中使用正则表达式

    一.查看配置,如果‘show advanced options’,‘Ole Automation Procedures’,‘Ad Hoc Distributed Queries’的config_val ...

  9. bzoj 1070 [SCOI2007]修车

    最小费用最大流. 将每个技术人员拆成车数个点,技术人员i的第j个点代表技术人员i修的倒数第j辆车. 源点向所有技术人员点连一条容量为1费用为0的边. 所有技术人员点向所有车点连边:技术人员i的第j个点 ...

  10. dom 节点篇---模块

    改写成如下代码: var creatTag=(function(){ //var count=5; var oUl=document.createElement('ul'); var oDiv=doc ...