题意:

  给定m个1位数字,要求用这些数字组成n的倍数的最小数字,如果无法组成就输出0

分析:

  BFS,由于n最大5000,余数最多5000,利用余数去判重,并记录下路径即可

代码:

  

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=5010;
int n,m,vis[maxn],pre[maxn],ans[maxn],q[maxn];
vector<int>s;
void print(int u)
{
if(u==-1)
return;
print(pre[u]);
printf("%d",ans[u]);
}
int main()
{
while(~scanf("%d",&n))
{
scanf("%d",&m);
s.clear();
memset(vis,0,sizeof(vis));
int head=0,rear=0;
int num;
while(m--)
{
scanf("%d",&num);
s.push_back(num);
}
sort(s.begin(),s.end());
if(n==0)
{
printf("0\n");
continue;
}
int i;
for(i=0;i<s.size();i++)
{
if(s[i]==0)
continue;
int tmp=s[i]%n;
ans[rear]=s[i];
pre[rear]=-1;
q[rear++]=tmp;
vis[tmp]=1;
}
int flag=1;
while(head<rear)
{
int u=q[head];
if(u==0)
{
print(head);
flag=0;
printf("\n");
break;
}
for(i=0;i<s.size();i++)
{
int temp=((u*10+s[i])%n);
if(vis[temp]==0)
{
vis[temp]=1;
q[rear]=temp;
ans[rear]=s[i];
pre[rear++]=head;
}
}
head++;
}
if(flag)
printf("0\n");
}
return 0;
}

UVA 1569 Multiple的更多相关文章

  1. UVA 1395 Slim Span

    题意: 要求的是所有生成树中最大边与最小边差值最小的那个. 分析: 其实可以利用最小瓶颈生成树,就是最小生成树这一性质,枚举原图的最小边,然后找相应生成树的最大边 代码: #include <i ...

  2. UVA - 1160 X-Plosives

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  3. UVA - 11400 Lighting System Design

    题文: You are given the task to design a lighting system for a huge conference hall. After doing a lot ...

  4. uva 11324 The Largest Clique

    vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...

  5. uva 11728 Alternate Task

    vjudge 上题目链接:uva 11728 其实是个数论水题,直接打表就行: #include<cstdio> #include<algorithm> using names ...

  6. UVa 103 Stacking Boxes --- DAG上的动态规划

    UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...

  7. UVa 489 HangmanJudge --- 水题

    UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...

  8. UVa 1339 Ancient Cipher --- 水题

    UVa 1339 题目大意:给定两个长度相同且不超过100个字符的字符串,判断能否把其中一个字符串重排后,然后对26个字母一一做一个映射,使得两个字符串相同 解题思路:字母可以重排,那么次序便不重要, ...

  9. UVa 1225 Digit Counting --- 水题

    UVa 1225 题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字 解题思路:用一个cnt数组记录0-9这10个数字出现 ...

随机推荐

  1. static_cast

    C 风格(C-style)强制转型例如以下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这 ...

  2. FlashBack-SCN-TIMESTAMP

    一.基于时间(as of timestamp)的flashback1.创建表create table flash_tab(id,vl) as select rownum,oname from ( se ...

  3. 查看mysql apache php nginx的编译参数

    查看mysql编译参数: cat /usr/local/mysql/bin/mysqlbug|grep configure 查看apache编译参数: cat /usr/local/apache2/b ...

  4. 清理SQL数据库日志

    Use DBSelect NAME,size From sys.database_files ALTER DATABASE DB SET RECOVERY SIMPLE WITH NO_WAIT AL ...

  5. WIFI网络访问(一)

    一,WIFI 网卡有哪些状态? WIFI 总共有以下五个状态,实际就是一些整形常量: 1.   WIFI_STATE_DISABLED : WIFI 不能使用,其值是: 1 . 2.   WIFI_S ...

  6. Chrome和Firefox浏览器调试对比

    最近的项目中使用Extjs5, 其中主要的一个特点就是js文件的动态加载,之前使用Firefox浏览器对js文件进行调试,打断点时,只对当次调试有效,刷新之后,由于动态加载的js文件(文件名后面加了一 ...

  7. mysql中select into 和sql中的select into 对比

    现在有张表为student,我想将这个表里面的数据复制到一个为dust的新表中去.answer 01: create table dust select * from student;//用于复制前未 ...

  8. 移动端-弹窗demo

    <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name ...

  9. PHP扩展开发之PHP的启动与终止

    PHP程序的启动可以看做是两个概念上的启动,终止也有两个概念上的终止.其中一个是PHP作为Apache(拿它举例,板砖勿扔)的一个模块的启动与终止, 这次启动php会初始化一些必要数据,比如与宿主Ap ...

  10. 【ecos学习1】wmware运行redboot[方法一]--脚本实现配置

    背景: 远程服务器Ubuntu生成软盘镜像,通过Mac下wmware运行. 1- 环境及版本: uname -a 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 ...