poj2635(素数表)

来源:互联网 发布:邱少云违背生理学知乎 编辑:程序博客网 时间:2024/06/05 16:02
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 <= 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 10143 20667 20667 302573 302573 400 0
Sample Output
GOODBAD 11GOODBAD 23GOODBAD 31
//Memory Time//624K  1235MS /*此题借鉴的http://blog.csdn.net/lyy289065406/article/details/6648530这是原作者地址*/#include<iostream>#include<string.h>using namespace std;const int Range=1000100;  //打表不能只打到100W,素数表中最大的素数必须大于10^6int Kt[10000];  //千进制的Kint L;int prime[Range+1];/*素数组打表*/void PrimeTable(void){int pNum=0;prime[pNum++]=2;for(int i=3;i<=Range;i+=2)  //奇偶法{bool flag=true;for(int j=0;prime[j]*prime[j]<=i;j++)  //根号法+递归法if(!(i%prime[j])){flag=false;break;}if(flag)prime[pNum++]=i;}return;}/*高精度K对p求模,因数检查(整除)*/bool mod(const int* K,const int p,const int len){int sq=0;for(int i=len-1;i>=0;i--)  //千进制K是逆序存放sq=(sq*1000+K[i])%p;  //同余模定理if(!sq)   //K被整除return false;return true;}int main(void){PrimeTable();char K[10000];while(cin>>K>>L && L){memset(Kt,0,sizeof(Kt));int lenK=strlen(K);for(int i=0;i<lenK;i++)  //把K转换为千进制Kt,其中Kt局部顺序,全局倒序{                      //如K=1234567=[  1][234][567] ,则Kt=[567][234][1  ]int pKt=(lenK+2-i)/3-1;Kt[pKt]=Kt[pKt]*10+(K[i]-'0');}int lenKt=(lenK+2)/3;bool flag=true;int pMin=0;  //能整除K且比L小的在prime中的最小素数下标while(prime[pMin]<L)  //枚举prime中比L小的素数{if(!mod(Kt,prime[pMin],lenKt)){flag=false;cout<<"BAD "<<prime[pMin]<<endl;break;}pMin++;}if(flag)cout<<"GOOD"<<endl;}return 0;}

0 0