結果
| 問題 | 
                            No.1657 Sum is Prime (Easy Version)
                             | 
                    
| コンテスト | |
| ユーザー | 
                             ks2m
                         | 
                    
| 提出日時 | 2021-08-27 21:42:38 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 219 ms / 2,000 ms | 
| コード長 | 1,277 bytes | 
| コンパイル時間 | 3,777 ms | 
| コンパイル使用メモリ | 78,964 KB | 
| 実行使用メモリ | 58,728 KB | 
| 最終ジャッジ日時 | 2024-11-21 01:21:38 | 
| 合計ジャッジ時間 | 8,073 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
ソースコード
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;
		}
	}
}
            
            
            
        
            
ks2m