結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー kjnho
提出日時 2016-05-16 22:34:55
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 1,661 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 32,904 KB
最終ジャッジ日時 2024-10-06 04:56:24
合計ジャッジ時間 5,100 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 11 WA * 9 TLE * 1 -- * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8

from collections import defaultdict as dd
from collections import Counter
from collections import deque
from string import ascii_lowercase
import math
import array

def main():
    L,H = map(int,raw_input().split())
    root_H = math.sqrt(H)
    primes = sieve_of_eratosthenes(10**5)

    cands = [p for p in primes if L <= p**2 <= H]
    if cands:
        P = max(cands)
        PP = P * min(p for p in primes if p > P)
        if PP <= H:
            print(PP)
        else:
            print(P)

    else:
        min_factor = 1
        retval = 1
        for n in range(L,H+1):
            for p in primes:
                if n%p == 0 and n != p:
                    if p >= min_factor:
                        min_factor = p
                        retval = n
                    break
        print(retval)

def sieve_of_eratosthenes(end, typecode="L"):
    assert end > 1
    # 整数iが素数であるかをis_prime[i]が示す
    # 最初はすべてTrueで初期化しておく
    # 最終的にprimesではなくこれを返してもよい
    is_prime = array.array("B", (True for i in range(end)))
    # 0, 1はいずれも素数ではない
    is_prime[0] = False
    is_prime[1] = False
    # 素数を格納する配列
    primes = array.array(typecode)
    # 篩う
    for i in range(2, end):
        if is_prime[i]:  # iが素数であるとき
            primes.append(i)  # 素数の配列に加える
            for j in range(2 * i, end, i):  # iを超えるiの倍数について
                is_prime[j] = False  # 素数ではないため除外する
    return primes

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