The Embarrassed Cryptographer POJ - 2635(大数取模+素数筛)

The Embarrassed Cryptographer

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



题意:给出一个由两个素数相乘得到的数字 K 和一个数字 L 要求 K 的较小的那个素数因子 <L 时输出 BAD 否则输出 GOOD。
思路:
素数打表后 从小到大遍历找出能被 K 整除的最小素数判断与 L 的大小关系。
由于 K 很大需要用字符串模拟除法取余操作,但是以十进制方式一位一位取的话会TLE,所以转为千进制,一次取3位模拟取模操作。

代码:
 1 #include <set>
 2 #include <map>
 3 #include <list>
 4 #include <stack>
 5 #include <queue>
 6 #include <deque>
 7 #include <cmath>
 8 #include <string>
 9 #include <vector>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <sstream>
14 #include <iostream>
15 #include <algorithm>
16 //#include <unordered_map>
17 #define INF 0x3f3f3f3f
18 #define ll long long
19 #define ull unsigned long long
20 #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
21 using namespace std;
22 const int maxn=1e6+100;
23 int prime[maxn];
24 bool isprime[maxn];
25 int cnt=0;
26 void dbprime()
27 {
28     memset(isprime,true,sizeof(isprime));
29     isprime[0] = isprime[1] = false;
30     for(int i=2;i<maxn;i++)
31     {
32         if(isprime[i])
33         {
34             prime[cnt++]=i;
35             for(int j=i+i;j<maxn;j+=i)
36             {
37                 isprime[j]=false;
38             }
39         }
40     }
41 }
42 
43 int main()
44 {
45     string k;
46     dbprime();
47     int l;
48     while(cin>>k>>l)
49     {
50         if(k=="0"&&l==0) break;
51         bool flag=true;
52         for(int i=0;i<cnt&&prime[i]<l;i++)
53         {
54             int t=0;
55             for(int j=0;j<k.size();j+=3)
56             {
57                 int t1=0;
58                 int pow=1;
59                 for(int p=j;p<j+3&&p<k.size();p++)
60                 {
61                     pow*=10;
62                     t1=t1*10+k[p]-'0';
63                 }
64                 t*=pow;
65                 t+=t1;
66                 t%=prime[i];
67             }
68             if(t==0)
69             {
70                 cout<<"BAD "<<prime[i]<<endl;
71                 flag=false;
72                 break;
73             }
74         }
75         if(flag) cout<<"GOOD"<<endl;
76     }
77 }

 

posted @ 2020-07-29 15:48  lihaji  阅读(166)  评论(0编辑  收藏  举报