Question meaning:
Give N, give nn a jj number, ask the minimum number of less than 500 digits composed of these numbers jj number is a positive integer multiple of the decimal number N.
Solution:
BFS.
Because N <= 5000, the remainder is used to determine the weight.
Code:
<SPAN style = "COLOR: #330033; FONT-SIZE: 18px ">#include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define eps 1e-6 # define INF 0x1f1f1f1f # define PI acos (-1.0) # define ll _ int64 # define lson l, m, (rt <1) # define rson m + 1, r, (rt <1) | 1 // # pragma comment (link Er, "/STACK: 10241000000,1024000000") using namespace std;/* freopen ("data. in "," r ", stdin); freopen (" data. out "," w ", stdout); */bool vis [0, 5500]; char save [20]; int n, jj, nn; struct Inf {string s; int m, len ;}; bool cmp (char a, char B) {return int (a) <int (B);} void bfs () {memset (vis, false, sizeof (vis); queue <Inf> myq; for (int I = 1; I <= nn; I ++) {if (save [I] = '0') continue; Inf tmp; tmp. s = "", tmp. len = 1; tmp. s + = save [I]; tmp. M = (save [I]> = '0' & save [I] <= '9 ')? (Save [I]-'0') :( 10 + save [I]-'A') % n; if (tmp. m = 0) // special handling of only one character {cout <tmp. s <endl; return;} vis [tmp. m] = true; myq. push (tmp);} while (! Myq. empty () {Inf tt = myq. front (); myq. pop (); for (int I = 1; I <= nn; I ++) {int aa = (save [I]> = '0' & save [I] <= '9 ')? (Save [I]-'0') :( 10 + save [I]-'A'); int t = (tt. m * jj + aa) % n; if (! T) // {cout <tt. s; printf ("% c \ n", save [I]); return;} if (vis [t]) continue; vis [t] = true; Inf cur; cur. len = tt. len + 1; if (cur. len> = 500) // more than 500-bit continue; cur. s = tt. s + save [I]; cur. m = t; myq. push (cur) ;}} printf ("give me the bomb please \ n"); return ;}int main () {int t; scanf ("% d ", & t); while (t --) {scanf ("% d", & n, & jj, & nn); getchar (); // fflush (stdin); // because of this sentence, wa for one morning (int I = 1; I <= nn; I ++) scanf ("% s ", save + I); // save [nn + 1] = '\ 0'; sort (save + 1, save + nn + 1, cmp ); if (n = 0) {if (save [1] = '0') printf ("0 \ n "); else printf ("give me the bomb please \ n"); continue;} // printf ("% s \ n", save + 1); bfs ();} return 0 ;}</SPAN>