The Embarrassed Cryptographer
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13757 | Accepted: 3750 |
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
Source
大致题意:
给定一个大数K,K是两个大素数的乘积的值。
再给定一个int内的数L
问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数。
解题思路:
首先对题目的插图表示无语。。。
高精度求模+同余模定理
1、 Char格式读入K。把K转成千进制Kt,同时变为int型。
把数字往大进制转换能够加快运算效率。若用十进制则耗费很多时间,会TLE。
千进制的性质与十进制相似。
例如,把K=1234567890转成千进制,就变成了:Kt=[ 1][234][567][890]。
为了方便处理,我的程序是按“局部有序,全局倒序”模式存放Kt
即Kt=[890][567][234][1 ] (一个中括号代表一个数组元素)
2、 素数打表,把10^6内的素数全部预打表,在求模时则枚举到小于L为止。
注意打表不能只打到100W,要保证素数表中最大的素数必须大于10^6,否则当L=100W且K为GOOD时,会因为数组越界而RE,这是因为越界后prime都是负无穷的数,枚举的while(prime[pMin]<L)循环会陷入死循环
3、 高精度求模。
主要利用Kt数组和同余模定理。
例如要验证123是否被3整除,只需求模124%3
但当123是一个大数时,就不能直接求,只能通过同余模定理对大数“分块”间接求模
具体做法是:
先求1%3 = 1
再求(1*10+2)%3 = 0
再求 (0*10+4)% 3 = 1
那么就间接得到124%3=1,这是显然正确的
而且不难发现, (1*10+2)*10+4 = 124
这是在10进制下的做法,千进制也同理,*10改为*1000就可以了
#include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 1000100 int L,prime[N+1];//打表不能只打到100W,素数表中最大的素数必须大于10^6 int Kt[N/100];//千进制的K char K[N/100]; void init(){ int pnum=0;/*素数组打表*/ prime[pnum++]=2; for(int i=3;i<=N;i+=2){//奇偶法 bool flag=1; for(int j=0;prime[j]*prime[j]<=i;j++){//根号法+递归法 if(i%prime[j]==0){ flag=0;break; } } if(flag) prime[pnum++]=i; } } bool mod(const int *K,const int p,const int len){ int ans=0;/*高精度K对p求模,因数检查(整除)*/ for(int i=len-1;i>=0;i--){//千进制K是逆序存放 ans=(ans*1000+K[i])%p;//同余模定理 } if(!ans) return 0;//K被整除 return 1; } int main(){ init(); while(cin>>K>>L){ if(!L) break; memset(Kt,0,sizeof(Kt)); int lenK=strlen(K); for(int i=0;i<lenK;i++){//把K转换为千进制Kt,其中Kt局部顺序,全局倒序 int pKt=(lenK+2-i)/3-1;//如K=1234567=[ 1][234][567] ,则Kt=[567][234][1 ] Kt[pKt]=Kt[pKt]*10+K[i]-‘0‘; } int lenKt=(lenK+2)/3; bool flag=1; int pMin=0;//能整除K且比L小的在prime中的最小素数下标 while(prime[pMin]<L){//枚举prime中比L小的素数 if(!mod(Kt,prime[pMin],lenKt)){ flag=0; cout<<"BAD "<<prime[pMin]<<endl; break; } pMin++; } if(flag) cout<<"GOOD"<<endl; } return 0; }