結果

問題 No.1312 Snake Eyes
ユーザー 👑 KazunKazun
提出日時 2021-02-16 18:06:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 326 ms / 2,000 ms
コード長 3,121 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 80,320 KB
最終ジャッジ日時 2024-05-07 18:18:51
合計ジャッジ時間 6,132 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
52,352 KB
testcase_01 AC 32 ms
52,608 KB
testcase_02 AC 35 ms
52,864 KB
testcase_03 AC 33 ms
52,736 KB
testcase_04 AC 33 ms
52,608 KB
testcase_05 AC 35 ms
52,352 KB
testcase_06 AC 36 ms
52,736 KB
testcase_07 AC 35 ms
52,736 KB
testcase_08 AC 35 ms
52,352 KB
testcase_09 AC 36 ms
52,736 KB
testcase_10 AC 33 ms
52,480 KB
testcase_11 AC 34 ms
52,992 KB
testcase_12 AC 33 ms
52,992 KB
testcase_13 AC 34 ms
52,864 KB
testcase_14 AC 35 ms
52,736 KB
testcase_15 AC 34 ms
52,736 KB
testcase_16 AC 33 ms
52,864 KB
testcase_17 AC 35 ms
52,352 KB
testcase_18 AC 34 ms
52,480 KB
testcase_19 AC 35 ms
52,992 KB
testcase_20 AC 34 ms
52,736 KB
testcase_21 AC 35 ms
53,120 KB
testcase_22 AC 36 ms
52,480 KB
testcase_23 AC 38 ms
52,992 KB
testcase_24 AC 38 ms
53,120 KB
testcase_25 AC 37 ms
52,992 KB
testcase_26 AC 35 ms
52,736 KB
testcase_27 AC 37 ms
52,992 KB
testcase_28 AC 37 ms
52,864 KB
testcase_29 AC 39 ms
52,736 KB
testcase_30 AC 38 ms
52,992 KB
testcase_31 AC 38 ms
52,736 KB
testcase_32 AC 37 ms
52,992 KB
testcase_33 AC 37 ms
52,992 KB
testcase_34 AC 35 ms
53,120 KB
testcase_35 AC 35 ms
52,480 KB
testcase_36 AC 37 ms
52,864 KB
testcase_37 AC 36 ms
53,248 KB
testcase_38 AC 54 ms
61,184 KB
testcase_39 AC 39 ms
54,784 KB
testcase_40 AC 40 ms
54,784 KB
testcase_41 AC 46 ms
61,312 KB
testcase_42 AC 40 ms
54,784 KB
testcase_43 AC 40 ms
55,168 KB
testcase_44 AC 39 ms
54,912 KB
testcase_45 AC 40 ms
54,784 KB
testcase_46 AC 39 ms
54,784 KB
testcase_47 AC 41 ms
54,528 KB
testcase_48 AC 80 ms
77,440 KB
testcase_49 AC 50 ms
63,360 KB
testcase_50 AC 42 ms
60,672 KB
testcase_51 AC 40 ms
55,296 KB
testcase_52 AC 39 ms
55,296 KB
testcase_53 AC 44 ms
60,800 KB
testcase_54 AC 41 ms
55,296 KB
testcase_55 AC 38 ms
55,296 KB
testcase_56 AC 57 ms
68,480 KB
testcase_57 AC 40 ms
55,168 KB
testcase_58 AC 45 ms
62,592 KB
testcase_59 AC 46 ms
61,952 KB
testcase_60 AC 42 ms
59,904 KB
testcase_61 AC 45 ms
61,952 KB
testcase_62 AC 45 ms
61,824 KB
testcase_63 AC 35 ms
52,608 KB
testcase_64 AC 41 ms
60,928 KB
testcase_65 AC 38 ms
54,784 KB
testcase_66 AC 34 ms
53,248 KB
testcase_67 AC 34 ms
52,608 KB
testcase_68 AC 41 ms
59,904 KB
testcase_69 AC 34 ms
53,120 KB
testcase_70 AC 52 ms
65,792 KB
testcase_71 AC 44 ms
62,080 KB
testcase_72 AC 43 ms
61,440 KB
testcase_73 AC 38 ms
54,912 KB
testcase_74 AC 34 ms
52,608 KB
testcase_75 AC 35 ms
53,120 KB
testcase_76 AC 326 ms
80,320 KB
testcase_77 AC 41 ms
60,288 KB
testcase_78 AC 40 ms
55,168 KB
testcase_79 AC 47 ms
63,104 KB
testcase_80 AC 48 ms
65,664 KB
testcase_81 AC 39 ms
54,528 KB
testcase_82 AC 59 ms
69,248 KB
testcase_83 AC 76 ms
77,056 KB
testcase_84 AC 39 ms
54,912 KB
testcase_85 AC 46 ms
63,616 KB
testcase_86 AC 69 ms
74,964 KB
testcase_87 AC 39 ms
54,784 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()
    return 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