您的位置:首页 > 其它

POJ 2635 The Embarrassed Cryptographer【数论,千进制,高精度求模+同余模定理】

2017-08-11 13:59 525 查看
The Embarrassed Cryptographer

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14856 Accepted: 4072
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

原题链接:http://poj.org/problem?id=2635

给定一个大数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就可以了

参考博客:http://blog.csdn.net/lyy289065406/article/details/6648530

AC代码:

#include<iostream>
#include<string.h>
using namespace std;

const int Range=1000100; //打表不能只打到100W,素数表中最大的素数必须大于10^6

int Kt[10000]; //千进制的K
int L;
int prime[Range+1];

/*素数组打表*/
void PrimeTable(void)
{
int pNum=0;
prime[pNum++]=2;

for(int i=3;i<=Range;i+=2) //奇偶法
{
bool flag=true;
for(int j=0;prime[j]*prime[j]<=i;j++) //根号法+递归法
if(!(i%prime[j]))
{
flag=false;
break;
}
if(flag)
prime[pNum++]=i;
}
return;
}

/*高精度K对p求模,因数检查(整除)*/
bool mod(const int* K,const int p,const int len)
{
int sq=0;
for(int i=len-1;i>=0;i--) //千进制K是逆序存放
sq=(sq*1000+K[i])%p; //同余模定理

if(!sq) //K被整除
return false;
return true;
}

int main(void)
{
PrimeTable();

char K[10000];
while(cin>>K>>L && L)
{
memset(Kt,0,sizeof(Kt));
int lenK=strlen(K);
for(int i=0;i<lenK;i++) //把K转换为千进制Kt,其中Kt局部顺序,全局倒序
{ //如K=1234567=[ 1][234][567] ,则Kt=[567][234][1 ]
int pKt=(lenK+2-i)/3-1;
Kt[pKt]=Kt[pKt]*10+(K[i]-'0');
}
int lenKt=(lenK+2)/3;

bool flag=true;
int pMin=0; //能整除K且比L小的在prime中的最小素数下标
while(prime[pMin]<L) //枚举prime中比L小的素数
{
if(!mod(Kt,prime[pMin],lenKt))
{
flag=false;
cout<<"BAD "<<prime[pMin]<<endl;
break;
}
pMin++;
}
if(flag)
cout<<"GOOD"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息