結果

問題 No.843 Triple Primes
ユーザー htensaihtensai
提出日時 2019-12-09 23:56:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 989 bytes
コンパイル時間 2,429 ms
コンパイル使用メモリ 73,188 KB
実行使用メモリ 62,056 KB
最終ジャッジ日時 2023-09-05 13:13:44
合計ジャッジ時間 14,805 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
56,192 KB
testcase_01 AC 245 ms
61,292 KB
testcase_02 AC 139 ms
56,648 KB
testcase_03 AC 141 ms
55,844 KB
testcase_04 AC 142 ms
56,184 KB
testcase_05 AC 142 ms
55,452 KB
testcase_06 AC 143 ms
56,536 KB
testcase_07 AC 234 ms
59,076 KB
testcase_08 AC 247 ms
59,004 KB
testcase_09 AC 262 ms
61,272 KB
testcase_10 AC 245 ms
60,540 KB
testcase_11 AC 253 ms
59,564 KB
testcase_12 AC 262 ms
61,736 KB
testcase_13 AC 259 ms
61,704 KB
testcase_14 AC 260 ms
61,556 KB
testcase_15 AC 244 ms
58,908 KB
testcase_16 AC 243 ms
58,976 KB
testcase_17 AC 119 ms
55,984 KB
testcase_18 AC 119 ms
55,896 KB
testcase_19 AC 119 ms
55,968 KB
testcase_20 AC 197 ms
56,820 KB
testcase_21 AC 166 ms
56,716 KB
testcase_22 AC 226 ms
58,856 KB
testcase_23 AC 231 ms
57,340 KB
testcase_24 AC 208 ms
56,996 KB
testcase_25 AC 197 ms
56,852 KB
testcase_26 AC 242 ms
61,364 KB
testcase_27 AC 162 ms
56,528 KB
testcase_28 AC 240 ms
62,056 KB
testcase_29 AC 202 ms
59,284 KB
testcase_30 AC 243 ms
61,676 KB
testcase_31 AC 169 ms
56,716 KB
testcase_32 AC 163 ms
56,540 KB
testcase_33 AC 207 ms
59,404 KB
testcase_34 AC 231 ms
59,440 KB
testcase_35 AC 249 ms
60,984 KB
testcase_36 AC 184 ms
57,012 KB
testcase_37 AC 223 ms
59,100 KB
testcase_38 AC 221 ms
58,776 KB
testcase_39 AC 241 ms
61,408 KB
testcase_40 AC 117 ms
56,572 KB
testcase_41 AC 118 ms
56,044 KB
testcase_42 AC 234 ms
59,288 KB
testcase_43 AC 235 ms
59,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        TreeSet<Integer> primeSet = new TreeSet<>();
        for (int i = 2; i <= n; i++) {
            if (isPrime(i, primeSet)) {
                primeSet.add(i);
            }
        }
        int count = 0;
        for (int x : primeSet) {
            int y = (int)(Math.sqrt(2 + x));
            if (primeSet.contains(y) && y * y == 2 + x) {
                if (x == 2) {
                    count++;
                } else {
                    count += 2;
                }
            }
        }
        System.out.println(count);
    }
    
    static boolean isPrime(int x, TreeSet<Integer> set) {
        for (int y : set) {
            if (Math.sqrt(x) < y) {
                return true;
            }
            if (x % y == 0) {
                return false;
            }
        }
        return true;
    }
}
0