记忆化搜索,因为要求最小的,肯定是从小到大,依次添加,那么通过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. 解决安装Visual Studio 2012后SQL Server 2008 远程过程调用失败的问题

    安装了Visual Studio 2012后,打开SQL Server 2008配置管理器,发现了一个问题.如下图 解决办法:

  2. Splash界面布局与代码实现(一)

    xml界面布局代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns ...

  3. js常用正则表达式汇总

    常用的前台正则表达式汇总. 1.手机号验证 手机格式以1开头,现有的手机格式一般为13.14.15.17.18等 var regMobile = /^1[34578]\d{9}$/; //或者为/^1 ...

  4. JS中表格的全选和删除要注意的问题

    在项目开发中,由于刚刚开始做项目,我对js还不是很精通,所以在用js对表格的全选和删除中遇到了不少问题,后来通过查找资料解决了,之后总结了一下关于js表格的全选和删除出现的一些问题,希望能帮助到大家. ...

  5. Oracle创建数据库、表、用户

    create tablespace south_knowledge logging datafile 'D:\TestDatabase\south_knowledge.dbf' size 10m au ...

  6. ps 网页配图设计

    网站配图设计 蒙太奇 品科软件---网页页面 1橡皮擦来画两图 容合 大橡皮擦擦出来自然 2图放到一个色块中 用剪贴蒙版 3调色阶 装饰下图片  矩形工具  形状  填充 画彩条 超出本框的怎么去掉多 ...

  7. <转>Python的内存泄漏及gc模块的使用分析

    一般来说在 Python 中,为了解决内存泄漏问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收.由于Python 有了自动垃圾回收功能,就造成了不少初学者误认为自己从此过上了好日子,不必再受内 ...

  8. SQL in Qt (一)

    Connecting to Databases To access a database with QSqlQuery or QSqlQueryModel, create and open one o ...

  9. PHP扩展开发之简单类开发

    接下来我们要用扩展的形式实现以下类(演示环境:linux.php-5.5.34-src) <?php class Person { private $_name; public function ...

  10. linux 安装nodejs

    首先去官网下载代码,这里一定要注意安装分两种,一种是Source Code源码,一种是编译后的文件. 我就是按照网上源码的安装方式去操作编译后的文件,结果坑了好久好久.     注意看好你下载的是什么 ...