poj 2635 The Embarrassed Cryptographer

来源:互联网 发布:日期提醒软件 编辑:程序博客网 时间:2024/06/11 19:54
The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14064 Accepted: 3838

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 10143 20667 20667 302573 302573 400 0

Sample Output

GOODBAD 11GOODBAD 23GOODBAD 31

Source

Nordic 2005

提示

题意:

由两个素数相乘组成的大数k(4<=k<=10^100),一个整数l(2<=l<=10^6),请判断如果组成大数的最小素数大于等于l,输出"GOOD",否则输出"BAD a",a为两个素数中最小的那一个。

思路:

就是对2~l位置后面一个素数的范围整数内的素数打表,之后从小到大枚举素数,判断结果。不过数据比较大,java比较方便。

不用java需要利用同余模定理,并且还要化作千进制去取余,不然还是会超时,素数打表用根号试除同样也会TLE。

详细解析去戳它吧(主要是我不会啊)

示例程序

java:

Source CodeProblem: 2635Code Length: 1110BMemory: 5592KTime: 1782MS(4750MS)//括号里是用试除法打表的方式的运行时间Language: JavaResult: Acceptedimport java.math.*;import java.util.*;public class Main {    public static void main(String[] args)    {        Scanner in = new Scanner(System.in);        int l,a[]=new int[78500],i,i1,top;        BigInteger k;        a[0]=2;        top=1;        for(i=3;1000005>=i;i=i+2)//新技能get√        {            for(i1=0;i>=a[i1]*a[i1];i1++)            {                if(i%a[i1]==0)                {                    break;                }            }            if(i<a[i1]*a[i1])            {                a[top]=i;                top++;            }        }        k=in.nextBigInteger();        l=in.nextInt();        while(k.compareTo(BigInteger.ZERO)!=0||l!=0)        {            for(i=0;l>a[i];i++)            {        if(k.mod(BigInteger.valueOf(a[i])).equals(BigInteger.ZERO)==true)//一直哇的原因就是用c里面的方式判断余数为0,没用equals()        {            break;        }            }            if(i==78500||a[i]>=l)//比c中的数组多开一个,不然会RE            {        System.out.println("GOOD");            }            else            {        System.out.println("BAD "+a[i]);            }            k=in.nextBigInteger();            l=in.nextInt();        }    }}
c:

Source CodeProblem: 2635Code Length: 1107BMemory: 704KTime: 1235MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>int main(){    int a[78499],l,i,i1,k[10000],num,pos,top;    char x[102];    a[0]=2;    top=1;    for(i=3;1000005>=i;i=i+2)    {        for(i1=0;i>=a[i1]*a[i1];i1++)        {            if(i%a[i1]==0)            {                break;            }        }        if(i<a[i1]*a[i1])        {            a[top]=i;            top++;        }    }    scanf("%s %d",x,&l);    while(x[0]!='0'||l!=0)    {        memset(k,0,sizeof(k));        top=strlen(x);        for(i=0;top>i;i++)//千进制换算        {            pos=(top+2-i)/3-1;            k[pos]=k[pos]*10+(x[i]-'0');        }        top=(top+2)/3;        for(i=0;l>a[i];i++)        {            num=0;            for(i1=top-1;i1>=0;i1--)            {                num=(num*1000+k[i1])%a[i];//取出来取余,同余模定理            }            if(num==0)            {                break;            }        }        if(i==78499||a[i]>=l)        {            printf("GOOD\n");        }        else        {            printf("BAD %d\n",a[i]);        }        scanf("%s %d",x,&l);    }    return 0;}
0 0
原创粉丝点击