結果

問題 No.843 Triple Primes
ユーザー tentententen
提出日時 2023-02-16 10:18:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,269 bytes
コンパイル時間 3,078 ms
コンパイル使用メモリ 84,004 KB
実行使用メモリ 54,784 KB
最終ジャッジ日時 2023-09-25 15:35:29
合計ジャッジ時間 9,442 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,484 KB
testcase_01 AC 92 ms
54,336 KB
testcase_02 AC 46 ms
49,688 KB
testcase_03 AC 45 ms
49,340 KB
testcase_04 AC 47 ms
49,704 KB
testcase_05 AC 44 ms
47,784 KB
testcase_06 AC 47 ms
49,452 KB
testcase_07 AC 89 ms
54,476 KB
testcase_08 AC 99 ms
54,584 KB
testcase_09 AC 103 ms
54,396 KB
testcase_10 AC 80 ms
54,200 KB
testcase_11 AC 101 ms
54,784 KB
testcase_12 AC 111 ms
54,564 KB
testcase_13 AC 92 ms
54,608 KB
testcase_14 AC 103 ms
54,632 KB
testcase_15 AC 91 ms
54,508 KB
testcase_16 AC 105 ms
54,004 KB
testcase_17 AC 42 ms
49,344 KB
testcase_18 AC 42 ms
49,420 KB
testcase_19 AC 43 ms
49,636 KB
testcase_20 AC 67 ms
51,920 KB
testcase_21 AC 63 ms
51,360 KB
testcase_22 AC 80 ms
51,888 KB
testcase_23 AC 92 ms
52,148 KB
testcase_24 AC 72 ms
51,336 KB
testcase_25 AC 66 ms
51,308 KB
testcase_26 AC 114 ms
54,468 KB
testcase_27 AC 49 ms
49,564 KB
testcase_28 AC 105 ms
52,536 KB
testcase_29 AC 73 ms
51,320 KB
testcase_30 AC 100 ms
54,368 KB
testcase_31 AC 54 ms
49,392 KB
testcase_32 AC 49 ms
49,464 KB
testcase_33 AC 73 ms
52,372 KB
testcase_34 AC 79 ms
51,852 KB
testcase_35 AC 102 ms
54,316 KB
testcase_36 AC 63 ms
51,152 KB
testcase_37 AC 82 ms
54,520 KB
testcase_38 AC 83 ms
51,988 KB
testcase_39 AC 82 ms
54,248 KB
testcase_40 AC 42 ms
47,384 KB
testcase_41 AC 42 ms
49,340 KB
testcase_42 AC 103 ms
54,296 KB
testcase_43 AC 107 ms
54,540 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 n = sc.nextInt();
        boolean[] isNotPrimes = new boolean[n + 1];
        ArrayList<Integer> primes = new ArrayList<>();
        HashSet<Integer> squares = new HashSet<>();
        for (int i = 2; i <= n; i++) {
            if (!isNotPrimes[i]) {
                primes.add(i);
                if (i <= Math.sqrt(2 * n)) {
                    squares.add(i * i);
                }
                for (int j = 2; j * i <= n; j++) {
                    isNotPrimes[j * i] = true;
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < primes.size(); i++) {
            if (squares.contains(2 + primes.get(i))) {
                if (i == 0) {
                    ans++;
                } else {
                    ans += 2;
                }
            }
        }
        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