結果

問題 No.551 夏休みの思い出(2)
ユーザー 👑 KazunKazun
提出日時 2020-09-01 02:29:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 354 ms / 4,000 ms
コード長 4,080 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 80,256 KB
最終ジャッジ日時 2024-04-28 12:22:02
合計ジャッジ時間 11,628 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
55,040 KB
testcase_01 AC 61 ms
63,872 KB
testcase_02 AC 73 ms
68,992 KB
testcase_03 AC 132 ms
77,440 KB
testcase_04 AC 179 ms
78,336 KB
testcase_05 AC 291 ms
80,256 KB
testcase_06 AC 235 ms
78,848 KB
testcase_07 AC 66 ms
65,536 KB
testcase_08 AC 69 ms
67,456 KB
testcase_09 AC 62 ms
64,384 KB
testcase_10 AC 67 ms
66,816 KB
testcase_11 AC 70 ms
68,352 KB
testcase_12 AC 60 ms
64,384 KB
testcase_13 AC 69 ms
67,328 KB
testcase_14 AC 62 ms
64,768 KB
testcase_15 AC 72 ms
68,224 KB
testcase_16 AC 62 ms
64,512 KB
testcase_17 AC 69 ms
67,072 KB
testcase_18 AC 72 ms
68,736 KB
testcase_19 AC 70 ms
67,712 KB
testcase_20 AC 72 ms
67,840 KB
testcase_21 AC 77 ms
71,168 KB
testcase_22 AC 74 ms
68,992 KB
testcase_23 AC 75 ms
69,888 KB
testcase_24 AC 76 ms
69,760 KB
testcase_25 AC 70 ms
68,608 KB
testcase_26 AC 76 ms
68,736 KB
testcase_27 AC 354 ms
79,360 KB
testcase_28 AC 244 ms
78,352 KB
testcase_29 AC 339 ms
78,976 KB
testcase_30 AC 336 ms
79,616 KB
testcase_31 AC 268 ms
79,104 KB
testcase_32 AC 315 ms
79,232 KB
testcase_33 AC 268 ms
78,848 KB
testcase_34 AC 352 ms
79,320 KB
testcase_35 AC 255 ms
78,944 KB
testcase_36 AC 244 ms
79,432 KB
testcase_37 AC 276 ms
78,080 KB
testcase_38 AC 328 ms
80,204 KB
testcase_39 AC 276 ms
78,592 KB
testcase_40 AC 258 ms
78,592 KB
testcase_41 AC 321 ms
78,800 KB
testcase_42 AC 275 ms
78,848 KB
testcase_43 AC 254 ms
78,600 KB
testcase_44 AC 255 ms
79,104 KB
testcase_45 AC 250 ms
78,824 KB
testcase_46 AC 340 ms
79,976 KB
testcase_47 AC 48 ms
55,040 KB
testcase_48 AC 49 ms
54,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Modulo_Error(Exception):
    pass

class Modulo():
    def __init__(self,a,n):
        self.a=a%n
        self.n=n

    def __str__(self):
        return "{} (mod {})".format(self.a,self.n)

    #+,-
    def __pos__(self):
        return self

    def __neg__(self):
        return  Modulo(-self.a,self.n)

    #等号,不等号
    def __eq__(self,other):
        if isinstance(other,Modulo):
            return (self.a==other.a) and (self.n==other.n)
        elif isinstance(other,int):
            return (self-other).a==0

    def __neq__(self,other):
        return not(self==other)

    #加法
    def __add__(self,other):
        if isinstance(other,Modulo):
            if self.n!=other.n:
                raise Modulo_Error("異なる法同士の演算です.")
            return Modulo(self.a+other.a,self.n)
        elif isinstance(other,int):
            return Modulo(self.a+other,self.n)

    def __radd__(self,other):
        if isinstance(other,int):
            return Modulo(self.a+other,self.n)

    #減法
    def __sub__(self,other):
        return self+(-other)

    def __rsub__(self,other):
        if isinstance(other,int):
            return -self+other

    #乗法
    def __mul__(self,other):
        if isinstance(other,Modulo):
            if self.n!=other.n:
                raise Modulo_Error("異なる法同士の演算です.")
            return Modulo(self.a*other.a,self.n)
        elif isinstance(other,int):
            return Modulo(self.a*other,self.n)

    def __rmul__(self,other):
        if isinstance(other,int):
            return Modulo(self.a*other,self.n)

    #Modulo逆数
    def inverse(self):
        return self.Modulo_Inverse()

    def Modulo_Inverse(self):
        x0, y0, x1, y1 = 1, 0, 0, 1
        a,b=self.a,self.n
        while b != 0:
            q, a, b = a // b, b, a % b
            x0, x1 = x1, x0 - q * x1
            y0, y1 = y1, y0 - q * y1

        if a!=1:
            raise Modulo_Error("{}の逆数が存在しません".format(self))
        else:
            return Modulo(x0,self.n)

    #除法
    def __truediv__(self,other):
        return self*(other.Modulo_Inverse())

    def __rtruediv__(self,other):
        return other*(self.Modulo_Inverse())

    #累乗
    def __pow__(self,m):
        u=abs(m)

        r=Modulo(1,self.n)

        while u>0:
            if u%2==1:
                r*=self
            self*=self
            u=u>>1

        if m>=0:
            return r
        else:
            return r.Modulo_Inverse()

#ルジャンドル記号
def Legendre(X):
    """ルジャンドル記号(a/p)を返す.

    ※法が素数のときのみ成立する.
    """

    if X==0:
        return 0
    elif X**((X.n-1)//2)==1:
        return 1
    else:
        return -1

#根号
def sqrt(X,All=False):
    """X=a (mod p)のとき,r*r=a (mod p)を満たすrを返す.

    ※法pが素数のときのみ有向
    ※存在しないときはNoneが返り値
    """
    if Legendre(X)==-1:
        return None

    from random import randint as ri
    if X==0:
        return X
    elif X.n==2:
        return X
    elif X.n%4==3:
        return X**((X.n+1)//4)

    p=X.n
    u=2
    s=1
    while (p-1)%(2*u)==0:
        u*=2
        s+=1
    q=(p-1)//u

    z=Modulo(0,p)
    while z**((p-1)//2)!=-1:
        z=Modulo(ri(1,p-1),p)

    m,c,t,r=s,z**q,X**q,X**((q+1)//2)
    while m>1:
        if t**(2**(m-2))==1:
            c=c*c
            m=m-1
        else:
            c,t,r,m=c*c,c*c*t,c*r,m-1

    if All:
        return (r,-r)
    else:
        return r
#================================================
P,R=map(int,input().split())
Q=int(input())
X=[""]*Q

for i in range(Q):
    A,B,C=map(lambda x:Modulo(int(x),P),input().split())
    D=B*B-4*A*C

    L=Legendre(D)
    if L==0:
        X[i]=str((-B/(2*A)).a)
    elif L==1:
        T=sqrt(D)
        alpha=((-B+T)/(2*A)).a
        beta=((-B-T)/(2*A)).a

        if alpha>beta:
            alpha,beta=beta,alpha
        X[i]=" ".join(map(str,[alpha,beta]))
    else:
        X[i]="-1"
print("\n".join(X))
0