记忆化搜索,因为要求最小的,肯定是从小到大,依次添加,那么通过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. 【web开发--js学习】functionName 如果是一个属性值,函数将不会被调用

    <html> <head> <meta http-equiv="Content-Type" Content="text/html; char ...

  2. Windows环境搭建Red5流媒体服务器指南(转)

    Windows环境搭建Red5流媒体服务器指南 Windows环境搭建Red5流媒体服务器指南 测试环境:Windows 7 一.   下载安装程序 red5-server 下载地址 https:// ...

  3. 2013腾讯编程马拉松初赛第〇场(3月20日)湫湫系列故事——植树节 HDOJ 4503

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4503 思路:hint from a GOD-COW. 将每一个人模拟成图的一个点,两点连线当且仅当两人是朋 ...

  4. Java IO之序列化

    序列化机制是Java语言内建的一种对象持久化方式,可以很容易的在JVM中的活动对象和字节数组之间转换.它的一个重要用途就是远程方法调用的时候,用来对开发人员屏蔽底层实现细节(远端的开发人员不知道这个对 ...

  5. IntelliJ IDEA 14使用笔记

    由eclipse到IDEA IDEA与eclipse的区别: IDEA的project对应eclipse的workspace: IDEA的module对应eclipse的project的. 所以要想在 ...

  6. jquery中attr()与prop()函数用法实例详解(附用法区别)

    本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...

  7. 异常处理 - PHP手册笔记

    PHP代码中所产生的异常可被throw语句抛出,并被catch语句捕获.需要进行异常处理的代码都必须放入try代码块内,每一个try至少要有一个与之对应的catch.当一个异常被抛出时,所在代码块后面 ...

  8. position:absolute实现垂直居中

    一些图标通常要垂直居中 如下所示: 而css中没有直接的样式.需要我们自己调试. 我用了position:absolute;来实现. 要想使得position:absolute;有效,它的父元素必须也 ...

  9. windows窗口分析,父窗口,子窗口,所有者窗口

    (本文尝试通过一些简单的实验,来分析Windows的窗口机制,并对微软的设计理由进行一定的猜测,需要读者具备C++.Windows编程及MFC经验,还得有一定动手能力.文中可能出现一些术语不统一的现象 ...

  10. 关于GPS偏移的基础知识

    转载地址 我们平时用到的地球坐标系统,叫做WGS84坐标,国家保密插件,也叫做加密插件或者加偏或者SM模组,其实就是对真实坐标系统进行人为的加偏处理,按照几行代码的算法,将真实的坐标加密成虚假的坐标, ...