結果

問題 No.577 Prime Powerful Numbers
ユーザー 37zigen37zigen
提出日時 2018-04-15 20:10:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 668 ms / 2,000 ms
コード長 5,617 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 88,688 KB
実行使用メモリ 78,580 KB
最終ジャッジ日時 2024-06-27 00:06:32
合計ジャッジ時間 7,863 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 299 ms
65,472 KB
testcase_01 AC 300 ms
64,676 KB
testcase_02 AC 315 ms
65,212 KB
testcase_03 AC 489 ms
74,708 KB
testcase_04 AC 390 ms
66,460 KB
testcase_05 AC 668 ms
78,580 KB
testcase_06 AC 541 ms
76,148 KB
testcase_07 AC 447 ms
69,940 KB
testcase_08 AC 544 ms
76,188 KB
testcase_09 AC 411 ms
66,556 KB
testcase_10 AC 275 ms
64,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
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);
	}

	HashSet<Long> set = new HashSet<>();

	boolean isSqrt(long a) {
		long v = (long) Math.sqrt(a);
		for (long i = v - 1; i < v + 1; ++i) {
			if (i * i == a)
				return true;
		}
		return false;
	}

	// n = p^aで表せるか判定
	boolean check2(long n) {
		if (BigInteger.valueOf(n).isProbablePrime(20) || isSqrt(n))
			return true;
		if (set.contains(n))
			return true;
		return false;

	}

	boolean check(long n) {
		if (n == 1 || n == 2 || n == 3) {
			return false;
		}
		if (n % 2 == 0) {
			return true;
		}
		int c = 0;
		while (1L << (c + 1) <= n)
			++c;
		for (int i = c; i >= 1; --i) {
			long m = n - (1L << i);
			if (check2(m)) {
				return true;
			}
		}
		return false;
	}

	long factor(long n) {
		for (int v : primeList) {
			if (n % v == 0)
				return v;
		}
		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();
	}

	long[] prime_cand = new long[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };

	boolean isPrime(long n) {
		if (n == 1)
			return false;
		if (n < isPrime.length) {
			return isPrime[(int) n];
		}
		if (n % 2 == 0)
			return false;

		long m = n - 1;
		int c = 0;
		while ((m & 1) == 0) {
			m >>= 1;
			++c;
		}
		out: for (int i = 0; i < prime_cand.length; ++i) {
			long a = prime_cand[i];
			a = pow_mod(a, m, n);
			boolean f = a == 1 || (c > 0 && a == n - 1);
			if (f)
				continue;
			for (int j = 0; j < c - 1; ++j) {
				a = mul_mod(a, a, n);
				f |= a == n - 1;
				if (f)
					continue out;
			}
			if (!f)
				return false;
		}
		return true;
	}

	long pow(long a, long n) {
		long ret = 1;
		for (; n > 0; n >>= 1, a *= a) {
			if (n % 2 == 1) {
				ret *= a;
			}
		}
		return ret;
	}

	long pow_mod(long a, long n, long mod) {
		long ret = 1;
		for (; n > 0; n >>= 1, a = mul_mod(a, a, mod)) {
			if ((n & 1) == 1) {
				ret = mul_mod(ret, a, mod);
			}
		}
		return ret;
	}

	long gcd(long a, long b) {
		if (a > b) {
			return gcd(b, a);
		}
		long tmp;
		while (a != 0) {
			tmp = b % a;
			b = a;
			a = tmp;
		}
		return b;
	}

	long mul_mod(long a, long b, long mod) {
		long ret = 0;
		while (b > 0) {
			if (b % 2 == 0) {
				a <<= 1;
				if (a >= mod)
					a -= mod;
				b >>= 1;
			} else {
				ret += a;
				if (ret >= mod)
					ret -= mod;
				a = a <<= 1;
				if (a >= mod)
					a -= mod;
				b >>= 1;
			}
		}
		return ret;
	}

	boolean[] isPrime = new boolean[1_000_000];
	ArrayList<Integer> primeList = new ArrayList<>();

	void run() {
		Arrays.fill(isPrime, true);
		isPrime[0] = false;
		isPrime[1] = false;
		for (int i = 2; i < isPrime.length; ++i) {
			if (isPrime[i]) {
				primeList.add(i);
				for (int j = 2 * i; j < isPrime.length; j += i) {
					isPrime[j] = false;
				}
				long a = i * i * i;
				while (a < (long) 1e18) {
					set.add(a);
					a = a * i;
					if (a / i * i != a)
						break;
				}
			}
		}
		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));
	}

}
0