結果

問題 No.1140 EXPotentiaLLL!
ユーザー ks2mks2m
提出日時 2020-07-31 21:34:05
言語 Java19
(openjdk 21)
結果
AC  
実行時間 801 ms / 2,000 ms
コード長 1,565 bytes
コンパイル時間 3,355 ms
コンパイル使用メモリ 74,788 KB
実行使用メモリ 90,292 KB
最終ジャッジ日時 2023-09-20 22:00:10
合計ジャッジ時間 12,494 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 752 ms
87,852 KB
testcase_01 AC 767 ms
87,988 KB
testcase_02 AC 750 ms
90,292 KB
testcase_03 AC 721 ms
87,668 KB
testcase_04 AC 690 ms
77,632 KB
testcase_05 AC 762 ms
87,992 KB
testcase_06 AC 773 ms
79,592 KB
testcase_07 AC 801 ms
87,876 KB
testcase_08 AC 112 ms
73,224 KB
testcase_09 AC 114 ms
73,280 KB
testcase_10 AC 118 ms
73,052 KB
testcase_11 AC 115 ms
73,200 KB
testcase_12 AC 114 ms
73,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		Eratosthenes era = new Eratosthenes(5000000);
		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < t; i++) {
			String[] sa = br.readLine().split(" ");
			long a = Long.parseLong(sa[0]);
			int p = Integer.parseInt(sa[1]);
			if (era.isSosuu(p)) {
				if (a % p == 0) {
					pw.println(0);
				} else {
					pw.println(1);
				}
			} else {
				pw.println(-1);
			}
		}
		br.close();
		pw.flush();
	}

	static class Eratosthenes {
		int[] div;

		public Eratosthenes(int n) {
			if (n < 2) return;
			div = new int[n + 1];
			div[0] = -1;
			div[1] = -1;
			int end = (int) Math.sqrt(n) + 1;
			for (int i = 2; i <= end; i++) {
				if (div[i] == 0) {
					div[i] = i;
					for (int j = i * i; j <= n; j+=i) {
						if (div[j] == 0) div[j] = i;
					}
				}
			}
			for (int i = end + 1; i <= n; i++) {
				if (div[i] == 0) div[i] = i;
			}
		}

		public Map<Integer, Integer> bunkai(int x) {
			Map<Integer, Integer> soinsu = new HashMap<>();
			while (x > 1) {
				Integer d = div[x];
				if (soinsu.containsKey(d)) {
					soinsu.put(d, soinsu.get(d) + 1);
				} else {
					soinsu.put(d, 1);
				}
				x /= d;
			}
			return soinsu;
		}

		public boolean isSosuu(int x) {
			return div[x] == x;
		}
	}
}
0