Given a large number, this number is obtained by multiplying two prime numbers. Now, you can ask whether there is a prime number smaller than the given number in the decomposition of this large number. Since the given number L is less than 10 ^ 6, we can create a table to 10 ^ 6. It is better to make it bigger (the smallest prime number larger than 10 ^ 6 ), in this way, you can easily jump out of the loop.
Next, we will try to divide all prime numbers whose number is smaller than l to see if the remainder is zero. Here we use the modulo operation of a large number, which is a big number pair of an int type. The process is like this. We can put a number (ABC... XYZ) as 10 * (ABC... XY) + z --> 10*(10 * (ABC... x) + Y) + Z. Keep doing this. We can start with modulo from the innermost part.
The Code is as follows:
#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define eps (1e-6)#define MAXN 1100000using namespace std;bool p[MAXN+5];int rec[90000], idx;void getprime() { idx = -1; for (int i = 2; i <= MAXN; ++i) { if (!p[i]) { rec[++idx] = i; } for (int j = 0; rec[j]*i <= MAXN; ++j) { p[rec[j]*i] = 1; if (i % rec[j] == 0) { break; } } }}int Mod(char key[], int L) { int len = strlen(key+2), ret = 0, beg = 2; if (len % 3 == 1) { beg = 0; } else if (len % 3 == 2) { beg = 1; } len += 2; for (int i = beg; i < len; i+=3) { int temp = (key[i]-'0')*100+(key[i+1]-'0')*10+(key[i+2]-'0'); ret = ((long long)ret*1000 + temp) % L; } return ret;}int main() { getprime(); int L, ret, flag; char key[150] = {'0', '0'}; while (scanf("%s %d", key+2, &L), (key[2]-'0') | L) { flag = 0; for (int i = 0; rec[i] < L; ++i) { if (Mod(key, rec[i]) == 0) { ret = rec[i]; flag = 1; break; } } if (flag) { printf("BAD %d\n", ret); } else { puts("GOOD"); } } return 0; }