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 ...
随机推荐
- Java基础知识笔记(八:集合类)
目录 1 集合类简介 2 List介绍及简单使用 2.1 LinkedList介绍及简单使用 2.2 ArrayList介绍及简单使用 2.3 Vector介绍及简单使用 2.3.1 S ...
- 关于如何使用sourcetree将本地项目提交到远端github总结?
使用sourcetree将本地项目提交到github里,目前来说还是很流行的,我也是听说好玩,所以来琢磨了一下,从环境搭建到配置好,差不多用了一下午加一晚上的时间,有点虐心,好吧,废话不多说,介绍一下 ...
- 【转】虚拟机VMware与主机共享文件介绍
from: http://www.cnblogs.com/kerrycode/p/3818095.html 写的比较详细,但是vm版本较旧. 2:通过共享文件夹功能 虚拟机VMware提供了在宿主机与 ...
- 【repost】JavaScript完美运动框架的进阶之旅
运动框架的实现思路 运动,其实就是在一段时间内改变left.right.width.height.opactiy的值,到达目的地之后停止. 现在按照以下步骤来进行我们的运动框架的封装: 匀速运动. 缓 ...
- 基于AngularJS的个推前端云组件探秘
基于AngularJS的个推前端云组件探秘 AngularJS是google设计和开发的一套前端开发框架,帮助开发人员简化前端开发的负担.AngularJS将帮助标准化的开发web应用结构并且提供了针 ...
- jquery each函数 break和continue功能
jquery each函数 break和continue功能幸运的是另一个突破,持续一个jQuery循环方式.你可以打破在函数返回一个jQuery参数虚假循环.一个可以继续执行只是在做不指定返回值或返 ...
- [LeetCode] Peeking Iterator 顶端迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- 分享基于EF+MVC+Bootstrap的通用后台管理系统及架构
基于EF+MVC+Bootstrap构建通用后台管理系统,集成轻量级的缓存模块.日志模块.上传缩略图模块.通用配置及服务调用, 提供了OA.CRM.CMS的原型实例,适合快速构建中小型互联网及行业 ...
- 【C#】调度程序进程已挂起,但消息仍在处理中;
环境:WPF.弹窗,messageBox.show();错误信息:调度程序进程已挂起,但消息仍在处理中:解决方法:Dispatcher.BeginInvoke(new Action(()=>{ ...
- ios应用程序结构
MVC开发模式 什么是mvc,相信有一定开发经验的程序员都应该知道. M —— 模型,为程序提供数据 V —— 视图,为用户提供界面 C —— 控制器,用来控制程序视图,即上面的V 在ios程序中,一 ...