POJ2635——The Embarrassed Cryptographer

来源:互联网 发布:大学网络课程网站 编辑:程序博客网 时间:2024/06/09 20:41
The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14148 Accepted: 3866

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 <= 10100 and 2 <= L <= 106. 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 10143 20667 20667 302573 302573 400 0

Sample Output

GOODBAD 11GOODBAD 23GOODBAD 31

Source

Nordic 2005


题目大意:

给出两个数 K 和 L,其中 K 为两个素数的乘积,问,是否有一个小于 L 的素数是 K 的因子,有,则输出 BAD x  ,其中 x 为最小的素数,没有输出  GOOD。

思路:

数据很大,只能用字符串来处理,每三位算一个计算单位,比如123456789  即 [123][456][789],

还要利用同余模定理,素数打表要用素数筛法,要不然会超时,

素数筛法:

bool primeflag[N+1];void primelist(){    memset(primeflag,true,sizeof(primeflag));    int i, j;    for( i = 2;i < N;i++ )    {        if( primeflag[i] )        {            prime[cnt++] = i;            for( j = i+i;j <= N;j += i )       //i的倍数都不是素数                primeflag[j] = false;        }    }}



//Source Code//Memory: 2000KTime: 1000MS//Language: G++Result: Accepted#include <iostream>#include <cmath>#include <cstring>using namespace std;const int N = 1000010;int prime[N+1], cnt = 0;bool primeflag[N+1];void primelist()           //素数打表{    memset(primeflag,true,sizeof(primeflag));    int i, j;    for( i = 2;i < N;i++ )    {        if( primeflag[i] )        {            prime[cnt++] = i;            for( j = i+i;j <= N;j += i )                primeflag[j] = false;        }    }}int check(char *s,int x){    int i, len, sum;    len = strlen(s);    sum = 0;    for( i = 0;i+3 < len;i += 3 )    {        sum = ( sum*1000 + (s[i]-'0')*100 + (s[i+1]-'0')*10 + (s[i+2]-'0') )%x;           //同余模定理    }    while( i<len )    {        sum = sum*10 + s[i++] - '0';    }    sum %= x;    return sum;}int main(){    primelist();    char s[110];    int L;    while( cin>>s>>L )    {        if( s[0]=='0'&&L==0 )            break;        int flag = 0;        for( int i = 0;i <cnt&&prime[i]<L;i++ )        {            if( !check(s,prime[i]) )            {                flag = 1;                cout<<"BAD "<<prime[i]<<endl;                break;            }        }        if( !flag )            cout<<"GOOD"<<endl;    }    return 0;}




0 0