# POJ 2635 The Embarrassed Cryptographer (同余问题)

### Description

The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of users, which is now in use in his company. The cryptographic keys are created from the product of two primes, and are believed to be secure because there is no known method for factoring such a product effectively.

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
GOOD
GOOD
BAD 31

### 思路

(a+b)%c=(a%c+b%c)%c

(a+b)%c=(a%c+b%c)%c

于是便间接得到了123%3=0 ，每一次运算中乘号的前面都是上一次运算的结果。

题目中有说明K的范围是[4,10^100]，打表出的素数个数大概有8w，本来分析的用十进制应该不会超时，但是提交之后发现果然超时了。无奈只能百度看到别人用的千进制存储，于是我也试了一下，AC~

用千进制存储主要是为了减少循环的次数，可以把原数长度缩短3倍，毕竟 1000=10^3

### AC代码

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;

const int Range = 1e6+100;
int prime[Range+1];
int kt[110];  //千进制存储的k
int l;
void init() //素数打表
{
int pNum=0;
prime[pNum++]=2;

for(int i=3; i<=Range; i+=2) //奇偶法
{
bool flag=true;
for(int j=0; prime[j]*prime[j]<=i; j++) //根号法+递归法
if(!(i%prime[j]))
{
flag=false;
break;
}
if(flag)prime[pNum++]=i;
}
return;
}

bool mod(int *k,int p,int len)  //判断高精度k是否可以整除p
{
int s=0;
for(int i=len-1; i>=0; i--) //同余模定理
s=(s*1000+k[i])%p;
return s==0;
}

int main()
{
init();
char k[110];
while(~scanf("%s%d",k,&l)&&l)
{
memset(kt,0,sizeof(kt));
int len=strlen(k);
for(int i=0; i<len; i++)    //十进制与千进制差1e3，因此在转换的时候直接每三位作为千进制的一位
{
int pkt=(len+2-i)/3-1;  //+2是为了在/3的时候保证上取整
kt[pkt]=kt[pkt]*10+(k[i]-'0');
}
int lent=(len+2)/3; //千进制数的位数
bool flag=true;
int pmin=0;
while(prime[pmin]<l)
{
if(mod(kt,prime[pmin],lent))
{
flag=false;
}