結果

問題 No.2724 Coprime Game 1
ユーザー ks2mks2m
提出日時 2024-04-12 22:20:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 1,553 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 78,416 KB
実行使用メモリ 79,644 KB
最終ジャッジ日時 2024-04-12 22:20:13
合計ジャッジ時間 5,343 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
77,652 KB
testcase_01 AC 138 ms
77,032 KB
testcase_02 AC 137 ms
77,232 KB
testcase_03 AC 287 ms
79,576 KB
testcase_04 AC 320 ms
79,468 KB
testcase_05 AC 320 ms
79,644 KB
testcase_06 AC 285 ms
79,036 KB
testcase_07 AC 321 ms
79,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
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());

		int m = 3000000;
		Eratosthenes era = new Eratosthenes(m);
		int[] a = new int[m + 1];
		for (int i = 2; i < a.length; i++) {
			if (era.isSosuu(i)) {
				a[i]++;
			}
			a[i] += a[i - 1];
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int z = 0; z < t; z++) {
			int n = Integer.parseInt(br.readLine());
			if (era.isSosuu(n)) {
				pw.println("P");
			} else {
				int num = n - 2 - (a[n] - a[n / 2]);
				if (num % 2 == 1) {
					pw.println("K");
				} else {
					pw.println("P");
				}
			}
		}
		pw.flush();
		br.close();
	}

	static class Eratosthenes {
		int[] div;

		public Eratosthenes(int n) {
			div = new int[n + 1];
			if (n < 2) return;
			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];
				soinsu.put(d, soinsu.getOrDefault(d, 0) + 1);
				x /= d;
			}
			return soinsu;
		}

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