結果

問題 No.843 Triple Primes
ユーザー amotoma3amotoma3
提出日時 2019-07-06 10:39:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 716 ms / 2,000 ms
コード長 4,572 bytes
コンパイル時間 4,431 ms
コンパイル使用メモリ 79,056 KB
実行使用メモリ 46,752 KB
最終ジャッジ日時 2024-09-19 14:20:04
合計ジャッジ時間 22,209 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,000 KB
testcase_01 AC 693 ms
46,608 KB
testcase_02 AC 84 ms
38,620 KB
testcase_03 AC 85 ms
38,128 KB
testcase_04 AC 86 ms
38,336 KB
testcase_05 AC 87 ms
38,080 KB
testcase_06 AC 88 ms
38,544 KB
testcase_07 AC 582 ms
46,364 KB
testcase_08 AC 613 ms
46,436 KB
testcase_09 AC 716 ms
46,724 KB
testcase_10 AC 578 ms
46,368 KB
testcase_11 AC 634 ms
46,540 KB
testcase_12 AC 658 ms
46,752 KB
testcase_13 AC 649 ms
46,192 KB
testcase_14 AC 638 ms
46,484 KB
testcase_15 AC 568 ms
46,208 KB
testcase_16 AC 588 ms
45,972 KB
testcase_17 AC 52 ms
37,152 KB
testcase_18 AC 51 ms
36,896 KB
testcase_19 AC 52 ms
37,108 KB
testcase_20 AC 218 ms
44,208 KB
testcase_21 AC 160 ms
44,048 KB
testcase_22 AC 402 ms
45,560 KB
testcase_23 AC 413 ms
45,456 KB
testcase_24 AC 239 ms
44,316 KB
testcase_25 AC 195 ms
44,812 KB
testcase_26 AC 701 ms
46,536 KB
testcase_27 AC 141 ms
41,612 KB
testcase_28 AC 651 ms
46,316 KB
testcase_29 AC 230 ms
44,760 KB
testcase_30 AC 679 ms
46,644 KB
testcase_31 AC 145 ms
43,556 KB
testcase_32 AC 128 ms
41,428 KB
testcase_33 AC 273 ms
44,864 KB
testcase_34 AC 362 ms
45,332 KB
testcase_35 AC 668 ms
46,752 KB
testcase_36 AC 181 ms
44,772 KB
testcase_37 AC 553 ms
46,092 KB
testcase_38 AC 456 ms
45,744 KB
testcase_39 AC 642 ms
46,656 KB
testcase_40 AC 53 ms
36,984 KB
testcase_41 AC 52 ms
37,200 KB
testcase_42 AC 594 ms
46,120 KB
testcase_43 AC 638 ms
46,748 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.util.Set;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * 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();
            List<Integer> primes = new Prime(n).getPrimes();
            Set<Integer> set = new TreeSet<>(primes);

            int ans = 0;
            int m = primes.size();
            for (int i = 0; i < m && primes.get(i) * primes.get(i) <= 2 * n; i++) {
                for (int j = 0; j < m; j++) {
                    if (set.contains(primes.get(i) * primes.get(i) - primes.get(j))) ans++;
                }
            }
            out.printLine(ans);
        }

    }

    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