結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー ks2mks2m
提出日時 2021-08-27 21:42:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,277 bytes
コンパイル時間 1,893 ms
コンパイル使用メモリ 80,028 KB
実行使用メモリ 71,616 KB
最終ジャッジ日時 2024-05-01 02:03:47
合計ジャッジ時間 6,626 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 160 ms
71,264 KB
testcase_01 AC 166 ms
70,868 KB
testcase_02 AC 177 ms
71,516 KB
testcase_03 AC 149 ms
71,384 KB
testcase_04 AC 150 ms
70,824 KB
testcase_05 AC 162 ms
71,540 KB
testcase_06 AC 151 ms
70,696 KB
testcase_07 AC 157 ms
70,636 KB
testcase_08 AC 171 ms
71,404 KB
testcase_09 AC 151 ms
70,740 KB
testcase_10 AC 150 ms
70,660 KB
testcase_11 AC 153 ms
70,596 KB
testcase_12 AC 169 ms
71,204 KB
testcase_13 AC 159 ms
70,540 KB
testcase_14 AC 154 ms
70,884 KB
testcase_15 AC 164 ms
71,228 KB
testcase_16 AC 166 ms
71,216 KB
testcase_17 AC 164 ms
71,372 KB
testcase_18 AC 165 ms
71,616 KB
testcase_19 AC 159 ms
70,644 KB
testcase_20 AC 164 ms
71,400 KB
testcase_21 AC 167 ms
71,160 KB
testcase_22 AC 154 ms
71,340 KB
testcase_23 AC 163 ms
71,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int l = sc.nextInt();
		int r = sc.nextInt();
		sc.close();

		Eratosthenes era = new Eratosthenes(4000000);
		int ans = 0;
		for (int i = l; i <= r; i++) {
			int sum = 0;
			int end = Math.min(i + 3, r);
			for (int j = i; j <= end; j++) {
				sum += j;
				if (era.isSosuu(sum)) {
					ans++;
				}
			}
		}
		System.out.println(ans);
	}

	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];
				soinsu.put(d, soinsu.getOrDefault(d, 0) + 1);
				x /= d;
			}
			return soinsu;
		}

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