poj 2635

来源:互联网 发布:weui.js 编辑:程序博客网 时间:2024/05/01 14:53
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 <= 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题目大意:有一个大数,是两个大素数的乘积,给定一个数,问你能否找到比它小的一个素数,可以被该大数整除附上代码:#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cmath>#include <cstring>using namespace std;#define maxn 1100000int isprime[maxn];void prime(){    int i,j;    isprime[0]=isprime[1]=0;//题目中说明最小的数为2    isprime[2]=1;//2肯定是素数啦!    for(i=3;i<maxn;i++)    {        isprime[i]=i%2;//数据太大,简化一下    }    int n;    n=int(sqrt(maxn));    for(i=3;i<=n;i++)    {        if(isprime[i])        {            for(j=i*2;j<maxn;j+=i)//如果i是素数,那么j肯定不是素数啦,j+=i是让j每次以i的倍数在递增,这样就把前面数据简化的那一步,是否为素数,重新定义了            {                isprime[j]=0;            }        }    }}int main(){    char s[1000];    int n,k,i,j,len;    while(cin>>s>>n)    {        if(s[0]=='0') break;        prime();        len=strlen(s);        for(k=2;k<n;k++)        {            if(isprime[k]==0)            {                continue;            }            int num=0;            for(i=0;i<len;i+=3)            {                int nums=0;                int t=1;                for(j=i;j<i+3&&j<len;j++)//转为千进制来算                {                    t*=10;                    nums=nums*10+(s[j]-'0');//每次把前面3位数算出                }                num=num*t+nums;//每次循环完后,t为1000,这样做,就是把大数整除,化为多次运算                num%=k;//找到那个素数            }            if(num==0)            {                printf("BAD %d\n",k);                break;            }        }            if(k==n)printf("GOOD\n");    }    return 0;}
0 0
原创粉丝点击