POJ 2635 The Embarrassed Cryptographer

来源:互联网 发布:怎样重新激活淘宝店铺 编辑:程序博客网 时间:2024/05/16 06:13
The Embarrassed Cryptographer
Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 11984Accepted: 3196
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 10
143 20
667 20
667 30
2573 30
2573 40

0 0


Sample Output


GOOD
BAD 11
GOOD
BAD 23
GOOD

BAD 31


不错的题!!!当时想的时候不知道怎么打这么长的素数表,看了小YOU的感觉自己素数打表都这么弱啊!!

题意就是一个由两个素数乘得大数,求其中最小的那个素数是否小于给出的数(记住是小于,不能等于,但愿被坑的人变少)。

用了高进制避免超时!!


AC代码如下:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#define M 1000050using namespace std;int prime[M];int tt;void isp()//这个打表算涨姿势了!!!!{    tt=0;    int i,j;    prime[tt++]=2;    for(i=3;i<M;i+=2)    {        int flag=1;        for(j=0;prime[j]*prime[j]<=i;j++)        {            if(i%prime[j]==0)            {                flag=0;                break;            }        }        if(flag)            prime[tt++]=i;    }    return ;}char s[205];int kk[10005];int wss;void conv()//转型{    int i,j;    int l=strlen(s);    memset(kk,0,sizeof kk);    int flag=0;    int bj=0;    int cx=l%3;    for(i=0,j=0;i<l;i++)    {        kk[j]=kk[j]*10+s[i]-'0';        flag++;        if(bj==0&&flag==cx)        {            bj=1;j++;flag=0;        }        if(flag==3)        {j++;flag=0;}    }    wss=j-1;    return;}int main(){    int n;    int i,j;    isp();    while(~scanf("%s%d",s,&n)&&s[0]!=0&&n!=0)    {        int an,ans;        conv();        int flag=0;        for(i=0;i<tt;i++)        {            if(prime[i]>=n)                break;            an=0;            for(j=0;j<=wss;j++)            {                an=(an*1000+kk[j])%prime[i];//同余定理            }            if(an==0)                {flag=1;ans=prime[i];break;}        }        if(flag)            printf("BAD %d\n",ans);        else printf("GOOD\n");    }    return 0;}


0 0
原创粉丝点击