結果

問題 No.732 3PrimeCounting
ユーザー tenten
提出日時 2021-11-11 18:08:22
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,874 ms / 3,000 ms
コード長 1,901 bytes
コンパイル時間 3,482 ms
コンパイル使用メモリ 78,464 KB
実行使用メモリ 43,328 KB
最終ジャッジ日時 2024-11-23 03:12:53
合計ジャッジ時間 27,088 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        boolean[] isNotPrime = new boolean[n * 3 + 1];
        ArrayList<Integer> primes = new ArrayList<>();
        for (int i = 3; i <= n * 3; i += 2) {
            if (isNotPrime[i]) {
                continue;
            }
            for (int j = 3; j * i <= n * 3; j += 2) {
                isNotPrime[j * i] = true;
            }
            if (i <= n) {
                primes.add(i);
            }
        }
        long ans = 0;
        int[] counts = new int[n * 2 + 1];
        int prev = 0;
        for (int x : primes) {
            for (int y : primes) {
                if (y >= prev) {
                    break;
                }
                counts[y + prev]++;
            }
            for (int i = x * 2 - 6; i >= 8; i -= 2) {
                if (!isNotPrime[i + x]) {
                    ans += counts[i];
                }
            }
            prev = x;
        }
        System.out.println(ans);
    }
}
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 nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0