結果

問題 No.843 Triple Primes
ユーザー amotoma3amotoma3
提出日時 2019-07-06 10:51:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 4,407 bytes
コンパイル時間 2,711 ms
コンパイル使用メモリ 78,984 KB
実行使用メモリ 43,108 KB
最終ジャッジ日時 2024-09-19 14:18:14
合計ジャッジ時間 7,990 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
37,044 KB
testcase_01 AC 104 ms
42,832 KB
testcase_02 AC 48 ms
37,160 KB
testcase_03 AC 48 ms
37,324 KB
testcase_04 AC 49 ms
36,696 KB
testcase_05 AC 50 ms
37,300 KB
testcase_06 AC 50 ms
37,180 KB
testcase_07 AC 115 ms
42,312 KB
testcase_08 AC 114 ms
42,596 KB
testcase_09 AC 124 ms
43,108 KB
testcase_10 AC 111 ms
42,544 KB
testcase_11 AC 123 ms
42,540 KB
testcase_12 AC 113 ms
42,900 KB
testcase_13 AC 113 ms
42,888 KB
testcase_14 AC 117 ms
42,816 KB
testcase_15 AC 114 ms
42,044 KB
testcase_16 AC 114 ms
42,504 KB
testcase_17 AC 47 ms
37,144 KB
testcase_18 AC 45 ms
36,932 KB
testcase_19 AC 45 ms
37,048 KB
testcase_20 AC 94 ms
39,928 KB
testcase_21 AC 83 ms
39,676 KB
testcase_22 AC 100 ms
41,340 KB
testcase_23 AC 104 ms
41,588 KB
testcase_24 AC 89 ms
40,116 KB
testcase_25 AC 90 ms
39,960 KB
testcase_26 AC 117 ms
42,728 KB
testcase_27 AC 73 ms
38,604 KB
testcase_28 AC 115 ms
42,780 KB
testcase_29 AC 86 ms
40,268 KB
testcase_30 AC 127 ms
42,736 KB
testcase_31 AC 83 ms
39,372 KB
testcase_32 AC 74 ms
38,336 KB
testcase_33 AC 93 ms
39,936 KB
testcase_34 AC 110 ms
41,144 KB
testcase_35 AC 133 ms
42,920 KB
testcase_36 AC 85 ms
39,340 KB
testcase_37 AC 115 ms
42,204 KB
testcase_38 AC 110 ms
41,572 KB
testcase_39 AC 117 ms
42,584 KB
testcase_40 AC 46 ms
36,976 KB
testcase_41 AC 45 ms
37,140 KB
testcase_42 AC 113 ms
42,348 KB
testcase_43 AC 114 ms
42,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.util.Set;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        No843 solver = new No843();
        solver.solve(1, in, out);
        out.close();
    }

    static class No843 {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            Set<Integer> primes = new TreeSet<>(new Prime(n).getPrimes());

            int ans = 0;
            for (int prime : primes) {
                if (prime * prime > n + 2) break;
                if (primes.contains(prime * prime - 2)) ans++;
            }
            out.printLine(n == 1 ? 0 : 2 * ans - 1);
        }

    }

    static class Prime {
        private int n;
        private boolean[] isPrime;
        private ArrayList<Integer> primes;

        public Prime(int n) {
            this.n = n;
            isPrime = new boolean[n + 1];
            primes = new ArrayList<>();
            sieve();
        }

        private void sieve() {
            for (int i = 2; i <= n; i++) {
                if (!isPrime[i]) {
                    primes.add(i);
                    for (long j = (long) i * i; j <= n; j += i) {
                        isPrime[(int) j] = true;
                    }
                }
            }
        }

        public ArrayList<Integer> getPrimes() {
            return primes;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }
}

0