結果

問題 No.843 Triple Primes
ユーザー tentententen
提出日時 2023-08-01 13:12:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 536 ms / 2,000 ms
コード長 2,262 bytes
コンパイル時間 2,192 ms
コンパイル使用メモリ 85,460 KB
実行使用メモリ 63,600 KB
最終ジャッジ日時 2024-04-19 12:40:31
合計ジャッジ時間 16,985 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
50,420 KB
testcase_01 AC 526 ms
63,600 KB
testcase_02 AC 64 ms
51,116 KB
testcase_03 AC 60 ms
51,112 KB
testcase_04 AC 87 ms
51,840 KB
testcase_05 AC 73 ms
51,000 KB
testcase_06 AC 85 ms
51,864 KB
testcase_07 AC 403 ms
58,336 KB
testcase_08 AC 421 ms
58,368 KB
testcase_09 AC 528 ms
58,860 KB
testcase_10 AC 424 ms
57,900 KB
testcase_11 AC 536 ms
61,500 KB
testcase_12 AC 500 ms
60,068 KB
testcase_13 AC 463 ms
57,872 KB
testcase_14 AC 487 ms
58,000 KB
testcase_15 AC 439 ms
58,456 KB
testcase_16 AC 423 ms
58,104 KB
testcase_17 AC 47 ms
50,544 KB
testcase_18 AC 45 ms
50,548 KB
testcase_19 AC 47 ms
50,164 KB
testcase_20 AC 282 ms
59,532 KB
testcase_21 AC 215 ms
58,660 KB
testcase_22 AC 350 ms
59,344 KB
testcase_23 AC 366 ms
59,588 KB
testcase_24 AC 286 ms
59,404 KB
testcase_25 AC 282 ms
59,620 KB
testcase_26 AC 478 ms
57,880 KB
testcase_27 AC 109 ms
52,732 KB
testcase_28 AC 466 ms
57,616 KB
testcase_29 AC 286 ms
59,472 KB
testcase_30 AC 477 ms
57,948 KB
testcase_31 AC 137 ms
55,060 KB
testcase_32 AC 107 ms
53,052 KB
testcase_33 AC 279 ms
59,604 KB
testcase_34 AC 346 ms
59,412 KB
testcase_35 AC 502 ms
58,332 KB
testcase_36 AC 256 ms
59,472 KB
testcase_37 AC 419 ms
57,872 KB
testcase_38 AC 369 ms
56,324 KB
testcase_39 AC 479 ms
57,892 KB
testcase_40 AC 49 ms
50,168 KB
testcase_41 AC 47 ms
50,492 KB
testcase_42 AC 450 ms
58,016 KB
testcase_43 AC 462 ms
58,192 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];
        TreeSet<Integer> primes = new TreeSet<>();
        ArrayList<Integer> squares = new ArrayList<>();
        for (int i = 2; i <= n; i++) {
            if (!isNotPrimes[i]) {
                primes.add(i);
                if ((long)i * i <= 2 * n) {
                    squares.add(i * i);
                }
                for (int j = 2; j * i <= n; j++) {
                    isNotPrimes[j * i] = true;
                }
            }
        }
        int ans = 0;
        for (int x : squares) {
            for (int y : primes) {
                if (x <= y) {
                    break;
                }
                if (primes.contains(x - y)) {
                    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