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. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  2. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  3. 未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序。解决办法

    在64位服务器系统上,默认不支持Microsoft.Jet.OLEDB.4.0的驱动程序,系统默认会提示未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0"的错误 ...

  4. electron 入门小白贴

    electron 入门小白贴 electron demo 跑起来! 毕设准备是做个 跨平台的做题的客户端,打算用 electron 来弄. 然而今天折腾了半天才终于吧demo给跑起来了.经历了许多的问 ...

  5. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  6. ORB-SLAM(六)回环检测

    上一篇提到,无论在单目.双目还是RGBD中,追踪得到的位姿都是有误差的.随着路径的不断延伸,前面帧的误差会一直传递到后面去,导致最后一帧的位姿在世界坐标系里的误差有可能非常大.除了利用优化方法在局部和 ...

  7. xv6课本翻译之——第0章 操作系统接口

    Chapter 0 第0章 Operating system interfaces 操作系统接口 The job of an operating system is to share a comput ...

  8. JUC学习笔记--Thread多线程基础

    实现多线程的两种方法 java 实现多线程通过两种方式1.继承Thread类 ,2.实现Runnable接口 class Newthead extends Thread{ public void ru ...

  9. php 远程图片本地化

    /** * 把新浪的远程图片下载到自己服务器上 * * @access public * @param goods_desc $goods_desc 要处理的内容 * @return mix 如果成功 ...

  10. 几种常见的Shell

    Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把它们称作一种Shell.我们常说有多少种Shell,其实说的是Shell脚本解释器. bash bash是L ...