The basic idea:
Still stuck in the modeling, personally I feel that BFS must pay attention to what aspects:
1. The state must find a possible expansion of the state, it can be iterated;
2. boundary, this problem e.g., if a broad search, that is 0 modulo, if using a deep search, that there is no overflow, longlongs up to 19;
But this subject there is a doubt, is the memory gauge problem;
I see two hundred own thought Tarsus types of division problems, but the memory of the explosion, according to monitoring the highest 63MB, or can not figure out what was happening;
key point:
no;
Attach code in question:
#include <the iostream> #include <String> #include <Vector> #include <Queue> the using namespace STD; const int MAXN = 101; BOOL devide (S String, int NUM) { if inspection is divisible // int r = 0; for (int I = 0; I <s.size (); I ++) { // for large numbers in front of the bulk; int TEMP = (S [I] - '0') 10 + * R & lt; R & lt TEMP = NUM%; } IF (R & lt == 0) return to true; the else return to false; } String BFS (n-int) { String = S ". 1"; Queue <String> Q; q.push (S); the while (Q! .empty ()) { String = q.front STR (); q.pop (); for (int i = 0; i < 2; i++) { if (i == 0) { str += "0"; } else { str += "1"; } if (devide(str, n)) return str; q.push(str); } } } int main() { int n; while (cin >> n) { if (n == 0) return 0; cout << bfs(n) << endl; } return 0; }