結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー tentententen
提出日時 2023-02-22 10:59:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 2,143 bytes
コンパイル時間 2,347 ms
コンパイル使用メモリ 86,748 KB
実行使用メモリ 53,624 KB
最終ジャッジ日時 2023-09-29 19:40:41
合計ジャッジ時間 5,276 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,360 KB
testcase_01 AC 39 ms
49,904 KB
testcase_02 AC 81 ms
53,624 KB
testcase_03 AC 40 ms
49,380 KB
testcase_04 AC 40 ms
49,276 KB
testcase_05 AC 41 ms
49,432 KB
testcase_06 AC 42 ms
49,396 KB
testcase_07 AC 39 ms
49,824 KB
testcase_08 AC 40 ms
49,468 KB
testcase_09 AC 40 ms
49,596 KB
testcase_10 AC 41 ms
49,428 KB
testcase_11 AC 41 ms
49,436 KB
testcase_12 AC 77 ms
53,440 KB
testcase_13 AC 68 ms
51,472 KB
testcase_14 AC 77 ms
51,324 KB
testcase_15 AC 79 ms
51,392 KB
testcase_16 AC 68 ms
51,736 KB
testcase_17 AC 70 ms
51,684 KB
testcase_18 AC 68 ms
51,496 KB
testcase_19 AC 83 ms
53,416 KB
testcase_20 AC 71 ms
51,360 KB
testcase_21 AC 83 ms
53,444 KB
testcase_22 AC 80 ms
53,440 KB
testcase_23 AC 78 ms
53,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int left = sc.nextInt();
        int right = sc.nextInt();
        boolean[] isNotPrimes = new boolean[right * 2 + 5];
        isNotPrimes[1] = true;
        for (int i = 2; i < isNotPrimes.length; i++) {
            if (!isNotPrimes[i]) {
                for (int j = 2; j * i < isNotPrimes.length; j++) {
                    isNotPrimes[j * i] = true;
                }
            }
        }
        int ans = 0;
        for (int i = left; i < right; i++) {
            if (!isNotPrimes[i]) {
                ans++;
            }
            if (!isNotPrimes[i * 2 + 1]) {
                ans++;
            }
        }
        if (!isNotPrimes[right]) {
            ans++;
        }
        System.out.println(ans);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0