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
Source
Nordic 2005
题意:
有一个大整数K,和你个int L。K是两个大素数之积,问两个大数中较小的一个是否可能小于L.
分析:
要解决这题,首先我们最直观的思路就是将小于L的所有素数分别求出来,看是否能被K整除。
但是k是一个大数,无法直接求,所以我们就要用到同余模定理求大数的模。
但是这里用10进制会超时,要用千进制表示。
关于求素数,直接将<=1000000的所有素数打表出来即可 。
同余定理:
(a+b)%c=(a%c + b%c)%c;
(a*b)%c=(a%c * b%c)%c;
对大数取余模板: 大数字符串形式 a[1000];
char a[1000];
int m=0;
for(int i=0;a[i]!='\0';i++)
m=((m*10)%n+(a[i]-'0')%n)%n;//也可以写成 m=(m*10+a[i]-'0')%n
m为所求的余数
这是在10进制下的做法,千进制也同理,*10改为*1000就可以了
例如要验证123是否被3整除,只需求模123%3
但当123是一个大数时,就不能直接求,只能通过同余模定理对大数“分块”间接求模
具体做法是:
先求1%3 = 1
再求(1*10+2)%3 = 0
再求 (0*10+4)% 3 = 1
那么就间接得到124%3=1,这是显然正确的
而且不难发现, (1*10+2)*10+4 = 124
把数字往大进制转换能够加快运算效率。若用十进制则耗费很多时间,会TLE。
千进制的性质与十进制相似。
例如,把K=1234567890转成千进制,就变成了:Kt=[ 1][234][567][890]。
为了方便处理,我的程序是按“局部有序,全局倒序”模式存放Kt
即Kt=[890][567][234][1 ] (一个中括号代表一个数组元素)
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1000010;
int prime[maxn+1];
char K[1000];
int Kt[100],L;
int getPrime()
{
memset(prime,0,sizeof(prime));
for(int i=2;i<=maxn;i++)
{
if(!prime[i]) prime[++prime[0]] = i;
for(int j=1;j<=prime[0] && prime[j]<=maxn/i;j++)
{
prime[prime[j]*i] = 1;
if(i % prime[j] == 0) break;
}
}
return prime[0];
}
bool mod(int* Kt,int p,int len)
{
int sq = 0;
for(int i=len-1;i>=0;i--)
sq = (sq*1000 + Kt[i]) % p;
if(!sq) return true;
return false;
}
int main()
{
getPrime();
while(scanf("%s%d",&K,&L)!=EOF)
{
if(L==0 && strcmp(K,"0")==0) break;
int len = strlen(K);
memset(Kt,0,sizeof(Kt));
for(int i=0;i<len;i++)
{
int t = (len+2-i)/3 - 1;
Kt[t] = Kt[t]*10 + K[i] - '0';
}
int lenn = (len+2)/3;
bool flag = true;
int num = 1;
while(prime[num] < L)
{
if(mod(Kt,prime[num],lenn))
{
flag = false;
printf("BAD %d\n",prime[num]);
break;
}
num++;
}
if(flag) printf("GOOD\n");
}
return 0;
}