The Embarrassed Cryptographer
Description ![]() 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 Source |
题目大意:
给出两个数 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: 2000K Time: 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;
}