ZOJ 1136 Mutiple (同余定理判重)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #define sc(x) scanf("%d",&x) 7 #define pf(x) printf("%d\n",x) 8 #define P printf("\n") 9 #define CL(x,y) memset(x, y, sizeof(x)) 10 #define FOR(i,x,v) for(int i=x; i<=v; i++) 11 using namespace std; 12 struct node 13 { 14 int num; 15 string str; 16 }; 17 const int MAX = 5002; 18 int N, M, arr[10], used[MAX], i, temp; 19 queue <node> Q; 20 void BFS(); 21 int main() 22 { 23 int i; 24 while(sc(N)!=EOF) 25 { 26 sc(M); 27 FOR(i, 0, M-1) 28 sc(arr[i]); 29 sort(arr, arr+M); 30 CL(used, 0); 31 while(!Q.empty ()) 32 Q.pop (); 33 if(N==0) pf(0); 34 else 35 BFS(); 36 while(!Q.empty()) 37 Q.pop (); 38 // P; 39 } 40 return 0; 41 } 42 void BFS() 43 { 44 node fir, cur, next; 45 fir.num = 0; 46 fir.str = ""; 47 Q.push(fir); 48 while(!Q.empty()) 49 { 50 cur = Q.front(); 51 Q.pop(); 52 // if(cur.num >= N && cur.num % N == 0) 53 // { 54 // pf(cur.num); 55 // return ; 56 // } 57 FOR(i, 0, M-1) 58 { 59 temp = cur.num*10 + arr[i]; 60 if(!temp) continue; 61 if(temp%N==0) 62 { 63 cout << cur.str << (char)(arr[i]+'0') << endl; 64 return ; 65 } 66 if(!used[temp%N]) 67 { 68 used[temp%N] = 1; 69 next.num = temp%N; 70 next.str = cur.str + (char)(arr[i]+'0'); 71 Q.push(next); 72 } 73 } 74 } 75 pf(0); 76 }
虽然错了10次,还是不清楚到底怎么了。。。等几天才懂了那个string 的含义