记忆化搜索,因为要求最小的,肯定是从小到大,依次添加,那么通过bfs,队列貌似是最好的选择。因为很可能那个数爆long long,所以采用字符串存储,并记录余数,通过模拟除法的方式来写。

剪枝:因为后面添加的数都是一样的,所以相同的余数后面的过程都是一样的,所以我们需要通过一个数组优化。

注意:string和char数组的相互转写和除数和被除数分别为0的情况。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
int a[10];
struct node
{
string s;
int res;
};
int mark[5000];
int m,n;
string bfs()
{
queue<node> q;
string ss;
for(int i=0;i<n;i++)
{
int x=a[i]%m;
if(a[i]!=0&&x==0)
{
return ss+char('0'+a[i]);
}
if(mark[x])
continue;
node t;
t.s=ss+char('0'+a[i]);
t.res=x;
q.push(t);
mark[x]=1;
}
node ans;
while(!q.empty())
{
node t=q.front();
q.pop();
for(int i=0;i<n;i++)
{
int xx=(t.res*10+a[i])%m;
if(t.res*10+a[i]!=0&&xx==0)
{
return t.s+char('0'+a[i]);
}
if(mark[xx])
continue;
node g;
g.res=xx;
g.s=t.s+char('0'+a[i]);
q.push(g);
mark[xx]=1;
}
}
return ss+char('0');
} int main()
{
// freopen("input.txt","r",stdin);
while(scanf("%d%d",&m,&n)==2)
{
memset(mark,0,sizeof(mark));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(m==0)
{
printf("%d\n",m);
continue;
}
sort(a,a+n);
cout<<bfs()<<endl;
}
}

zoj1136 Multiple的更多相关文章

  1. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  2. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  3. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  4. SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“

    最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...

  5. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  6. JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher

    加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...

  7. scala - multiple overloaded alternatives of method bar define default arguments

    同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...

  8. 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?

    复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...

  9. 多文档上传(upload multiple documents)功能不能使用怎么办?

    问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...

随机推荐

  1. Excel中将时间格式转化成时间戳格式

    时间戳转成正常日期的公式:C1=(A1+8*3600)/86400+70*365+19其中A1表示当时的1249488000时间戳数值其中C1就是所需的日期格式,C1单元格属性改成日期格式就可以了.正 ...

  2. js操作iframe框架时应该屡清楚的一些概念

    1.获取iframe的window对象 存在跨域访问限制. iframeElement.contentWindow 兼容 2.获取iframe的document对象 存在跨域访问限制. chrome: ...

  3. iOS中通知的添加和移除

    我们都知道viewWillAppear:方法是在控制器的view将要显示的时候调用的,而viewWillDisappear:方法是在控制器的view将要隐藏的时候调用.很多时候我们根据自身需要将相关代 ...

  4. 新浪微博iOS示例,登录,获取个人信息

    1.导入第三方库和系统框架

  5. MySql高效分页SQL

    public string GetQuerySql(ITSPAreaQueryModel model, object state = null) { ); sqlBuilder.AppendForma ...

  6. servlet 之 返回json数据并显示

    //实体类import java.util.ArrayList; public class ObjectType { private String type; private ArrayList< ...

  7. C#传递参数大集合

    方法的参数是个值得特别注意的地方.方法的参数传递有四种类型:传值(by value),传址(by reference),输出参数(by output),数组参数(by array).传值参数无需额外的 ...

  8. AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表

    见归档项目ListViewDemo.zip. 基本步骤如下: 1.创建主布局文件,里面包含一个ListView元素. <RelativeLayout xmlns:android="ht ...

  9. make clean与make distclean的区别

    make clean仅仅是清除之前编译的可执行文件及配置文件. 而make distclean要清除所有生成的文件. Makefile 在符合GNU Makefiel惯例的Makefile中,包含了一 ...

  10. 今日哈工大刷推荐python脚本

    import httplib import random import time import urllib2 import re address = raw_input("Please i ...