結果

問題 No.843 Triple Primes
ユーザー tentententen
提出日時 2020-08-31 22:57:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 1,034 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 77,200 KB
実行使用メモリ 45,832 KB
最終ジャッジ日時 2024-04-28 11:28:27
合計ジャッジ時間 11,448 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
41,232 KB
testcase_01 AC 224 ms
44,848 KB
testcase_02 AC 129 ms
41,760 KB
testcase_03 AC 123 ms
41,524 KB
testcase_04 AC 138 ms
41,748 KB
testcase_05 AC 120 ms
41,584 KB
testcase_06 AC 142 ms
41,816 KB
testcase_07 AC 203 ms
44,420 KB
testcase_08 AC 219 ms
44,708 KB
testcase_09 AC 233 ms
45,368 KB
testcase_10 AC 215 ms
44,052 KB
testcase_11 AC 218 ms
44,512 KB
testcase_12 AC 225 ms
44,644 KB
testcase_13 AC 226 ms
44,944 KB
testcase_14 AC 226 ms
44,804 KB
testcase_15 AC 217 ms
44,564 KB
testcase_16 AC 222 ms
44,000 KB
testcase_17 AC 111 ms
41,048 KB
testcase_18 AC 108 ms
41,240 KB
testcase_19 AC 108 ms
41,124 KB
testcase_20 AC 176 ms
42,700 KB
testcase_21 AC 163 ms
42,576 KB
testcase_22 AC 194 ms
44,052 KB
testcase_23 AC 194 ms
43,532 KB
testcase_24 AC 176 ms
43,112 KB
testcase_25 AC 173 ms
42,284 KB
testcase_26 AC 225 ms
45,832 KB
testcase_27 AC 150 ms
42,216 KB
testcase_28 AC 224 ms
44,892 KB
testcase_29 AC 184 ms
43,172 KB
testcase_30 AC 218 ms
44,720 KB
testcase_31 AC 164 ms
42,340 KB
testcase_32 AC 145 ms
42,200 KB
testcase_33 AC 173 ms
42,964 KB
testcase_34 AC 223 ms
43,804 KB
testcase_35 AC 234 ms
44,956 KB
testcase_36 AC 170 ms
42,592 KB
testcase_37 AC 209 ms
44,064 KB
testcase_38 AC 199 ms
44,436 KB
testcase_39 AC 222 ms
44,756 KB
testcase_40 AC 110 ms
41,256 KB
testcase_41 AC 109 ms
41,388 KB
testcase_42 AC 224 ms
44,412 KB
testcase_43 AC 220 ms
44,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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