3031: Multiple

Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte
Total Submit:
60
          
Accepted:10

Description

a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2..XM (if such a multiple exists).

Input

The input has several data sets separated by an empty line, each data set having the following format:

On the first line - the number N
On the second line - the number M
On the following M lines - the digits X1,X2..XM.

Output

For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.

An example of input and output:

Sample Input

22
3
7
0
1

2
1
1

Sample Output

110
0

Source

Southeastern Europe 2000

这个题目可能没那么爽啊,就是给你m个数字,然后用这些数字就构造n的倍数

首先肯定贪心取啊,然后就是大数取余,所以这个时候同余的,然后bfs就完了

#include<stdio.h>
#include<string.h>
#include<algorithm>
const int N=;
struct T
{
int res,pre,digit;
} q[N],t;
int n,m,a[];
bool vis[N];
void print (int x)
{
if(q[x].pre==-)return;
print(q[x].pre),printf("%d",q[x].digit);
}
int bfs()
{
q[]= {,-,},memset(vis,,sizeof vis);
int tot=,lst=;
while(tot<lst)
{
t=q[tot];
for(int i=,t1; i<m; i++)
{
t1=(t.res*+a[i])%n;
if(!a[i]&&t.pre==-)continue;
if(!vis[t1])vis[t1]=,q[lst++]= {t1,tot,a[i]};
if(!t1)
{
print(lst-),printf("\n");
return ;
}
}
tot++;
}
return ;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<m; i++)scanf("%d",&a[i]);
if(!n)printf("0\n");
else
{
std::sort(a,a+m);
if(!bfs())printf("0\n");
}
}
}

TOJ3031: Multiple bfs的更多相关文章

  1. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  2. POJ1426Find The Multiple[BFS]

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27433   Accepted: 114 ...

  3. ZOJ 1136 Multiple (BFS)

    Multiple Time Limit: 10 Seconds      Memory Limit: 32768 KB a program that, given a natural number N ...

  4. POJ 1465 Multiple (BFS,同余定理)

    id=1465">http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K T ...

  5. poj 1426 Find The Multiple (bfs 搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 729 ...

  6. POJ1426 Find The Multiple —— BFS

    题目链接:http://poj.org/problem?id=1426 Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  7. POJ 1426 Find The Multiple BFS

    没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...

  8. poj1426 - Find The Multiple [bfs 记录路径]

    传送门 转:http://blog.csdn.net/wangjian8006/article/details/7460523 (比较好的记录路径方案) #include<iostream> ...

  9. poj 1426 Find The Multiple ( BFS+同余模定理)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

随机推荐

  1. ImageLoader常用方法注释

    ImageLoader中的常用方法及相关作用注释 ImageLoader 的ImageLoaderConfiguration config 配置 ImageLoaderConfiguration co ...

  2. Linux系统日志分析

    Linux系统拥有非常灵活和强大的日志功能,可以保存几乎所有的操作记录,并可以从中检索出我们需要的信息. 大部分Linux发行版默认的日志守护进程为 syslog,位于 /etc/syslog 或 / ...

  3. Android篇---Styles和Themes常见用法可能疑点小结

    1.style和theme的区别: 简而言之,style指的就是安卓中一个UI控件的样式,而themes指的是安卓中一个activity界面或者整个安卓应用整体的样式.theme的范围比style的范 ...

  4. 网站安全webshell扫描

    做个记录,使用Detector进行php网站webshell扫描 开源项目托管地址:https://github.com/emposha/PHP-Shell-Detector安装使用都很简单

  5. SAP Cloud for Customer客户主数据的地图集成

    点击这个按钮可以通过地图的方式查看C4C客户在地图上的地理位置: 只需要在这个客户的地址栏里维护上天府软件园的经度和维度: 就能够在C4C的客户列表页面里显示该客户在地图上的位置: 要获取更多Jerr ...

  6. MovieReview—A dog's purpose(一只狗的使命)

    Be Here Now                                                             A dog in the movie was reinc ...

  7. 【UML】部署图Deployment diagram(实现图)(转)

    http://blog.csdn.net/sds15732622190/article/details/49049665 前言 下面要介绍UML中的部署图,和构件图一样,它也属于实现图的一种,五种静态 ...

  8. shell的切换

    从zsh切换到bash:在命令行输入bash即可 从bash切换到zsh:在命令行输入zsh即可

  9. Python 多线程应用

    同步锁 import time import threading def subNum(): global num # print("ok") lock.acquire() # 加 ...

  10. javaweb基础(11)_cookie的会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...