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.
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.
143 10 143 20 667 20 667 30 2573 30 2573 40 0 0
GOOD BAD 11 GOOD BAD 23 GOOD BAD 31
说是素数表,其实是,输入的大数k,是由两个素数组成,输入l,如果最小的素数比l小,就输出BAD P 否则输出GOOD,我只会素数表,思路是看题解才知道的,算出素数表后,再判断k能否被素数(从最小开始)整除,这要用到大数同余。整除使判断与l的大小关系,大数同余:12300000%3,可以1%3=1,(1*10+2)%3=0,(0*10+3)%3=0,故12300000%3=0,这里因为k很大,所以转换为千进制,过程也是类似的
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int prime[1001000]; int khex[10001]; char k[1000]; void primenum(); int judge(int a,int b); int main() { primenum(); int l,len,i,temp,flag; while(cin>>k>>l&&l) { memset(khex,0,sizeof(khex)); len=strlen(k); for(i=0;i<len;i++) { temp=(len+2-i)/3-1; khex[temp]=khex[temp]*10+(k[i]-'0');//转千进制 } len=(len+2)/3; flag=1; for(i=0;prime[i]<l;i++) { if(!judge(prime[i],len)) { flag=0; break; } } if(flag) { cout<<"GOOD"<<endl; } else { cout<<"BAD "<<prime[i]<<endl; } } } void primenum() { int i,j,num=0;//算素数 prime[num++]=2; int flag; for(i=3;i<1001000;i+=2) { flag=1; for(j=0;prime[j]*prime[j]<=i;j++) { if(i%prime[j]==0) { flag=0; break; } } if(flag) { prime[num++]=i; } } } int judge(int a,int b) { int sum=0,i; for(i=b-1;i>=0;i--)//同余定理 { sum=(sum*1000+khex[i])%a; } return sum; }