結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2018-11-29 14:41:24 |
| 言語 | Java (openjdk 23) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 4,150 bytes |
| コンパイル時間 | 3,901 ms |
| コンパイル使用メモリ | 82,044 KB |
| 実行使用メモリ | 53,400 KB |
| 最終ジャッジ日時 | 2024-11-18 16:30:05 |
| 合計ジャッジ時間 | 5,222 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 3 RE * 7 |
ソースコード
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.io.UncheckedIOException;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author mikit
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
LightScanner in = new LightScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
No3030 solver = new No3030();
solver.solve(1, in, out);
out.close();
}
static class No3030 {
public void solve(int testNumber, LightScanner in, PrintWriter out) {
for (int x : in.ints(in.ints())) {
out.println(x + " " + (IntMath.isPrime(x) ? 1 : 0));
}
}
}
static final class IntMath {
private IntMath() {
}
public static boolean isPrime(int n) {
if (n == 2) {
return true;
} else if (n <= 1 || (n & 1) == 0) {
return false;
}
int s = BitMath.lsb(n - 1);
int d = (n - 1) >> s;
ModMath m = new ModMath(n);
outer:
for (int a : new int[]{2, 7, 61}) {
int r = (int) m.pow(a, d);
if (r == 1 || r == n - 1) {
continue;
}
for (int j = 0; j < s; j++) {
r = (int) m.pow(r, 2);
if (r == n - 1) {
continue outer;
}
}
return false;
}
return true;
}
}
static class LightScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public LightScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
}
public String string() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return tokenizer.nextToken();
}
public int ints() {
return Integer.parseInt(string());
}
public int[] ints(int length) {
return IntStream.range(0, length).map(x -> ints()).toArray();
}
}
static final class BitMath {
private BitMath() {
}
public static int count(int v) {
v = (v & 0x55555555) + ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
v = (v & 0x0f0f0f0f) + ((v >> 4) & 0x0f0f0f0f);
v = (v & 0x00ff00ff) + ((v >> 8) & 0x00ff00ff);
v = (v & 0x0000ffff) + ((v >> 16) & 0x0000ffff);
return v;
}
public static int lsb(int v) {
if (v == 0) {
throw new IllegalArgumentException("Bit not found");
}
v |= (v << 1);
v |= (v << 2);
v |= (v << 4);
v |= (v << 8);
v |= (v << 16);
return 32 - count(v);
}
}
static class ModMath {
private static final int DEFAULT_MOD = 1_000_000_007;
private final long mod;
public ModMath(long mod) {
this.mod = mod;
}
public ModMath() {
this(DEFAULT_MOD);
}
public long pow(long x, long y) {
if (y == 0) {
return 1;
} else if (y % 2 == 0) {
long z = pow(x, y / 2);
return (z * z) % mod;
} else {
return (x % mod) * pow(x, y - 1) % mod;
}
}
}
}