結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー KilisameKilisame
提出日時 2017-07-13 13:31:26
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,299 bytes
コンパイル時間 2,144 ms
コンパイル使用メモリ 78,308 KB
実行使用メモリ 65,628 KB
最終ジャッジ日時 2024-04-16 18:31:18
合計ジャッジ時間 15,596 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
54,140 KB
testcase_01 AC 137 ms
54,376 KB
testcase_02 AC 135 ms
54,196 KB
testcase_03 AC 136 ms
54,496 KB
testcase_04 AC 133 ms
54,124 KB
testcase_05 AC 134 ms
54,200 KB
testcase_06 AC 134 ms
54,216 KB
testcase_07 AC 135 ms
54,380 KB
testcase_08 AC 134 ms
54,108 KB
testcase_09 AC 136 ms
54,280 KB
testcase_10 AC 135 ms
54,308 KB
testcase_11 AC 134 ms
54,324 KB
testcase_12 AC 136 ms
54,408 KB
testcase_13 AC 133 ms
54,416 KB
testcase_14 AC 134 ms
54,512 KB
testcase_15 AC 136 ms
54,284 KB
testcase_16 AC 175 ms
54,508 KB
testcase_17 AC 137 ms
54,352 KB
testcase_18 AC 133 ms
53,836 KB
testcase_19 AC 131 ms
54,228 KB
testcase_20 AC 163 ms
54,516 KB
testcase_21 AC 139 ms
54,284 KB
testcase_22 AC 239 ms
57,668 KB
testcase_23 AC 236 ms
57,212 KB
testcase_24 AC 247 ms
57,196 KB
testcase_25 AC 246 ms
57,052 KB
testcase_26 TLE -
testcase_27 AC 124 ms
53,416 KB
testcase_28 AC 342 ms
60,388 KB
testcase_29 AC 874 ms
64,212 KB
testcase_30 AC 289 ms
57,800 KB
testcase_31 AC 318 ms
58,380 KB
testcase_32 AC 304 ms
57,868 KB
testcase_33 AC 313 ms
57,856 KB
testcase_34 AC 360 ms
59,612 KB
testcase_35 AC 333 ms
58,704 KB
testcase_36 AC 297 ms
57,948 KB
testcase_37 AC 301 ms
58,048 KB
testcase_38 AC 285 ms
58,360 KB
testcase_39 AC 277 ms
57,868 KB
testcase_40 AC 288 ms
57,816 KB
testcase_41 AC 235 ms
57,232 KB
testcase_42 AC 187 ms
54,808 KB
testcase_43 AC 432 ms
62,484 KB
testcase_44 AC 685 ms
63,784 KB
testcase_45 AC 122 ms
52,996 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);
        long l = Long.parseLong(cin.next());
        long h = Long.parseLong(cin.next());
        cin.close();
        Main prime = new Main(l, h);
        prime.execute();
    }
}
0