ZOJ 1136 Multiple (BFS + number of numbers with more than one pruning search)

Source: Internet
Author: User

ZOJ problem Set-1136
Multiple
Time Limit:10 Seconds Memory limit:32768 KB

A program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits x1,x2. XM (at least one), finds the smallest strictly positive multiple of N there is no other digits besides x1,x2. XM (if such a multiple exists).

The input file has several data sets separated by a empty line, and each data set has the following format:

On the first line-the number N
On the second line-the number M
On the following M lines-the digits x1,x2. Xm.

For each data set, the program should write to standard output in a single line the multiple, if such a multiple exists, a nd 0 otherwise.

An example of input and output:

Input

22
3
7
0
1

2
1
1

Output

110
0
Source:southeastern Europe 2000
Submit Status
Mainly pruning: The pruning of number theory, which is explained directly by examples, such as 36%24==12==60%24, then 36*****%24==60******%24
such as 368%24==608%24, so in the search, if you have searched for 36%24==12, then you can skip the 60 o'clock

#include <cstdio>#include <cstring>#include <iostream>#include <set>#include <map>#include <stack>#include <algorithm>#include <queue>using namespace STD;#define CL (A, B) memset (A,b,sizeof (a) )#define LL Long LongConst intmaxn=100002;Const intinf=9999999;intN,m;intA[MAXN];intMOD[MAXN];intGetMode (strings) {intLen=s.size ();intans=0; for(intI=0; i<len;i++) {ans= (ans*Ten+s[i]-' 0 ')%n; }returnAns;}stringBFS () {cl (mod,0); Queue<string>Q for(intI=0; i<m;i++) {//Initialize the start of the queueQ.push (string(1, a[i]+' 0 '));//string (Number,char) constructor} while(!q.empty ()) {stringS=q.front (); Q.pop ();if(s!="0"&&getmode (s) = =0){returnS } for(intI=0; i<m;i++) {stringTmp=s;if(tmp=="0"&&a[i]==0)Continue;//Special award to prevent the appearance of 00000 ... The situationtmp+=string(1, a[i]+' 0 ');intD=getmode (TMP);if(mod[d]++)Continue;//appear congruence, skip directlyQ.push (TMP); }    }return "0";}intMain () { while(~scanf("%d", &n)) {scanf("%d", &m); for(intI=0; i<m;i++) {scanf("%d", &a[i]); }if(n==0){printf("0\n");Continue; } sort (a,a+m);printf("%s\n", BFS (). C_STR ()); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 1136 Multiple (multiple of BFS + number of numbers in the same remainder pruning search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.