给你一个数n 然后给你m个数,让你求一个最小的数,这个数是n的倍数,并且由题目中提供的m个数组成。用BFS。
此题可以用同余判断的方法来剪枝。
假如 A%X == B%X (设A<B)
那么 (A*10+Ki)%X==(B*10+Ki)%X
所以A,B之中我们只要取前面的A就行,因为题目要取最小的数,通过同余我们可以知道,A和B这两个数在末尾添加任何相同的数MOD X之后的余数都是一样的,因此对于比A大的数我们就没有必要去扩展了。B可以直接减掉。即对余数进行标记,已经出现过的余数(即当前得到的数与我之前已经得到的某个数同余),将不再扩展节点。
先对m个数字从小到大排序,这样保证我们前面平凑得到的数字是最小的。越早搜索到的符合题目要求的就是我们要的最小的数。
Multiple
Time Limit: 1000MS | Memory Limit: 32768K | |
Total Submissions: 5394 | Accepted: 1174 |
Description
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 that has no other digits besides X1,X2..XM (if such a multiple exists).
Input
The input has several data sets separated by an empty line, each data set having 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.
On the first line - the number N
On the second line - the number M
On the following M lines - the digits X1,X2..XM.
Output
For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.
An example of input and output:
An example of input and output:
Sample Input
22 3 7 0 1 2 1 1
Sample Output
110 <p>0</p><p> </p><p>附上我<span style="font-family: monospace; font-size: 14px; line-height: 26px; white-space: pre-wrap;">的代码:</span><pre name="code" class="cpp">#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<climits> #include<queue> #include<vector> #include<map> #include<sstream> #include<set> #include<stack> #include<utility> //#pragma comment(linker, "/STACK:102400000,102400000") #define PI 3.1415926535897932384626 #define eps 1e-10 #define sqr(x) ((x)*(x)) #define FOR0(i,n) for(int i=0 ;i<(n) ;i++) #define FOR1(i,n) for(int i=1 ;i<=(n) ;i++) #define FORD(i,n) for(int i=(n) ;i>=0 ;i--) #define lson num<<1,le,mid #define rson num<<1|1,mid+1,ri #define MID int mid=(le+ri)>>1 #define zero(x)((x>0? x:-x)<1e-15) using namespace std; const int INF =0x3f3f3f3f; const int maxn= 5000 ; //const int maxm= ; //const int INF= ; //typedef long long ll; //ifstream fin("input.txt"); //ofstream fout("output.txt"); //fin.close(); //fout.close(); //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); int n,m; vector<int> dig; bool vis[maxn+20]; struct Node { int dig; int re; int pre; Node (){ } Node (int d,int r,int p):dig(d),re(r),pre(p) { } } node[maxn+20]; void print(Node x) { if(x.pre!=0) print( node[x.pre] ); printf("%d",x.dig); } /* void print(int x) { if(node[x].pre!=0) print(node[x].pre); printf("%d",node[x].dig); }*/ void bfs() { Node st(0,0,-1); memset(vis,0,sizeof vis); int r=0,f=0; node[r++]=st; while(f<r) { Node st=node[f]; for(int i=0;i<m;i++) { if( st.pre==-1&& dig[i]==0 ) continue; int tmp=(st.re*10+dig[i])%n; if(vis[tmp] ) continue; if(tmp==0 ) { if(st.pre!= -1 ) print( st); printf("%d",dig[i]); putchar('\n'); return ; } vis[tmp]=1; Node now(dig[i] ,tmp , f); node[r++]=now; /* if(tmp==0) { print(r-1); putchar('\n'); return ; }*/ } f++; } puts("0"); } int main() { int x; while(~scanf("%d%d",&n,&m)) { dig.clear(); FOR1(i,m) { scanf("%d",&x); dig.push_back(x); } sort(dig.begin(),dig.end()); if(!n ) { puts("0") ;continue;} // 0 and 4999 (inclusively),单独处理,否则出现分母为零的情况。 bfs(); } return 0; }