poj2635

高精度,java快要忘没了。java使用文件输入要抛出异常。

View Code
import java.util.*;
import java.io.*;
import java.math.*;

public class Main {
static int[] prm = new int[1000000];
static boolean[] is = new boolean[1000000];

static int getprm(int n) {
int i, j, k = 0;
int s, e = (int) (Math.sqrt(0.0 + n) + 1);
for (i = 0; i < 1000000; i++)
is[i]
= true;
prm[k
++] = 2;
is[
0] = is[1] = false;
for (i = 4; i < n; i += 2)
is[i]
= false;
for (i = 3; i < e; i += 2)
if (is[i]) {
prm[k
++] = i;
for (s = i * 2, j = i * i; j < n; j += s)
is[j]
= false;
// 因为j是奇数,所以+奇数i后是偶数,不必处理!
}
for (; i < n; i += 2)
if (is[i])
prm[k
++] = i;
return k;
// 返回素数的个数
}

public static void main(String args[]) throws FileNotFoundException {
Scanner cin
= new Scanner(new BufferedInputStream(System.in));
//Scanner cin =new Scanner (new FileInputStream("t.txt"));
String st;
int count = getprm(1000000);
boolean found = false;
BigInteger zero
= new BigInteger("0");
while (cin.hasNext()) {
st
= cin.next();
found
= false;
BigInteger a
= new BigInteger(st);
int b = cin.nextInt();
if (a.equals(zero) && b == 0)
break;
for (int i = 0; i < count && prm[i] < b; i++)
if (a.mod(BigInteger.valueOf(prm[i])).equals(zero)) {
System.out.println(
"BAD " + prm[i]);
found
= true;
break;
}
if (!found)
System.out.println(
"GOOD");
}
}
}

原文地址:https://www.cnblogs.com/rainydays/p/2047746.html