結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-10-14 15:00:31 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,824 bytes |
| コンパイル時間 | 2,693 ms |
| コンパイル使用メモリ | 79,432 KB |
| 実行使用メモリ | 80,916 KB |
| 最終ジャッジ日時 | 2024-11-17 18:43:50 |
| 合計ジャッジ時間 | 13,330 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 8 TLE * 2 |
ソースコード
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
long t = System.currentTimeMillis();
new Main().run();
System.err.println(System.currentTimeMillis() - t);
}
// n = p^aで表せるか判定
boolean check2(long n) {
if (isPrime(n))
return true;
long fa = factor(n);
while (!isPrime(fa))
fa = factor(fa);
while (n % fa == 0)
n /= fa;
if (n != 1)
return false;
else
return true;
}
boolean check(long n) {
if (n == 1 || n == 2 || n == 3) {
return false;
}
if (n % 2 == 0) {
return true;
}
for (int i = 0; i < 63; ++i) {
long m = n - (1L << i);
if (m < 0) {
return false;
}
if (check2(m)) {
return true;
}
}
throw new AssertionError();
}
long factor(long n) {
if (n < 1e6) {
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0)
return i;
}
throw new AssertionError();
}
for (int i = 2; i < 1000; ++i) {
if (n % i == 0)
return i;
}
int Lim = (int) (Math.sqrt(Math.sqrt(n)));
for (int c = 1; c < 200; ++c) {
long x = 2;
long y = (x * x + c) % n;
for (int t = 0; t < Lim; ++t) {
long g = gcd(n, Math.abs(x - y));
if (g != 1 && g != n)
return g;
if (g == n)
break;
x = (mul_mod(x, x, n) + c) % n;
y = (mul_mod(y, y, n) + c) % n;
y = (mul_mod(y, y, n) + c) % n;
}
}
tr(n);
throw new AssertionError();
}
boolean isPrime(long n) {
if (n == 1)
return false;
if (n < 1000) {
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0)
return false;
}
return true;
}
if (n % 2 == 0)
return false;
long m = n - 1;
int c = 0;
while (m % 2 == 0) {
m /= 2;
++c;
}
for (int i = 0; i < 20; ++i) {
long a = (long) (Math.random() * (n - 1)) + 1;
a = pow(a, m, n);
boolean f = a == 1 || (c > 0 && a == n - 1);
for (int j = 0; j < c - 1; ++j) {
a = mul_mod(a, a, n);
f |= a == n - 1;
}
if (!f)
return false;
}
return true;
}
long pow(long a, long n, long mod) {
long ret = 1;
for (; n > 0; n >>= 1, a = mul_mod(a, a, mod)) {
if (n % 2 == 1) {
ret = mul_mod(ret, a, mod);
}
}
return ret;
}
long gcd(long a, long b) {
if (a > b) {
return gcd(b, a);
}
if (a == 0)
return b;
return gcd(b % a, a);
}
long mul_mod(long a, long b, long mod) {
long ret = 0;
while (b > 0) {
if (b % 2 == 0) {
a = 2 * a;
if (a >= mod)
a -= mod;
b /= 2;
// a*b=2*a*b/2
} else {
// a*b=a*(2b'+1)
ret += a;
if (ret >= mod)
ret -= mod;
a = 2 * a;
if (a >= mod)
a -= mod;
b /= 2;
}
}
return ret;
}
void run() {
Scanner sc = new Scanner();
int Q = sc.nextInt();
PrintWriter pw = new PrintWriter(System.out);
while (Q-- > 0) {
long n = sc.nextLong();
pw.println(check(n) ? "Yes" : "No");
}
pw.close();
}
class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
private void skipUnprintable() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
}
public boolean hasNext() {
skipUnprintable();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
return (int) nextLong();
}
}
static void tr(Object... objects) {
System.out.println(Arrays.deepToString(objects));
}
}