您的位置:首页 > 其它

poj2635 The Embarrassed Cryptographer

2016-08-18 23:17 447 查看
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.

首先用线性筛求出1e6以内的质数,然后用所有不超过l的质数验证。

这样问题就转化成了大整数取模。逐位加上数,边加边取模即可。

#include<cstdio>
#include<cstring>
const int mx=1e6;
int prm[1000010],tot;
bool have[1000010];
char s[110];
void make()
{
for (int i=2;i<=mx;i++)
{
if (!have[i]) prm[++tot]=i;
for (int j=1;j<=tot&&i*prm[j]<=mx;j++)
{
have[i*prm[j]]=1;
if (i%prm[j]==0) break;
}
}
}
int main()
{
int i,j,k,l,m,n,p,q,x,y,z;
bool flag;
make();
while (scanf("%s%d",s+1,&l)&&l)
{
m=strlen(s+1);
flag=0;
for (i=1;i<=tot&&prm[i]<l;i++)
{
n=0;
for (j=1;j+2<=m;j+=3)
n=(n*1000+(s[j]-'0')*100+(s[j+1]-'0')*10+s[j+2]-'0')%prm[i];
if (j==m-1) n=(n*100+(s[j]-'0')*10+s[j+1]-'0')%prm[i];
if (j==m) n=(n*10+s[j]-'0')%prm[i];
if (n==0)
{
flag=1;
printf("BAD %d\n",prm[i]);
break;
}
}
if (!flag) printf("GOOD\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息