POJ2635

来源:互联网 发布:win7优化软件 编辑:程序博客网 时间:2024/05/16 16:22

The Embarrassed Cryptographer

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15103 Accepted: 4137

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

题意: 给你一个k和L,其中k的范围很大,让你判断k是否是两个素数的乘积,且两个素数中小的在[2,L]范围内,输出即可。范围: 4<=K<=10100,2<=L<=106
分析: 我们先把1e6内的素数打表打到一个数组里然后直接暴力判断即可

参考代码

import java.util.Arrays;import java.util.Scanner;import  java.math.BigInteger;public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        BigInteger a;        int[] p = new int[500000];        int P = 0,l;        boolean[] isp = new boolean[5100000];        isp[1] = true;        for(int i = 2;i <= 2005;i++) {            if(!isp[i]) {                for(int j = i*i;j <= 1010000;j += i) {                    isp[j] = true;                }            }        }        for(int i = 1;i <= 1010000;i++) {            if(!isp[i]) {                p[P++] = i;            }        }        while (in.hasNextBigInteger()) {            a = in.nextBigInteger();            l = in.nextInt();            if(a.add(BigInteger.valueOf(l)).compareTo(BigInteger.valueOf(0)) == 0)                break;            boolean flg = false;            for(int i = 0;i < P;i++) {                if(p[i] >= l) break;                BigInteger b = a.divide(BigInteger.valueOf(p[i]));                if(a.compareTo(b.multiply(BigInteger.valueOf(p[i]))) == 0) {                    System.out.println("BAD " + p[i]);                    flg = true;                    break;                }            }            if(!flg) {                System.out.println("GOOD");            }        }    }}
  • 如有错误或遗漏,请私聊下UP,thx
原创粉丝点击