結果

問題 No.732 3PrimeCounting
ユーザー htensai
提出日時 2020-01-27 10:49:33
言語 Java
(openjdk 23)
結果
AC  
実行時間 894 ms / 3,000 ms
コード長 1,286 bytes
コンパイル時間 2,450 ms
コンパイル使用メモリ 78,492 KB
実行使用メモリ 43,888 KB
最終ジャッジ日時 2024-09-14 11:51:34
合計ジャッジ時間 26,716 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[]  args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        ArrayList<Integer> primes = new ArrayList<>();
        for (int i = 3; i <= 3 * n; i += 2) {
            if (isPrime(i, primes)) {
                primes.add(i);
            }
        }
        long total = 0;
        int[] counts = new int[3 * n];
        for (int i = 2; i < primes.size(); i++) {
            int c = primes.get(i);
            if (c > n) {
                break;
            }
            int b = primes.get(i - 1);
            for (int j = 0; j < i - 1; j++) {
                int a = primes.get(j);
                counts[a + b]++;
            }
            for (int j = i + 1; j < primes.size(); j++ ) {
                int sum = primes.get(j);
                if (sum - c > 0) {
                    total += counts[sum - c];
                }
            }
        }
        System.out.println(total);
    }
    
    static boolean isPrime(int x, ArrayList<Integer> primes) {
        for (int y : primes) {
            if (y > Math.sqrt(x)) {
                break;
            }
            if (x % y == 0) {
                return false;
            }
        }
        return true;
    } 
}
0