結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー KilisameKilisame
提出日時 2016-05-20 13:42:48
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,371 bytes
コンパイル時間 2,550 ms
コンパイル使用メモリ 74,144 KB
実行使用メモリ 66,832 KB
最終ジャッジ日時 2023-08-29 01:07:10
合計ジャッジ時間 15,444 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,952 KB
testcase_01 AC 121 ms
55,720 KB
testcase_02 AC 124 ms
56,076 KB
testcase_03 AC 122 ms
55,900 KB
testcase_04 AC 121 ms
55,584 KB
testcase_05 AC 119 ms
55,716 KB
testcase_06 AC 120 ms
55,384 KB
testcase_07 AC 120 ms
55,852 KB
testcase_08 AC 121 ms
56,112 KB
testcase_09 AC 119 ms
55,700 KB
testcase_10 AC 122 ms
56,028 KB
testcase_11 AC 121 ms
55,944 KB
testcase_12 AC 123 ms
55,748 KB
testcase_13 AC 122 ms
55,376 KB
testcase_14 AC 122 ms
56,060 KB
testcase_15 AC 125 ms
55,904 KB
testcase_16 AC 161 ms
55,844 KB
testcase_17 AC 123 ms
55,976 KB
testcase_18 AC 121 ms
56,076 KB
testcase_19 AC 121 ms
55,760 KB
testcase_20 AC 153 ms
56,196 KB
testcase_21 AC 129 ms
55,944 KB
testcase_22 AC 203 ms
58,860 KB
testcase_23 AC 221 ms
58,920 KB
testcase_24 AC 243 ms
59,372 KB
testcase_25 AC 242 ms
59,464 KB
testcase_26 TLE -
testcase_27 AC 125 ms
55,856 KB
testcase_28 AC 322 ms
60,964 KB
testcase_29 AC 788 ms
64,596 KB
testcase_30 AC 274 ms
59,664 KB
testcase_31 AC 306 ms
61,112 KB
testcase_32 AC 289 ms
59,572 KB
testcase_33 AC 298 ms
59,612 KB
testcase_34 AC 340 ms
60,644 KB
testcase_35 AC 309 ms
57,600 KB
testcase_36 AC 282 ms
59,088 KB
testcase_37 AC 280 ms
59,028 KB
testcase_38 AC 257 ms
58,728 KB
testcase_39 AC 258 ms
59,284 KB
testcase_40 AC 273 ms
59,380 KB
testcase_41 AC 231 ms
58,800 KB
testcase_42 AC 185 ms
56,524 KB
testcase_43 AC 421 ms
63,656 KB
testcase_44 AC 580 ms
64,764 KB
testcase_45 AC 121 ms
55,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    /* 検査範囲数最小 */
    private long l;

    /* 検査範囲数最大 */
    private long h;

    public Main(long l, long h) {
        this.l = l;
        this.h = h;
    }

    /**
     * 素数判定。isProbablePrimeのcertaintyは適当に。
     */
    private boolean isPrime(long l) {
        BigInteger bi = new BigInteger(String.valueOf(l));
        return bi.isProbablePrime(7);
    }

    /**
     * lを素因数分解したときに1番小さい素因数を返す。
     */
    private long getMinPrimeFactor(long l) {
        /* lが素数ならlで確定 */
        if (isPrime(l)) {
            return l;
        }
        /* lが合成数ならばp(素数)とn=l/p(素数かわからない)の2数の積に分解できる。この時pとnのうち小さいほうの数が最大となるのは、p=n(両者素数)のときなので、とりあえずsqrt(l)以下の最大の素数から割り始める */
        for (long i = (long) Math.sqrt(l); i > 1; i--) {
            if (isPrime(i) && l % i == 0) {
                /* 割り切れた場合、pと、nの素因数のうちの最小のもの、を比較して小さいほうがこの数の最小の素因数 */
                long quotient = l / i;
                return Math.min(i, getMinPrimeFactor(quotient));
            }
        }
        /* 素数でないならば、必ずどこかで割り切れる素因数が存在するので、ここに到達することはない。 */
        throw new IllegalArgumentException();
    }

    private void execute() {
        for (long i = (long) Math.sqrt(h); i > 1; i--) {
            if (isPrime(i)) {
                for (long j = h; j >= l; j--) {
                    if (j % i == 0 && getMinPrimeFactor(j / i) >= i) {
                        System.out.println(j);
                        System.exit(0);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String line = null;
        while (cin.hasNext()) {
            line = cin.nextLine();
        }
        cin.close();
        String[] lines = line.split(" ");
        Main prime = new Main(Long.parseLong(lines[0]), Long.parseLong(lines[1]));
        prime.execute();
    }
}
0