poj2635The Embarrassed Cryptographer(数论)(解题报告)

来源:互联网 发布:计算机java二级考试 编辑:程序博客网 时间:2024/05/29 15:37

http://poj.org/problem?id=2635

Language:

The Embarrassed Cryptographer

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 10080

Accepted: 2639

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

题意:给出kL,,要求K是由两个素因子组成,且K是由两个素因子乘积形成,要求其中一个素因子小于L

思路:高精度取模+同余定理。

1.构造素数表格,这里可用奇偶法,和线性素数筛选法

2,由于给出K非常大,因此可将K化为千进制存到一组数组中,以提高高精度运算的效率

//Memory 480K  Time1266MS  length1207B#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>using namespace std;const int maxn=1001000;int len,t2,total;int prime[maxn+1];int k[40];char ch[105];void makeprime()//构造素数表 {       total=0;      int flag;      prime[total++]=2;     for(int i=3;i<=maxn;i+=2)     {flag=1;      for(int j=0;prime[j]*prime[j]<=i;j++)     {if(i%prime[j]==0)      {       flag=0;       break;      }     }     if(flag)     prime[total++]=i;     }     }
void change()//把字符串转化为千进制 {int t1=len%3;t2=len/3;if(t1==0)t2--;int j=t2;memset(k,0,sizeof(k));for(int i=len-1;i>=0;i-=3)if(j>=0){for(int t=(i-2>0? i-2:0);t<=i;t++)k[j]=k[j]*10+ch[t]-'0';j--;}}int Mod(int d)//大数取模运算 {int ans=0;for(int i=0;i<=t2;i++)ans=(ans*1000+k[i])%d;return ans;}int main(){makeprime();int L;while(~scanf("%s%d",ch,&L)&&L) { len=strlen(ch); int ok=1; change();  for(int i=0;prime[i]<L&&i<total;i++) if(Mod(prime[i])==0) { printf("BAD %d\n",prime[i]); ok=0; break; } if(ok) printf("GOOD\n"); } system("pause"); return 0;}


 

小结:对于素数的筛选和构造中,可用三种方法:

1,用时最短的是线性筛选法,但耗空间;

2.奇偶法筛选选素数:省空间,但与线性筛选法相比,比较耗时。

3.暴力枚举,超时!!!

原创粉丝点击