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

时间:2021-07-26 06:25:05

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:

 /*************************************************************************
> File Name: poj2635.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月29日 星期三 01时13分50秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 1000010
using namespace std;
bool is_prime[MAXN];
char num[];
int num_int[];
int N;
void init()
{
is_prime[]=is_prime[]=;
for (int i=;i<=MAXN-;i++)
if (!is_prime[i])
for (int j=i+i;j<=MAXN-;j+=i)
is_prime[j]=;
}
int main()
{
init();
while (cin>>num>>N)
{
int len=strlen(num);
if (len==&&num[]==''&&N==) break;
int k=;
for (int i=len-; i>=;)
{
int tmp=;
tmp+=num[i--]-'';
if (i>=) tmp+=*(num[i--]-'');
if (i>=) tmp+=*(num[i--]-'');
num_int[k++]=tmp;
}
k--;
int t=k;
bool ok=;
for (int i=; i<N; i++)
if (!is_prime[i])
{
k=t;
int ans=num_int[k--]%i;
while (k>=) ans=(ans*+num_int[k--])%i;
if (!ans) {ok=;printf("BAD %d\n",i);break;}
}
if (!ok)
printf("GOOD\n");
}
return ;
}