結果

問題 No.843 Triple Primes
ユーザー ks2mks2m
提出日時 2019-06-28 22:22:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 1,587 bytes
コンパイル時間 3,397 ms
コンパイル使用メモリ 78,788 KB
実行使用メモリ 60,348 KB
最終ジャッジ日時 2023-10-19 17:48:00
合計ジャッジ時間 13,749 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,420 KB
testcase_01 AC 304 ms
60,024 KB
testcase_02 AC 85 ms
54,324 KB
testcase_03 AC 67 ms
54,264 KB
testcase_04 AC 81 ms
54,536 KB
testcase_05 AC 68 ms
54,160 KB
testcase_06 AC 93 ms
54,328 KB
testcase_07 AC 292 ms
60,280 KB
testcase_08 AC 290 ms
60,340 KB
testcase_09 AC 304 ms
60,112 KB
testcase_10 AC 292 ms
60,276 KB
testcase_11 AC 290 ms
60,168 KB
testcase_12 AC 299 ms
60,036 KB
testcase_13 AC 296 ms
60,028 KB
testcase_14 AC 306 ms
60,072 KB
testcase_15 AC 292 ms
58,236 KB
testcase_16 AC 290 ms
60,256 KB
testcase_17 AC 55 ms
51,380 KB
testcase_18 AC 54 ms
53,416 KB
testcase_19 AC 55 ms
51,536 KB
testcase_20 AC 168 ms
59,596 KB
testcase_21 AC 135 ms
58,392 KB
testcase_22 AC 236 ms
60,280 KB
testcase_23 AC 228 ms
59,852 KB
testcase_24 AC 167 ms
59,548 KB
testcase_25 AC 145 ms
59,564 KB
testcase_26 AC 299 ms
60,144 KB
testcase_27 AC 98 ms
55,724 KB
testcase_28 AC 296 ms
60,024 KB
testcase_29 AC 168 ms
59,508 KB
testcase_30 AC 301 ms
60,112 KB
testcase_31 AC 120 ms
58,188 KB
testcase_32 AC 108 ms
55,784 KB
testcase_33 AC 178 ms
59,668 KB
testcase_34 AC 225 ms
60,240 KB
testcase_35 AC 304 ms
60,140 KB
testcase_36 AC 146 ms
59,280 KB
testcase_37 AC 284 ms
60,188 KB
testcase_38 AC 265 ms
60,348 KB
testcase_39 AC 300 ms
60,012 KB
testcase_40 AC 56 ms
53,412 KB
testcase_41 AC 55 ms
53,416 KB
testcase_42 AC 289 ms
60,284 KB
testcase_43 AC 302 ms
60,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		br.close();

		if (n == 1) {
			System.out.println(0);
			return;
		}

		List<Integer> sosuu = sosuuList(n);
		int ans = 1;
		for (int i = 1; i < sosuu.size(); i++) {
			int x = sosuu.get(i) + 2;
			Map<Integer, Integer> soinsu = bunkai(x);
			if (soinsu.size() == 1) {
				for (Integer o : soinsu.keySet()) {
					if (soinsu.get(o) == 2) {
						ans += 2;
					}
				}
			}
		}
		System.out.println(ans);
	}

	static List<Integer> sosuuList(int n) {
		List<Integer> sosuu = new ArrayList<Integer>();
		for (int i = 2; i <= n; i++) {
			int r = (int) Math.sqrt(i);
			boolean flg = false;
			for (Integer o : sosuu) {
				if (r < o) {
					break;
				}
				if (i % o == 0) {
					flg = true;
					break;
				}
			}
			if (!flg) {
				sosuu.add(i);
			}
		}
		return sosuu;
	}

	static Map<Integer, Integer> bunkai(int n) {
		Map<Integer, Integer> soinsu = new HashMap<Integer, Integer>();
		int end = (int) Math.sqrt(n);
		int d = 2;
		while (n > 1) {
			if (n % d == 0) {
				n /= d;
				if (soinsu.containsKey(d)) {
					soinsu.put(d, soinsu.get(d) + 1);
				} else {
					soinsu.put(d, 1);
				}
				end = (int) Math.sqrt(n);
			} else {
				if (d > end) {
					d = n - 1;
				}
				d++;
			}
		}
		return soinsu;
	}
}
0