POJ2635——The Embarrassed Cryptographer(高精度取模+筛选取素数)

The Embarrassed Cryptographer


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

题目大意:

    给定一个大数N,它是又两个素数a,b相乘生成的。在给定一个c,判断是否 c<min(a,b). 若c大于min(a,b),输出BAD min(a,b);否则输出GOOD

解题思路:

    给定的c小于等于10^6。故将10^6以内的素数用筛选法筛选出来,在判断是否存在N%prime==0。

    由于N的范围为10^100。不能直接判断。

    举个例子:123%6

    ->((1%6*10+2)%6*10+3)%6

    可以分步取余。

    PS:一位一位取余数会超时。三位三位的取也不会超int型,且效率不会超时。

Code:

 1 /*************************************************************************
 2     > File Name: poj2635.cpp
 3     > Author: Enumz
 4     > Mail: 369372123@qq.com
 5     > Created Time: 2014年10月29日 星期三 01时13分50秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<climits>
22 #define MAXN 1000010
23 using namespace std;
24 bool is_prime[MAXN];
25 char num[101];
26 int num_int[50];
27 int N;
28 void init()
29 {
30     is_prime[1]=is_prime[0]=1;
31     for (int i=2;i<=MAXN-5;i++)
32         if (!is_prime[i])
33             for (int j=i+i;j<=MAXN-5;j+=i)
34                 is_prime[j]=1;
35 }
36 int main()
37 {
38     init();
39     while (cin>>num>>N)
40     {
41         int len=strlen(num);
42         if (len==1&&num[0]=='0'&&N==0) break;
43         int k=1;
44         for (int i=len-1; i>=0;)
45         {
46             int tmp=0;
47             tmp+=num[i--]-'0';
48             if (i>=0) tmp+=10*(num[i--]-'0');
49             if (i>=0) tmp+=100*(num[i--]-'0');
50             num_int[k++]=tmp;
51         }
52         k--;
53         int t=k;
54         bool ok=0;
55         for (int i=1; i<N; i++)
56             if (!is_prime[i])
57             {
58                 k=t;
59                 int ans=num_int[k--]%i;
60                 while (k>=1) ans=(ans*1000+num_int[k--])%i;
61                 if (!ans) {ok=1;printf("BAD %d
",i);break;}
62             }
63         if (!ok)
64             printf("GOOD
");
65     }
66     return 0;
67 }
原文地址:https://www.cnblogs.com/Enumz/p/4062994.html