The Embarrassed Cryptographer(POJ--2635

来源:互联网 发布:数据库报表开发帆软 编辑:程序博客网 时间:2024/05/16 08:38

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.
题意:多组输入。输入一个K和一个L,K是由两个素数相乘得来的,如果这两个素数中最小的那个数小于L则输出"BAD p",p即那个最小的素数;如果连最小的那个素数都大于等于L,则直接输出"GOOD"。
思路:首先就是要打一个2~10^6的素数表,从2~L(不包括L)依次枚举每个素数,看是否能被K整除,如果能则说明K有小于L的素数因子,如果不能则说明K的素数因子都大于等于L。因为L的最大极限才10^6,所以打素数表是没问题的。要解决该问题还有两点要考虑到。第一点:K的存储。K的最大极限是10^100,已经超过long long 范围了,所以K只能用字符串来存储。第二点:判断是否能整除。由于K的范围很大且其是由字符串存储所以不能直接对素数因子取余,此时要用到同余模取余的算法,即把K均匀分成若干份,分别对于素数因子取余,详细操作请看代码。

Sample Input

143 10143 20667 20667 302573 302573 400 0

Sample Output

GOODBAD 11GOODBAD 23GOODBAD 31

#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;long long int bes=10000000000,num[2000000];  //将K分成了10^10份,分别存储在数组num[]中,则每一份的最大极限是10^10,以防超范围所以开的long long 型数组bool  vis[2000000];int main(){    memset(vis,0,sizeof(vis));            //打素数表    int m=sqrt(1000005+0.5);    for(int i=2; i<=m; i++)        if(!vis[i])            for(int j=i*i; j<=1000005; j+=i)                vis[j]=1;    char k[120];    int l;    while(~scanf("%s %d",k,&l))    {        if(k[0]=='0'&&l==0)        break;        memset(num,0,sizeof(num));        int len=strlen(k),ss=0;        for(int i=0; i<len; i+=10)                  //拆分K并转换成整数型        {            long long cnt=0;                                       for(int j=i; j<i+10&&j<len; j++)                cnt=cnt*10+k[j]-'0';            num[ss++]=cnt;        }//        for(int i=0; i<ss; i++)//            printf("%lld \n",num[i]);        bool flag=true;        for(int i=2; i<=l; i++)        {            if(!vis[i])                                      //如果i是素数            {                long long int sum=0;           //因为每10位是一部分,所以在同余模取余时当需要加上后边部分时,当前部分要乘以10^10,而如果是在代码中直接乘以10^10是不可以的,因为在函数里直接运用一个常数这个常数会被默认为是int 型,那么10^10就会超int型,所以要在主函数外定于一个long long型常量来记录10^10                for(int j=0; j<ss; j++)                    sum=(sum*bes+num[j])%i;                if(sum==0)                {                    printf("BAD %d\n",i);                    flag=false;                    break;                }            }        }        if(flag)            printf("GOOD\n");    }    return 0;}<strong></strong>


0 0
原创粉丝点击