The Embarrassed Cryptographer(高精度取模+同余模定理)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11435 | Accepted: 3040 |
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.
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
题意:k是两个素数的乘积,但k是一个大数,若两个素数中最小的素数不小于l输出“GOOD",否则输出"BAD"和最小的素数;
思路:高精度取模:例如k是“1234567”,转化为千进制后,在kt数组里的形式为kt[1][234][567],在程序里的形式是kt[567][234][1],即整体逆序,局部有序;
同余模定理:如kt[567][234][1]对100取模,
1%100= 1;
(1*1000+234)%100 = 34;
(34*1000+567)%100 = 67;
67!=0,所以原来的k不能被100整除;
1 #include<stdio.h> 2 #include<string.h> 3 const int MAX = 1000010; 4 int prime[MAX]; 5 char k[10000]; 6 int l; 7 int kt[10000];//将k转化成千进制数存到kt数组里; 8 9 //素数筛; 10 void prime_table() 11 { 12 int pnum = 0,i,j; 13 prime[pnum++] = 2; 14 15 for(i= 3; i <= MAX; i+=2) 16 { 17 bool flag = true; 18 for(j = 0; prime[j]*prime[j] <= i; j++) 19 { 20 if(!(i%prime[j])) 21 { 22 flag = false; 23 break; 24 } 25 } 26 if(flag) 27 prime[pnum++] = i; 28 } 29 } 30 31 //判断k能否被prime整除,同余模定理; 32 bool check(int kt[],int prime,int len) 33 { 34 int i; 35 int t = 0; 36 for(i = len-1; i >= 0; i--) 37 t = (t*1000+kt[i])%prime; 38 if(t) 39 return false; 40 return true; 41 } 42 43 int main() 44 { 45 int i,cnt; 46 prime_table(); 47 while(~scanf("%s %d",k,&l)) 48 { 49 if(k[0] == '0' && l == 0) 50 break; 51 memset(kt,0,sizeof(kt)); 52 53 int lenk = strlen(k); 54 55 for(i = 0; i < lenk; i++) 56 { 57 cnt = (lenk+2-i)/3-1; 58 kt[cnt] = kt[cnt]*10+(k[i]-'0'); 59 }//将k转化为千进制数,如“1234567”被转化为kt[567][234][1]; 60 int lenkt = (lenk+2)/3;//kt数组的长度; 61 62 bool flag = true; 63 int pnum = 0; 64 while(prime[pnum] < l) 65 { 66 if(check(kt,prime[pnum],lenkt)) 67 { 68 printf("BAD %d\n",prime[pnum]); 69 flag = false; 70 break; 71 } 72 pnum++; 73 } 74 if(flag) 75 printf("GOOD\n"); 76 } 77 return 0; 78 }