結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー ks2mks2m
提出日時 2021-08-27 21:42:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 1,277 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 74,504 KB
実行使用メモリ 75,244 KB
最終ジャッジ日時 2023-08-13 08:23:58
合計ジャッジ時間 7,757 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 177 ms
75,244 KB
testcase_01 AC 187 ms
73,240 KB
testcase_02 AC 196 ms
73,116 KB
testcase_03 AC 178 ms
73,064 KB
testcase_04 AC 181 ms
75,080 KB
testcase_05 AC 178 ms
73,632 KB
testcase_06 AC 177 ms
73,076 KB
testcase_07 AC 178 ms
71,784 KB
testcase_08 AC 181 ms
73,472 KB
testcase_09 AC 180 ms
73,028 KB
testcase_10 AC 172 ms
73,648 KB
testcase_11 AC 180 ms
73,424 KB
testcase_12 AC 195 ms
73,200 KB
testcase_13 AC 193 ms
73,644 KB
testcase_14 AC 190 ms
73,524 KB
testcase_15 AC 194 ms
73,284 KB
testcase_16 AC 185 ms
73,280 KB
testcase_17 AC 183 ms
73,380 KB
testcase_18 AC 187 ms
73,464 KB
testcase_19 AC 200 ms
73,268 KB
testcase_20 AC 192 ms
73,156 KB
testcase_21 AC 199 ms
73,040 KB
testcase_22 AC 175 ms
73,160 KB
testcase_23 AC 186 ms
73,180 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