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
BAD 11
GOOD
BAD 23
GOOD
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,小于输出BAD和小的那个素数.
同余模定理:123%2=((((1%2)*10+2)%2)%10+3)%2=1=((01%2)*100+23)%2
对K进行千分制转化处理然后进行枚举操作
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxl=1010000;
int prime[maxl];
int c[334];
bool mod(int l,int* k,int s)
{
int ans=0,i;
for (i=l-1;i>=0;i--)
{
ans=(ans*1000+k[i])%s;
}
if (ans==0) return true;else return false;
}
void PrimeTable()
{
int i,t=0;
prime[0]=2;
for (i=3;i<=maxl;i+=2)
{
int j;
bool flag=true;
for (j=0;prime[j]*prime[j]<=i;j++)
{
if (i%prime[j]==0) {flag=false;break;}
}
if (flag) {t++;prime[t]=i;}
}
}
int main()
{
char k[1000];
int l;
PrimeTable();
while (cin>>k>>l && (l))
{
int i,t=0,len=strlen(k),lenc,c[334]={0};
lenc=(len+2)/3;
for (i=0;i<=len-1;i++)
c[(len-i+2)/3-1]=c[(len-i+2)/3-1]*10+k[i]-'0';
int flag=false;
while (prime[t]<l)
{
if (mod(lenc,c,prime[t]))
{cout<<"BAD "<<prime[t]<<endl;flag=true;break;}
t++;
}
if (!flag) cout<<"GOOD"<<endl;
}
}