結果

問題 No.843 Triple Primes
ユーザー tentententen
提出日時 2022-08-03 11:48:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,562 bytes
コンパイル時間 4,644 ms
コンパイル使用メモリ 74,980 KB
実行使用メモリ 53,076 KB
最終ジャッジ日時 2023-10-11 03:00:07
合計ジャッジ時間 8,613 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,484 KB
testcase_01 AC 86 ms
52,584 KB
testcase_02 AC 47 ms
50,096 KB
testcase_03 AC 44 ms
50,120 KB
testcase_04 AC 50 ms
50,204 KB
testcase_05 AC 46 ms
50,364 KB
testcase_06 AC 47 ms
50,272 KB
testcase_07 AC 84 ms
53,076 KB
testcase_08 AC 97 ms
52,500 KB
testcase_09 AC 95 ms
52,844 KB
testcase_10 AC 87 ms
52,456 KB
testcase_11 AC 88 ms
52,664 KB
testcase_12 AC 92 ms
52,472 KB
testcase_13 AC 97 ms
52,908 KB
testcase_14 AC 91 ms
52,628 KB
testcase_15 AC 86 ms
52,616 KB
testcase_16 AC 85 ms
52,496 KB
testcase_17 AC 41 ms
49,352 KB
testcase_18 AC 42 ms
49,632 KB
testcase_19 AC 41 ms
49,612 KB
testcase_20 AC 75 ms
52,120 KB
testcase_21 AC 62 ms
52,020 KB
testcase_22 AC 82 ms
52,724 KB
testcase_23 AC 81 ms
52,356 KB
testcase_24 AC 74 ms
51,980 KB
testcase_25 AC 75 ms
51,976 KB
testcase_26 AC 88 ms
52,584 KB
testcase_27 AC 50 ms
50,396 KB
testcase_28 AC 91 ms
53,020 KB
testcase_29 AC 78 ms
52,044 KB
testcase_30 AC 88 ms
52,468 KB
testcase_31 AC 59 ms
50,132 KB
testcase_32 AC 50 ms
50,132 KB
testcase_33 AC 75 ms
52,208 KB
testcase_34 AC 76 ms
52,144 KB
testcase_35 AC 91 ms
50,444 KB
testcase_36 AC 77 ms
52,220 KB
testcase_37 AC 82 ms
52,712 KB
testcase_38 AC 82 ms
52,700 KB
testcase_39 AC 90 ms
52,680 KB
testcase_40 AC 41 ms
49,404 KB
testcase_41 AC 43 ms
49,464 KB
testcase_42 AC 85 ms
52,800 KB
testcase_43 AC 90 ms
52,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        if (n == 1) {
            System.out.println(0);
            return;
        }
        boolean[] isNotPrimes = new boolean[n + 1];
        HashSet<Integer> squares = new HashSet<>();
        int ans = 0;
        for (int i = 2; i <= n; i++) {
            if (!isNotPrimes[i]) {
                for (int j = 2; j * i <= n; j++) {
                    isNotPrimes[j * i] = true;
                }
                if (squares.contains(i + 2)) {
                    ans += 2;
                }
                if (i <= Math.sqrt(Integer.MAX_VALUE)) {
                    squares.add(i * i);
                }
            }
        }
        System.out.println(ans + 1);
    }
    
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0