zoj1136 Multiple
记忆化搜索,因为要求最小的,肯定是从小到大,依次添加,那么通过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的更多相关文章
- 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 ... 这个错误是因为有两个相 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [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 ...
- SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“
最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- 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") ...
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- 多文档上传(upload multiple documents)功能不能使用怎么办?
问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...
随机推荐
- 使用react-native做一个简单的应用-03欢迎界面
Android和iOS的欢迎界面是不一样的,在iOS中有一个默认的欢迎界面,而Android则需要自己写.因此我就分开说一下这两个平台的欢迎界面的搭建.下面先看一下实现效果: Android: iOS ...
- PHP学习笔记十三【二维数组】
<?php //二维数组 $arr=array(array(1,2,3),array(4,5,6)); $arr1[0]=array(12,34,65); $arr1[1]=array(34,6 ...
- UVA 11214 Guarding the Chessboard
题意: 皇后防御的范围是他所在横.竖.对角线,地图上的#为可以放旗子的地方.问最少放几个皇后能防守所有#. 分析: vis数组开4维,对应行.列.主对角线.副对角线 代码: #include < ...
- [zz]npm安装错误解决方法
错误: npm ERR! at Object.parse (native) npm ERR! at Packer.readRules (/usr/local/lib/node_modules/npm/ ...
- Citrix 服务器虚拟化之十一 Xenserver管理vApps
Citrix 服务器虚拟化之十一 Xenserver管理vApps vApps是把几个业务相关的虚拟机作为一个单一实体管理,把vApps中的虚拟机的称为Application.启动vApps时其中包 ...
- Struts2 校验
Struts2校验格式: actionName-methodName-invalidation.xml 该配置文件必须和action类在同一个包下. <?xml version="1 ...
- Oracle EBS-SQL (INV-12):检查待定事物处理1.sql
/*未加工物料*/ update inv.mtl_material_transactions_temp set process_flag='Y', LOCK_FLAG='N', TRANSACTION ...
- 【Xamarin 挖墙脚系列:Xamarin SDK开源了................】
在前不久举行的 Build 2016 开发者大会上,微软宣布它收购的 Xam ...
- git 拉取远程分之到本地
git checkout -b newbranch_name --track origin/feature/newbranch_name 如果遇到类似: fatal: git checkout: up ...
- 线性表的Java实现
一.概念 对于常用的数据结构,可分为线性结构和非线性结构,线性结构主要是线性表,非线性结构主要是数和图.当n>0时,表可表示为:(a0,a1,a2,a3,…an) 1. 线性表的特征: 1.存在 ...