The Embarrassed Cryptographer
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13274 | Accepted: 3608 |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/a64f64479d9437bf747b1ad5425cdc17.jpeg)
What Odd Even did not think of, was that both factors in a key should be large, not just their product. It is now possible that some of the users of the system have weak keys. In a desperate attempt not to be fired, Odd Even secretly goes through all the users keys, to check if they are strong enough. He uses his very poweful Atari, and is especially careful when checking his boss' key.
Input
The input consists of no more than 20 test cases. Each test case is a line with the integers 4 <= K <= 10
100 and 2 <= L <= 10
6. K is the key itself, a product of two primes. L is the wanted minimum size of the factors in the key. The input set is terminated by a case where K = 0 and L = 0.
Output
For each number K, if one of its factors are strictly less than the required L, your program should output "BAD p", where p is the smallest factor in K. Otherwise, it should output "GOOD". Cases should be separated by a line-break.
Sample Input
143 10 143 20 667 20 667 30 2573 30 2573 40 0 0
Sample Output
GOOD BAD 11 GOOD BAD 23 GOOD BAD 31给定一个由两个大素数相乘的数K,在给一个整数L,判断构成K的素数是否存在小于L的,若存在输出这个数,若不存在,输出GOOD
#include <iostream>
#include <cstring>
#include <cstdio>
#define LL long long
using namespace std;
char K[1010];
int L, Kt[1100];
const int maxn = 1e6+100;
int Prim[maxn];
int top,len;
bool vis[maxn];
void PrimTable()
{
int top = 0;
Prim[top++] = 2;
for(int i=3;i<=maxn; i+=2)
{
bool flag = false;
for(int j=0; Prim[j] * Prim[j] <= i; j++)
{
if(i % Prim[j] == 0)
{
flag = true;
break;
}
}
if(!flag)
Prim[top++] = i;
}
}
bool MOD(int mod)
{
LL ans = 0;
for(int i = len - 1; i >=0; i--)
{
ans = (ans * 10000 + Kt[i]) % mod;
}
if(ans)
return true;
return false;
}
int main()
{
PrimTable();
while(cin>>K>>L && L)
{
top = 0;
memset(Kt, 0, sizeof(Kt));
len = strlen(K);
for(int i=0; i<len; i++)
{
int pos = (len - i + 3) / 4 - 1;
Kt[pos] = Kt[pos] * 10 + (K[i] - '0');
}
len = (len + 3) / 4;
bool flag = true;
int pos = 0;
while(Prim[pos] < L)
{
if(!MOD(Prim[pos]))
{
printf("BAD %d\n",Prim[pos]);
flag = false;
break;
}
pos++;
}
if(flag)
printf("GOOD\n");
}
return 0;
}