結果

問題 No.551 夏休みの思い出(2)
ユーザー nisesansuunisesansuu
提出日時 2017-09-14 13:39:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,806 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 821,552 KB
最終ジャッジ日時 2024-04-25 09:14:57
合計ジャッジ時間 9,304 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
11,008 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 36 ms
11,008 KB
testcase_04 AC 43 ms
10,880 KB
testcase_05 AC 65 ms
11,008 KB
testcase_06 AC 78 ms
10,880 KB
testcase_07 AC 27 ms
11,136 KB
testcase_08 AC 27 ms
11,008 KB
testcase_09 AC 29 ms
11,136 KB
testcase_10 AC 29 ms
10,880 KB
testcase_11 AC 32 ms
11,008 KB
testcase_12 AC 28 ms
10,880 KB
testcase_13 AC 28 ms
10,880 KB
testcase_14 AC 28 ms
10,880 KB
testcase_15 AC 27 ms
10,880 KB
testcase_16 AC 27 ms
10,880 KB
testcase_17 AC 137 ms
47,524 KB
testcase_18 AC 417 ms
157,092 KB
testcase_19 AC 327 ms
92,924 KB
testcase_20 AC 416 ms
157,220 KB
testcase_21 AC 525 ms
157,088 KB
testcase_22 AC 60 ms
21,000 KB
testcase_23 AC 97 ms
31,676 KB
testcase_24 AC 248 ms
84,092 KB
testcase_25 AC 132 ms
47,528 KB
testcase_26 AC 238 ms
84,216 KB
testcase_27 MLE -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class ModP:
    def __init__(self,p,r):
        self.p = p
        self.r = r
        self.log = {}
        self.exp = {}
        
        self.log[0] = -1
        self.exp[-1] = 0
        x = 1
        for i in range(p-1):
            self.log[x] = i
            self.exp[i] = x
            x = (x*r)%p
    
    def multi(self,a,b):
        if a==-1 or b==-1:
            return -1
        else:
            return (a+b)%(self.p-1)
    
    def rec(self,a):
        return (-a)%(self.p-1)
    
    def plus(self,a,b):
        if a==-1:
            return b
        elif b==-1:
            return a
        else:
            return self.log[(self.exp[a]+self.exp[b])%self.p]
    
    def opp(self,a):
        if a==-1:
            return -1
        else:
            return self.log[self.p-self.exp[a]]
    
    def sqrt(self,a):
        if a==-1:
            return -1
        elif a%2==1:
            return -2
        else:
            return a/2
    
    def solve(self,a,b,c):
        a,b,c = self.log[a],self.log[b],self.log[c]
        y = self.sqrt(self.plus(self.multi(b,b),self.opp(self.multi(self.log[4%self.p],self.multi(a,c)))))
        if y==-2:
            return -1
        elif y==-1:
            return self.exp[self.multi(self.opp(b),self.rec(self.multi(self.log[2],a)))]
        else:
            x = self.exp[self.multi(self.plus(self.opp(b),y),self.rec(self.multi(self.log[2],a)))]
            y = self.exp[self.multi(self.opp(self.plus(b,y)),self.rec(self.multi(self.log[2],a)))]
            if x>y: x,y = y,x
            return "%d %d"%(x,y)

def main():
    P,R = list(map(int,input().split()))
    pclass = ModP(P,R)
    Q = int(input())
    for i in range(Q):
        a,b,c = list(map(int,input().split()))
        print(pclass.solve(a,b,c))

if __name__ == '__main__':
    main()
0