結果

問題 No.1312 Snake Eyes
ユーザー 👑 KazunKazun
提出日時 2021-03-02 13:20:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 3,113 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 80,396 KB
最終ジャッジ日時 2024-05-07 18:18:44
合計ジャッジ時間 6,694 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,992 KB
testcase_01 AC 38 ms
52,352 KB
testcase_02 AC 38 ms
52,608 KB
testcase_03 AC 38 ms
52,352 KB
testcase_04 AC 39 ms
52,608 KB
testcase_05 AC 35 ms
52,608 KB
testcase_06 AC 36 ms
52,480 KB
testcase_07 AC 43 ms
52,352 KB
testcase_08 AC 38 ms
52,608 KB
testcase_09 AC 36 ms
52,992 KB
testcase_10 AC 38 ms
52,608 KB
testcase_11 AC 39 ms
52,608 KB
testcase_12 AC 38 ms
52,608 KB
testcase_13 AC 38 ms
52,608 KB
testcase_14 AC 38 ms
52,736 KB
testcase_15 AC 38 ms
52,992 KB
testcase_16 AC 39 ms
52,864 KB
testcase_17 AC 38 ms
52,992 KB
testcase_18 AC 39 ms
52,352 KB
testcase_19 AC 38 ms
52,864 KB
testcase_20 AC 38 ms
52,736 KB
testcase_21 AC 38 ms
52,736 KB
testcase_22 AC 37 ms
52,608 KB
testcase_23 AC 38 ms
53,248 KB
testcase_24 AC 37 ms
52,864 KB
testcase_25 AC 37 ms
52,352 KB
testcase_26 AC 38 ms
52,736 KB
testcase_27 AC 38 ms
52,864 KB
testcase_28 AC 38 ms
52,992 KB
testcase_29 AC 37 ms
52,352 KB
testcase_30 AC 39 ms
53,120 KB
testcase_31 AC 39 ms
52,992 KB
testcase_32 AC 38 ms
52,864 KB
testcase_33 AC 38 ms
52,480 KB
testcase_34 AC 40 ms
52,608 KB
testcase_35 AC 40 ms
52,608 KB
testcase_36 AC 40 ms
52,864 KB
testcase_37 AC 39 ms
52,480 KB
testcase_38 AC 58 ms
61,184 KB
testcase_39 AC 43 ms
55,168 KB
testcase_40 AC 43 ms
54,784 KB
testcase_41 AC 50 ms
61,312 KB
testcase_42 AC 42 ms
54,656 KB
testcase_43 AC 42 ms
54,912 KB
testcase_44 AC 42 ms
54,656 KB
testcase_45 AC 44 ms
54,656 KB
testcase_46 AC 42 ms
55,040 KB
testcase_47 AC 44 ms
54,784 KB
testcase_48 AC 100 ms
77,312 KB
testcase_49 AC 56 ms
63,360 KB
testcase_50 AC 50 ms
60,800 KB
testcase_51 AC 46 ms
55,424 KB
testcase_52 AC 45 ms
55,296 KB
testcase_53 AC 50 ms
60,928 KB
testcase_54 AC 45 ms
55,168 KB
testcase_55 AC 46 ms
55,168 KB
testcase_56 AC 66 ms
68,352 KB
testcase_57 AC 45 ms
54,912 KB
testcase_58 AC 51 ms
62,208 KB
testcase_59 AC 51 ms
61,824 KB
testcase_60 AC 46 ms
60,288 KB
testcase_61 AC 50 ms
62,208 KB
testcase_62 AC 51 ms
61,696 KB
testcase_63 AC 38 ms
52,864 KB
testcase_64 AC 49 ms
60,928 KB
testcase_65 AC 44 ms
54,784 KB
testcase_66 AC 40 ms
53,376 KB
testcase_67 AC 39 ms
52,864 KB
testcase_68 AC 46 ms
60,032 KB
testcase_69 AC 40 ms
53,120 KB
testcase_70 AC 60 ms
65,792 KB
testcase_71 AC 50 ms
62,080 KB
testcase_72 AC 50 ms
61,440 KB
testcase_73 AC 45 ms
54,784 KB
testcase_74 AC 38 ms
52,608 KB
testcase_75 AC 40 ms
52,736 KB
testcase_76 AC 283 ms
80,396 KB
testcase_77 AC 49 ms
60,288 KB
testcase_78 AC 46 ms
55,040 KB
testcase_79 AC 54 ms
63,232 KB
testcase_80 AC 64 ms
66,304 KB
testcase_81 AC 43 ms
54,784 KB
testcase_82 AC 70 ms
70,144 KB
testcase_83 AC 93 ms
76,800 KB
testcase_84 AC 43 ms
54,784 KB
testcase_85 AC 53 ms
63,616 KB
testcase_86 AC 81 ms
74,496 KB
testcase_87 AC 44 ms
54,656 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(r):
    X=N
    p=X%r
    while X:
        if X%r!=p:
            return False
        X//=r
    return True
#================================================
N=int(input())
Y=Pollard_Rho_Prime_Factorization(N)
D=divisors(Y)
S=set()

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 (p not in S):
            S.add(p)
            if check(p):
                X=p
print(X)
0