Brute-force search matches by character until it meets or exceeds the specified range.
1 # include <cstdio> 2 # include <memory. h> 3 # include <queue> 4 # include <string> 5 using namespace STD; // if no value is added, the queue statement cannot pass 6 int N, C; 7 int visit [5001]; // mark the remainder N: 0-5000 8 int num [16]; 9 struct ss 10 {11 int A [502]; 12 INT Len; 13}; 14 15 int Mod (SS t) 16 {17 int TT = 0; 18 for (INT I = 0; I <t. len; I ++) 19 TT = (TT * C + T. A [I]) % N; 20 return tt; 21} 22 23 void print (SS t) 24 {25 for (INT I = 0; I <T. len; I ++) 26 {27 if (T. A [I] <= 9) 28 printf ("% d", T. A [I]); 29 else 30 printf ("% C", T. A [I]-10 + 'A'); 31} 32 printf ("\ n"); 33} 34 int BFS () 35 {36 memset (visit, 0, sizeof (visit); 37 ss s; 38 int modd; 39 queue <SS> q; 40 for (INT I = 1; I <16; I ++) 41 {42 if (Num [I]) 43 {44 s. A [0] = I; 45 s. len = 1; 46 modd = Mod (s); 47 If (! Modd) // The modulo is 0 48 {49 print (s); 50 return 1; 51} 52 else 53 {54 // Q. Push (s); 55 if (! Visit [modd]) 56 {57 visit [modd] = 1; 58 Q. Push (s); 59} 60} 61} 62} 63 while (! Q. empty () 64 {65 s = Q. front (); 66 Q. pop (); 67 for (INT I = 0; I <16; I ++) 68 {69 If (Num [I]) 70 {71 S. A [S. len] = I; 72 s. len ++; 73 modd = Mod (s); 74 if (! Modd) 75 {76 print (s); 77 return 1; 78} 79 else 80 {81 If (! Visit [modd] & S. len <499) 82 {83 visit [modd] = 1; 84 Q. push (s); 85} 86} 87 88 s. len --; // restore 89} 90} 91 92} 93 return 0; 94} 95 96 int main () 97 {98 freopen ("input.txt", "r ", stdin); 99 int T, M; 100 char ch [2]; // note that a space access '\ 0' 101 scanf ("% d ", & T); 102 while (t --) 103 {104 scanf ("% d", & N, & C); 105 scanf ("% d ", & M); 106 memset (Num, 0, sizeof (Num); 107 for (INT I = 0; I <m; I ++) 108 {109 scanf ("% s ", & Ch); 110 if (CH [0]> = '0' & Ch [0] <= '9 ') 111 num [CH [0]-'0'] = 1; 112 else113 num [CH [0]-'A' + 10] = 1; 114} 115 116 If (n = 0) 117 {118 If (Num [0]) 119 printf ("% d \ n", 0 ); 120 else121 printf ("give me the bomb please \ n"); 122} 123 else124 {125 bool B = BFS (); 126 If (! B) 127 printf ("give me the bomb please \ n"); 128} 129 130} 131 return 0; 132}