結果

問題 No.1626 三角形の構築
ユーザー 👑 KazunKazun
提出日時 2021-08-14 01:16:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 534 ms / 2,000 ms
コード長 3,162 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 81,688 KB
最終ジャッジ日時 2024-04-20 16:16:23
合計ジャッジ時間 4,887 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
61,824 KB
testcase_01 AC 134 ms
78,104 KB
testcase_02 AC 87 ms
76,892 KB
testcase_03 AC 97 ms
76,800 KB
testcase_04 AC 84 ms
77,312 KB
testcase_05 AC 86 ms
77,056 KB
testcase_06 AC 82 ms
76,672 KB
testcase_07 AC 106 ms
76,900 KB
testcase_08 AC 119 ms
77,320 KB
testcase_09 AC 137 ms
77,568 KB
testcase_10 AC 108 ms
76,612 KB
testcase_11 AC 125 ms
77,056 KB
testcase_12 AC 129 ms
77,912 KB
testcase_13 AC 110 ms
77,696 KB
testcase_14 AC 119 ms
77,428 KB
testcase_15 AC 130 ms
77,940 KB
testcase_16 AC 138 ms
77,852 KB
testcase_17 AC 125 ms
77,952 KB
testcase_18 AC 115 ms
77,244 KB
testcase_19 AC 111 ms
77,092 KB
testcase_20 AC 134 ms
77,268 KB
testcase_21 AC 119 ms
77,372 KB
testcase_22 AC 135 ms
77,312 KB
testcase_23 AC 126 ms
76,928 KB
testcase_24 AC 534 ms
81,688 KB
testcase_25 AC 40 ms
52,224 KB
testcase_26 AC 81 ms
76,800 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 Divisors_from_Prime_Factor(P,sorting=False):
    X=[1]
    for p,e in P:
        q=1
        n=len(X)
        for _ in range(e):
            q*=p
            for j in range(n):
                X.append(X[j]*q)

    if sorting: X.sort()
    return X
#==================================================
Q=int(input())
for _ in range(Q):
    S,T=map(int,input().split())

    if T%2==1:
        print(0)
        continue

    D=Divisors_from_Prime_Factor(Pollard_Rho_Prime_Factorization(S*S),True); M=len(D)
    E=[]

    for i in range(M):
        x=D[i]
        for j in range(i,M):
            y=D[j]

            if S*S<x*y*y*(x+y+y):
                break

            z=(T-2*(x+y))//2
            if y<=z and x*y*z*(x+y+z)==S*S:
                E.append((x+y,x+z,y+z))

    print(len(E))
    for t in E:
        print(*t)
0