結果

問題 No.551 夏休みの思い出(2)
ユーザー shotoyooshotoyoo
提出日時 2021-06-14 00:49:46
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,794 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 87,320 KB
実行使用メモリ 79,496 KB
最終ジャッジ日時 2023-08-25 04:02:41
合計ジャッジ時間 9,367 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
72,024 KB
testcase_01 AC 73 ms
71,820 KB
testcase_02 AC 76 ms
71,528 KB
testcase_03 AC 110 ms
78,624 KB
testcase_04 AC 126 ms
78,596 KB
testcase_05 AC 126 ms
79,152 KB
testcase_06 AC 126 ms
79,496 KB
testcase_07 AC 70 ms
71,536 KB
testcase_08 AC 71 ms
71,828 KB
testcase_09 AC 73 ms
71,916 KB
testcase_10 AC 73 ms
71,540 KB
testcase_11 AC 76 ms
76,228 KB
testcase_12 AC 72 ms
72,024 KB
testcase_13 AC 72 ms
71,880 KB
testcase_14 AC 72 ms
71,760 KB
testcase_15 AC 72 ms
71,884 KB
testcase_16 AC 74 ms
71,828 KB
testcase_17 AC 83 ms
76,596 KB
testcase_18 AC 84 ms
76,628 KB
testcase_19 AC 86 ms
76,564 KB
testcase_20 AC 89 ms
77,052 KB
testcase_21 AC 91 ms
77,024 KB
testcase_22 AC 84 ms
76,640 KB
testcase_23 AC 82 ms
76,644 KB
testcase_24 AC 85 ms
76,892 KB
testcase_25 AC 80 ms
76,416 KB
testcase_26 AC 85 ms
76,680 KB
testcase_27 TLE -
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 #

import sys
input = lambda : sys.stdin.readline().rstrip()


sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

### 離散対数問題
def gcd2(a, b):
    """a*x + b*y = gcd(a,b)なるx,yも求める
    """
    l = []
    while b:
        l.append(divmod(a,b))
        a, b = b, a%b
    x, y = 1, 0
    for aa,bb in l[::-1]:
        x, y = y, x - aa*y
    return a, x, y

def modinv(x, M):
    """素数ではないM、Mと互いに素なxに対し
    x * y == 1 mod M なるyを求める
    """
    a,xx,yy = gcd2(x,M)
    return a,xx%M
def dlog(x,y,M):
    """互いに素なx,Mに対してpow(x,i,M)==y なる最小のiを返す
    存在しない場合Noneを返す
    """
    l = int(M**0.5)+1
    d = {}
    v = 1
    for i in range(l+1):
        d[v] = i
        v *= x
        v %= M
    v = y
    _,tmp = modinv(pow(x,l,M), M)
    for i in range(l+1):
        if v in d:
            return i*l + d[v]
        v *= tmp
        v %= M
    return None

def sub(a,b,c):
    ainv = pow(a, p-2, p)
    b = b * ainv % p
    c = c * ainv % p
    v = (b*b*inv4 - c)%p
    if v==0:
        return (-b*inv2)%p
    res = dlog(r,v,p)
#     print(r,v,res)
    if res is None:
        return -1
    elif res%2==1:
        return -1
    kk1 = res//2
    kk2 = (res+p-1)//2
    bb = b * inv2 % p
    x1 = pow(r, kk1, p) - bb
    x2 = pow(r, kk2, p) - bb
#     print(res, v, r, kk1, kk2)
    return " ".join((map(str,sorted((x1%p, x2%p)))))
p,r = list(map(int, input().split()))
q = int(input())
inv4 = pow(4, p-2, p)
inv2 = pow(2, p-2, p)
ans = []
for i in range(q):
    a,b,c = map(int, input().split())
    res = sub(a,b,c)
    ans.append(res)
    
write("\n".join(map(str, ans)))
0