http://poj.org/problem?id=1465
Multiple
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. 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: Sample Input 22 3 7 0 1 2 1 1 Sample Output 110 0 Source |
题意:
给出一个整数N。和M个0~9的数。求N的一个最小倍数,且该数仅由这M个数构成,不存在则输出0。
分析:
如果存在终于的数,一定能够写成A1*10^(k-1)+A2*10^(k-2)+...+Ak,Ai属于给出的M个数的集合。k有可能非常大。64位整数也可能存不下。
注意到最后的结果是N的倍数,如果结果是X,则有X%N=0,注意到结果的多项式形式,显然能想到使用同余定理。
我们须要从1位扩展到k位(当然要一步步来)。能够用BFS,用余数来记录状态(仅仅须要第一个),这样状态不超过N个。一旦余数为0,我们须要的结果就出来了。
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<ctime> #include<cctype> #include<cmath> #include<string> #include<cstring> #include<stack> #include<queue> #include<list> #include<vector> #include<map> #include<set> #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define PI 3.1415926535897932384626 #define eps 1e-10 #define maxm #define maxn using namespace std; int X[10]; int n,m; int q[5555]; int st[5555]; bool __hash[5555]; struct __node { int x,mod,fir; }node[5555]; void write(int x) { int top=-1; for (;~x;x=node[x].fir) st[++top]=node[x].x; while (top>=0) printf("%d",st[top--]); puts(""); } void bfs() { int f=0,r=-1,cnt=0; if (!n) { printf("%d ",0); return ; } memset(__hash,0,sizeof __hash); for (int i=0;i<m;i++) { if (!X[i]) continue; int mod=X[i]%n; if (!mod) { printf("%d ",X[i]); return ; } if (__hash[mod]) continue; __hash[mod]=true; node[cnt]=(__node){X[i],mod,-1}; q[++r]=cnt; cnt++; } while (f<=r) { int x=q[f++]; for (int i=0;i<m;i++) { int mod=(node[x].mod*10+X[i])%n; if (__hash[mod]) continue; __hash[mod]=true; node[cnt]=(__node){X[i],mod,x}; q[++r]=cnt; if (!mod) { write(cnt); return ; } cnt++; } } printf("0 "); } int main() { #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE while (~scanf("%d",&n)) { scanf("%d",&m); for (int i=0;i<m;i++) scanf("%d",X+i); sort(X,X+m); bfs(); } return 0; }