結果

問題 No.1312 Snake Eyes
ユーザー 👑 KazunKazun
提出日時 2021-02-16 18:12:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 3,140 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 80,924 KB
最終ジャッジ日時 2024-05-07 18:19:11
合計ジャッジ時間 5,873 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,580 KB
testcase_01 AC 36 ms
53,668 KB
testcase_02 AC 35 ms
53,916 KB
testcase_03 AC 34 ms
53,812 KB
testcase_04 AC 35 ms
53,984 KB
testcase_05 AC 34 ms
53,604 KB
testcase_06 AC 35 ms
52,796 KB
testcase_07 AC 32 ms
53,136 KB
testcase_08 AC 33 ms
53,880 KB
testcase_09 AC 36 ms
53,184 KB
testcase_10 AC 35 ms
53,312 KB
testcase_11 AC 36 ms
53,344 KB
testcase_12 AC 35 ms
53,072 KB
testcase_13 AC 35 ms
53,256 KB
testcase_14 AC 37 ms
54,380 KB
testcase_15 AC 36 ms
53,924 KB
testcase_16 AC 35 ms
53,536 KB
testcase_17 AC 35 ms
53,336 KB
testcase_18 AC 34 ms
53,028 KB
testcase_19 AC 35 ms
53,824 KB
testcase_20 AC 35 ms
53,736 KB
testcase_21 AC 35 ms
53,496 KB
testcase_22 AC 33 ms
53,476 KB
testcase_23 AC 35 ms
53,792 KB
testcase_24 AC 36 ms
54,008 KB
testcase_25 AC 35 ms
53,900 KB
testcase_26 AC 33 ms
53,456 KB
testcase_27 AC 34 ms
54,448 KB
testcase_28 AC 34 ms
53,760 KB
testcase_29 AC 33 ms
52,872 KB
testcase_30 AC 34 ms
53,848 KB
testcase_31 AC 35 ms
53,804 KB
testcase_32 AC 35 ms
53,720 KB
testcase_33 AC 34 ms
53,300 KB
testcase_34 AC 36 ms
54,032 KB
testcase_35 AC 34 ms
54,552 KB
testcase_36 AC 36 ms
54,172 KB
testcase_37 AC 36 ms
54,904 KB
testcase_38 AC 44 ms
62,968 KB
testcase_39 AC 38 ms
56,196 KB
testcase_40 AC 39 ms
55,284 KB
testcase_41 AC 43 ms
63,276 KB
testcase_42 AC 40 ms
55,760 KB
testcase_43 AC 40 ms
55,360 KB
testcase_44 AC 39 ms
55,984 KB
testcase_45 AC 39 ms
55,552 KB
testcase_46 AC 39 ms
55,708 KB
testcase_47 AC 38 ms
55,268 KB
testcase_48 AC 77 ms
77,236 KB
testcase_49 AC 49 ms
63,488 KB
testcase_50 AC 44 ms
61,056 KB
testcase_51 AC 42 ms
55,680 KB
testcase_52 AC 40 ms
55,680 KB
testcase_53 AC 44 ms
61,184 KB
testcase_54 AC 41 ms
55,680 KB
testcase_55 AC 41 ms
55,552 KB
testcase_56 AC 58 ms
68,992 KB
testcase_57 AC 39 ms
55,424 KB
testcase_58 AC 48 ms
62,592 KB
testcase_59 AC 46 ms
62,464 KB
testcase_60 AC 40 ms
60,544 KB
testcase_61 AC 46 ms
62,020 KB
testcase_62 AC 45 ms
62,208 KB
testcase_63 AC 35 ms
52,864 KB
testcase_64 AC 43 ms
60,800 KB
testcase_65 AC 39 ms
55,424 KB
testcase_66 AC 35 ms
53,376 KB
testcase_67 AC 34 ms
52,992 KB
testcase_68 AC 40 ms
60,032 KB
testcase_69 AC 35 ms
52,736 KB
testcase_70 AC 52 ms
66,048 KB
testcase_71 AC 47 ms
62,848 KB
testcase_72 AC 47 ms
61,696 KB
testcase_73 AC 41 ms
55,040 KB
testcase_74 AC 37 ms
52,608 KB
testcase_75 AC 36 ms
53,248 KB
testcase_76 AC 273 ms
80,924 KB
testcase_77 AC 43 ms
61,420 KB
testcase_78 AC 40 ms
56,712 KB
testcase_79 AC 45 ms
64,548 KB
testcase_80 AC 50 ms
67,600 KB
testcase_81 AC 39 ms
55,356 KB
testcase_82 AC 61 ms
69,988 KB
testcase_83 AC 77 ms
77,648 KB
testcase_84 AC 40 ms
55,296 KB
testcase_85 AC 47 ms
63,744 KB
testcase_86 AC 67 ms
74,812 KB
testcase_87 AC 39 ms
55,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#Miller-Rabinの素数判定法
def Miller_Rabin_Primality_Test(N,Times=20):
    """Miller-Rabinによる整数Nの素数判定を行う.

    N:整数
    ※:Trueは正確にはProbably Trueである(Falseは確定False).
    """
    from random import randint as ri

    if N==2:
        return True

    if N==1 or N%2==0:
        return False

    q=N-1
    k=0
    while q&1==0:
        k+=1
        q>>=1

    for _ in range(Times):
        m=ri(2,N-1)
        y=pow(m,q,N)
        if y==1:
            continue

        flag=True
        for i in range(k):
            if (y+1)%N==0:
                flag=False
                break

            y*=y
            y%=N

        if flag:
            return False
    return True

#ポラード・ローアルゴリズムによって素因数を発見する
#参考元:https://judge.yosupo.jp/submission/6131
def Find_Factor_Rho(N):
    if N==1:
        return 1
    from math import gcd
    m=1<<(N.bit_length()//8+1)

    for c in range(1,99):
        f=lambda x:(x*x+c)%N
        y,r,q,g=2,1,1,1
        while g==1:
            x=y
            for i in range(r):
                y=f(y)
            k=0
            while k<r and g==1:
                for i in range(min(m, r - k)):
                    y=f(y)
                    q=q*abs(x - y)%N
                g=gcd(q,N)
                k+=m
            r <<=1

        if g<N:
            if Miller_Rabin_Primality_Test(g):
                return g
            elif Miller_Rabin_Primality_Test(N//g):
                return N//g
    return N

#ポラード・ローアルゴリズムによる素因数分解
#参考元:https://judge.yosupo.jp/submission/6131
def Pollard_Rho_Prime_Factorization(N):
    I=2
    res=[]
    while I*I<=N:
        if N%I==0:
            k=0
            while N%I==0:
                k+=1
                N//=I
            res.append([I,k])

        I+=1+(I%2)

        if I!=101 or N<2**20:
            continue

        while N>1:
            if Miller_Rabin_Primality_Test(N):
                res.append([N,1])
                N=1
            else:
                j=Find_Factor_Rho(N)
                k=0
                while N%j==0:
                    N//=j
                    k+=1
                res.append([j,k])
    if N>1:
        res.append([N,1])
    res.sort(key=lambda x:x[0])
    return res

def integer_product(T):
    a=1
    for t in T:
        a*=t
    return a

from itertools import product
def divisors(X):
    E=[[pow(p,k) for k in range(e+1)] for p,e in X]
    A=[integer_product(t) for t in product(*E)]
    A.sort()
    for a in A:
        yield a

def check(N,r):
    if r in Memo:
        return Memo[r]
    p=N%r
    while N:
        if N%r!=p:
            Memo[r]=False
            return False
        N//=r

    Memo[r]=True
    return True
#================================================
N=int(input())
Y=Pollard_Rho_Prime_Factorization(N)
D=divisors(Y)
Memo={}

X=N+1
for a in D:
    M=N//a

    Y=Pollard_Rho_Prime_Factorization(M-1)
    B=divisors(Y)

    for p in B:
        if p>=X:
            break

        if 2<=p and check(N,p):
            X=p
print(X)
0