is actually a simple search problem.
Small to large +BFS can guarantee a minimum of O (n) complexity.
#include <cstdio>#include<iostream>#include<fstream>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<queue>#defineRep (I, L, R) for (int i=l; i<=r; i++)#defineDown (i, L, R) for (int i=l; i>=r; i--)#defineMAXN 5678#defineMAX 1<<30using namespacestd;intN, C, M, bn[maxn][509];Chars[5];BOOLb[ -];queue<int>Q;voidInit () {Rep (I,0, the) B[i] =0; Rep (I,0, maxn-1) bn[i][0] =0; while(!q.empty ()) Q.pop ();}intMain () {intT scanf"%d", &t); while(t--) {Init (); scanf ("%d%d%d", &n, &c, &m); Rep (I,1, M) {scanf ("%s", s); if(s[0] <='9') b[s[0]-'0'] =1; Elseb[s[0]-'A'+Ten] =1; } if(n = =0) { if(b[0]) printf ("0\n");Elseprintf"give me the bomb please\n"); Continue; } Rep (I,1, the)if(B[i] &&!bn[i%n][0]) {bn[i%n][0] =1; bn[i%n][1] = i; Q.push (i%N); } while(!q.empty () &&!bn[0][0]) { intx =Q.front (); Q.pop (); if(bn[x][0] == -)Continue; Rep (I,0, the)if(B[i] &&!bn[(x*c+i)%n][0]) { inty = (x*c+i)%N; Rep (J,1, bn[x][0]) bn[y][j+1] = Bn[x][j]; bn[y][1] = i; bn[y][0] = bn[x][0]+1; Q.push (y); } } if(bn[0][0]) {Down (i, bn[0][0],1)if(bn[0][i] <=9) printf ("%c", bn[0][i]+'0');Elseprintf"%c", bn[0][i]-Ten+'A'); printf"\ n"); } Elseprintf"give me the bomb please\n"); }}
View Code
HDU-1226 Super Password