結果

問題 No.1312 Snake Eyes
ユーザー 👑 KazunKazun
提出日時 2021-02-16 04:41:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 79,468 KB
最終ジャッジ日時 2024-05-07 18:18:37
合計ジャッジ時間 7,216 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
52,608 KB
testcase_01 AC 43 ms
52,480 KB
testcase_02 AC 42 ms
52,352 KB
testcase_03 AC 42 ms
52,608 KB
testcase_04 AC 42 ms
52,224 KB
testcase_05 AC 42 ms
52,608 KB
testcase_06 AC 43 ms
52,480 KB
testcase_07 AC 42 ms
52,224 KB
testcase_08 AC 42 ms
52,608 KB
testcase_09 AC 43 ms
52,736 KB
testcase_10 AC 43 ms
52,352 KB
testcase_11 AC 43 ms
52,224 KB
testcase_12 AC 42 ms
52,608 KB
testcase_13 AC 42 ms
52,608 KB
testcase_14 AC 42 ms
52,352 KB
testcase_15 AC 44 ms
52,480 KB
testcase_16 AC 44 ms
52,736 KB
testcase_17 AC 43 ms
52,352 KB
testcase_18 AC 43 ms
52,608 KB
testcase_19 AC 43 ms
52,608 KB
testcase_20 AC 42 ms
52,480 KB
testcase_21 AC 42 ms
52,864 KB
testcase_22 AC 43 ms
52,480 KB
testcase_23 AC 43 ms
52,992 KB
testcase_24 AC 43 ms
52,480 KB
testcase_25 AC 42 ms
52,608 KB
testcase_26 AC 43 ms
52,992 KB
testcase_27 AC 43 ms
52,352 KB
testcase_28 AC 42 ms
52,608 KB
testcase_29 AC 43 ms
52,736 KB
testcase_30 AC 44 ms
52,992 KB
testcase_31 AC 44 ms
52,480 KB
testcase_32 AC 44 ms
52,736 KB
testcase_33 AC 43 ms
52,352 KB
testcase_34 AC 43 ms
52,736 KB
testcase_35 AC 43 ms
52,480 KB
testcase_36 AC 43 ms
52,864 KB
testcase_37 AC 44 ms
53,120 KB
testcase_38 AC 161 ms
61,056 KB
testcase_39 AC 49 ms
54,912 KB
testcase_40 AC 48 ms
54,784 KB
testcase_41 AC 55 ms
61,056 KB
testcase_42 AC 49 ms
54,656 KB
testcase_43 AC 50 ms
54,912 KB
testcase_44 AC 49 ms
54,528 KB
testcase_45 AC 50 ms
54,784 KB
testcase_46 AC 49 ms
54,784 KB
testcase_47 AC 49 ms
54,784 KB
testcase_48 AC 100 ms
77,056 KB
testcase_49 AC 61 ms
62,976 KB
testcase_50 AC 55 ms
60,672 KB
testcase_51 AC 50 ms
55,680 KB
testcase_52 AC 51 ms
55,040 KB
testcase_53 AC 55 ms
60,544 KB
testcase_54 AC 51 ms
55,424 KB
testcase_55 AC 49 ms
55,168 KB
testcase_56 AC 86 ms
73,088 KB
testcase_57 AC 51 ms
55,040 KB
testcase_58 AC 58 ms
62,208 KB
testcase_59 AC 60 ms
62,080 KB
testcase_60 AC 56 ms
60,032 KB
testcase_61 AC 58 ms
61,952 KB
testcase_62 AC 57 ms
61,696 KB
testcase_63 AC 43 ms
52,864 KB
testcase_64 AC 55 ms
60,544 KB
testcase_65 AC 49 ms
54,784 KB
testcase_66 AC 44 ms
53,248 KB
testcase_67 AC 43 ms
52,992 KB
testcase_68 AC 52 ms
59,776 KB
testcase_69 AC 44 ms
52,480 KB
testcase_70 AC 67 ms
65,664 KB
testcase_71 AC 59 ms
62,080 KB
testcase_72 AC 56 ms
61,440 KB
testcase_73 AC 48 ms
54,656 KB
testcase_74 AC 43 ms
52,736 KB
testcase_75 AC 44 ms
52,608 KB
testcase_76 AC 332 ms
79,468 KB
testcase_77 AC 53 ms
60,288 KB
testcase_78 AC 49 ms
55,168 KB
testcase_79 AC 58 ms
62,976 KB
testcase_80 AC 61 ms
65,280 KB
testcase_81 AC 50 ms
54,784 KB
testcase_82 AC 79 ms
69,376 KB
testcase_83 AC 98 ms
76,800 KB
testcase_84 AC 50 ms
54,784 KB
testcase_85 AC 60 ms
63,360 KB
testcase_86 AC 91 ms
74,496 KB
testcase_87 AC 48 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):
    p=N%r
    while N:
        if N%r!=p:
            return False
        N//=r
    return True
#================================================
N=int(input())
Y=Pollard_Rho_Prime_Factorization(N)
D=divisors(Y)

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