結果

問題 No.36 素数が嫌い!
ユーザー yuppe19 😺
提出日時 2015-09-25 15:20:29
言語 Python2
(2.7.18)
結果
AC  
実行時間 52 ms / 5,000 ms
コード長 1,592 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 8,792 KB
最終ジャッジ日時 2024-06-27 00:30:27
合計ジャッジ時間 2,350 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python
from collections import defaultdict
from fractions import gcd
from itertools import chain
from Queue import Queue
from random import randrange

flatten = chain.from_iterable

def rabin_miller(p):
    if p < 2:
        return False
    if p != 2 and p % 2 == 0:
        return False
    s = p - 1
    while s % 2 == 0:
        s >>= 1
    for _ in xrange(10):
        a = randrange(p-1) + 1
        temp = s
        mod = pow(a, temp, p)
        while temp != p-1 and mod != 1 and mod != p-1:
            mod = (mod * mod) % p
            temp *= 2
        if mod != p-1 and temp % 2 == 0:
            return False
    return True

def pollard(n):
    if n % 2 == 0:
        return 2
    x = randrange(2, 1000000)
    c = randrange(2, 1000000)
    y = x
    d = 1
    while d == 1:
        x = (x * x + c) % n
        y = (y * y + c) % n
        y = (y * y + c) % n
        d = gcd(x-y, n)
        if d == n:
            break
    return d

# Prime Factorization by Pollard Rho alrorithm
# http://code.activestate.com/recipes/577037-pollard-rho-prime-factorization/
def factorize(n):
    que = Queue()
    res = defaultdict(int)
    if n == 1:
        res[1] = 1
        return res
    que.put(n)
    while not que.empty():
        l = que.get()
        if rabin_miller(l):
            res[l] += 1
            continue
        d = pollard(l)
        que.put(d)
        if d != l:
            que.put(l / d)
    return res

n = int(raw_input())
factored = factorize(n)
arr = map(sum, zip(*factored.items()))[1:]
cnt = arr.pop() if arr else 0
print 'YES' if cnt >= 3 else 'NO'
0