結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-05-14 13:11:25
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 44 ms / 1,000 ms
コード長 1,510 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 11,024 KB
実行使用メモリ 8,976 KB
最終ジャッジ日時 2023-08-29 00:59:08
合計ジャッジ時間 2,967 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,920 KB
testcase_01 AC 19 ms
8,844 KB
testcase_02 AC 21 ms
8,848 KB
testcase_03 AC 20 ms
8,896 KB
testcase_04 AC 19 ms
8,820 KB
testcase_05 AC 19 ms
8,968 KB
testcase_06 AC 19 ms
8,772 KB
testcase_07 AC 19 ms
8,916 KB
testcase_08 AC 19 ms
8,840 KB
testcase_09 AC 20 ms
8,768 KB
testcase_10 AC 19 ms
8,852 KB
testcase_11 AC 19 ms
8,844 KB
testcase_12 AC 19 ms
8,808 KB
testcase_13 AC 19 ms
8,912 KB
testcase_14 AC 19 ms
8,916 KB
testcase_15 AC 19 ms
8,824 KB
testcase_16 AC 20 ms
8,848 KB
testcase_17 AC 19 ms
8,776 KB
testcase_18 AC 19 ms
8,916 KB
testcase_19 AC 19 ms
8,776 KB
testcase_20 AC 19 ms
8,808 KB
testcase_21 AC 19 ms
8,920 KB
testcase_22 AC 20 ms
8,892 KB
testcase_23 AC 20 ms
8,776 KB
testcase_24 AC 26 ms
8,768 KB
testcase_25 AC 32 ms
8,824 KB
testcase_26 AC 34 ms
8,896 KB
testcase_27 AC 26 ms
8,960 KB
testcase_28 AC 27 ms
8,820 KB
testcase_29 AC 34 ms
8,772 KB
testcase_30 AC 32 ms
8,824 KB
testcase_31 AC 25 ms
8,776 KB
testcase_32 AC 26 ms
8,888 KB
testcase_33 AC 26 ms
8,912 KB
testcase_34 AC 25 ms
8,916 KB
testcase_35 AC 25 ms
8,916 KB
testcase_36 AC 22 ms
8,944 KB
testcase_37 AC 31 ms
8,820 KB
testcase_38 AC 25 ms
8,976 KB
testcase_39 AC 44 ms
8,776 KB
testcase_40 AC 43 ms
8,948 KB
testcase_41 AC 41 ms
8,776 KB
testcase_42 AC 30 ms
8,916 KB
testcase_43 AC 30 ms
8,768 KB
testcase_44 AC 28 ms
8,948 KB
testcase_45 AC 20 ms
8,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import math
import random


def find_min_factor(n):
    assert n > 0
    if n % 2 == 0:
        return 2
    for i in range(3, n, 2):
        if i * i > n:
            return n
        elif n % i == 0:
            return i


def is_prime_miller_rabin(n, k=30):
    assert n > 0
    if n == 2:
        return True
    elif n == 1 or n % 2 == 0:
        return False
    d = n - 1
    s = 0
    while d % 2 == 0:
        d //= 2
        s += 1
    for i in range(k):
        a = random.randint(1, n - 1)
        if pow(a, d, n) != 1:
            for r in range(0, s):
                if pow(a, d * 2 ** r, n) == n - 1:
                    break
            else:
                return False
    return True


def solve(l, h):
    answer = 0
    max_min_factor = 0
    # c = p * d
    #   c (>= 4) : a composite number
    #   p (>= 2) : a prime number
    #   d (>= 2) : an integer
    for p in range(2, math.floor(math.sqrt(h)) + 1)[:: -1]:
        if p < max_min_factor:
            break
        elif h // p * p < l or not is_prime_miller_rabin(p):
            continue
        for d in range(p, h // p + 1)[:: -1]:
            c = p * d
            if l > c:
                break
            f = find_min_factor(c)
            if max_min_factor < f or max_min_factor == f and answer < c:
                max_min_factor = f
                answer = c
    return answer


def main():
    l, h = map(int, input().split())
    print(solve(l, h))


if __name__ == '__main__':
    main()
0