poj 2635 The Embarrassed Cryptographer

The Embarrassed Cryptographer
Time Limit: 2000MS Memory Limit: 65536K
Total 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 <= 10 100 and 2 <= L <= 10 6. 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(4<=k<=10^100),一个整数l(2<=l<=10^6),请判断如果组成大数的最小素数大于等于l,输出"GOOD",否则输出"BAD a",a为两个素数中最小的那一个。

思路:

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

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

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

示例程序

java:

Source Code

Problem: 2635		Code Length: 1110B
Memory: 5592K		Time: 1782MS(4750MS)			//括号里是用试除法打表的方式的运行时间
Language: Java		Result: Accepted
import 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 Code

Problem: 2635		Code Length: 1107B
Memory: 704K		Time: 1235MS
Language: GCC		Result: 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值