POJ 2635 The Embarrassed Cryptographer(整数刷新,千进制取模)

The Embarrassed Cryptographer
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9291   Accepted: 2403

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

 
 
 

大致题意:

给定一个大数K,K是两个大素数的乘积的值。

再给定一个int内的数L

问这两个大素数中最小的一个是否小于L,如果小于则输出这个素数。

 

解题思路:

首先对题目的插图表示无语。。。

 

高精度求模+同余模定理

 

1、  Char格式读入K。把K转成千进制Kt,同时变为int型。

把数字往大进制转换能够加快运算效率。若用十进制则耗费很多时间,会TLE。

千进制的性质与十进制相似。

例如,把K=1234567890转成千进制,就变成了:Kt=[  1][234][567][890]。

为了方便处理,我的程序是按“局部有序,全局倒序”模式存放Kt

即Kt=[890][567][234][1  ]  (一个中括号代表一个数组元素)

2、  素数打表,把10^6内的素数全部预打表,在求模时则枚举到小于L为止。

注意打表不能只打到100W,要保证素数表中最大的素数必须大于10^6,否则当L=100W且K为GOOD时,会因为数组越界而RE,这是因为越界后prime都是负无穷的数,枚举的while(prime[pMin]<L)循环会陷入死循环

3、  高精度求模。

主要利用Kt数组和同余模定理。

例如要验证123是否被3整除,只需求模124%3

但当123是一个大数时,就不能直接求,只能通过同余模定理对大数“分块”间接求模

具体做法是:

先求1%3 = 1

再求(1*10+2)%3 = 0

再求 (0*10+4)% 3 = 1

那么就间接得到124%3=1,这是显然正确的

而且不难发现, (1*10+2)*10+4 = 124

这是在10进制下的做法,千进制也同理,*10改为*1000就可以了

 

 

算法思路:千进制表示已知数,进行高精度取余即可,不过大牛们说,百进制TLE,千进制AC,万进制WA,

 

 

#include<stdio.h>
#include<string.h>
const int MAXN=1000010;
int prime[MAXN+1];
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];
}

int Kt[100];
int L;
char str[1000];

bool mod(int *K,int p,int len)
{
int sq=0;
for(int i=len-1;i>=0;i--)
sq=(sq*1000+K[i])%p;
if(!sq) return false;
return true;
}
int main()
{
getPrime();

while(scanf("%s %d",&str,&L)!=EOF)
{
if(L==0&&strcmp(str,"0")==0) break;
int len=strlen(str);
memset(Kt,0,sizeof(Kt));
for(int i=0;i<len;i++)
{
int ii=(len+2-i)/3-1;
Kt[ii]=Kt[ii]*10+str[i]-'0';
}
int lenKt=(len+2)/3;
bool flag=true;
int pMin=1;
while(prime[pMin]<L)
{
if(!mod(Kt,prime[pMin],lenKt))
{
flag=false;
printf("BAD %d\n",prime[pMin]);
break;
}
pMin++;
}
if(flag) printf("GOOD\n");
}
return 0;
}

 

posted on 2012-04-01 23:18  kuangbin  阅读(1344)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: