結果

問題 No.551 夏休みの思い出(2)
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-12-12 22:33:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 657 ms / 4,000 ms
コード長 1,375 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 86,732 KB
実行使用メモリ 98,336 KB
最終ジャッジ日時 2023-09-28 16:13:17
合計ジャッジ時間 21,314 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 193 ms
83,320 KB
testcase_01 AC 195 ms
83,488 KB
testcase_02 AC 205 ms
84,068 KB
testcase_03 AC 271 ms
87,948 KB
testcase_04 AC 309 ms
88,432 KB
testcase_05 AC 386 ms
89,980 KB
testcase_06 AC 439 ms
90,960 KB
testcase_07 AC 199 ms
83,988 KB
testcase_08 AC 201 ms
83,708 KB
testcase_09 AC 204 ms
83,844 KB
testcase_10 AC 195 ms
83,840 KB
testcase_11 AC 196 ms
83,888 KB
testcase_12 AC 197 ms
83,816 KB
testcase_13 AC 195 ms
83,852 KB
testcase_14 AC 201 ms
84,028 KB
testcase_15 AC 199 ms
83,768 KB
testcase_16 AC 200 ms
83,696 KB
testcase_17 AC 203 ms
84,188 KB
testcase_18 AC 208 ms
84,156 KB
testcase_19 AC 205 ms
84,260 KB
testcase_20 AC 208 ms
84,296 KB
testcase_21 AC 208 ms
84,280 KB
testcase_22 AC 210 ms
84,148 KB
testcase_23 AC 207 ms
84,100 KB
testcase_24 AC 202 ms
84,260 KB
testcase_25 AC 205 ms
83,996 KB
testcase_26 AC 206 ms
83,736 KB
testcase_27 AC 580 ms
93,172 KB
testcase_28 AC 571 ms
92,848 KB
testcase_29 AC 578 ms
93,576 KB
testcase_30 AC 546 ms
92,708 KB
testcase_31 AC 657 ms
98,336 KB
testcase_32 AC 547 ms
92,980 KB
testcase_33 AC 576 ms
92,968 KB
testcase_34 AC 561 ms
93,644 KB
testcase_35 AC 620 ms
93,968 KB
testcase_36 AC 569 ms
93,052 KB
testcase_37 AC 535 ms
91,672 KB
testcase_38 AC 577 ms
92,176 KB
testcase_39 AC 577 ms
93,540 KB
testcase_40 AC 537 ms
92,192 KB
testcase_41 AC 539 ms
92,480 KB
testcase_42 AC 591 ms
94,132 KB
testcase_43 AC 571 ms
93,780 KB
testcase_44 AC 602 ms
92,880 KB
testcase_45 AC 574 ms
93,664 KB
testcase_46 AC 524 ms
92,248 KB
testcase_47 AC 193 ms
83,504 KB
testcase_48 AC 190 ms
83,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random
def mod_sqrt(a: int, p: int) -> int:
	if a == 0: return 0
	if p == 2: return 1
	k = (p - 1) // 2
	if pow(a, k, p) != 1: return -1
	while True:
		n = random.randint(2, p - 1)
		r = (n ** 2 - a) % p
		if r == 0: return n
		if pow(r, k, p) == p - 1: break
	k += 1
	w, x, y, z = n, 1, 1, 0
	while k:
		if k % 2:
			y, z = w * y + r * x * z, x * y + w * z
		w, x = w * w + r * x * x, 2 * w * x
		w %= p; x %= p; y %= p; z %= p
		k >>= 1
	return y
import typing
# 拡張Euclidの互除法
def extgcd(a: int, b: int, d: int = 0) -> typing.Tuple[int, int, int]:
	g = a
	if b == 0:
		x, y = 1, 0
	else:
		x, y, g = extgcd(b, a % b)
		x, y = y, x - a // b * y
	return x, y, g
 
# mod p における逆元
def invmod(a: int, p: int) -> int:
	x, y, g = extgcd(a, p)
	x %= p
	return x
p, r = map(int, input().split())
q = int(input())
for _ in range(q):
	a, b, c = map(int, input().split())
	n = ((b ** 2) * invmod(4 * a, p) % p - c) * invmod(a, p) % p
	m = mod_sqrt(n, p)
	ans = []
	if m == 0:
		tmp = (m - b * invmod(2 * a, p)) % p
		if (a * tmp ** 2 + b * tmp + c) % p == 0: ans.append(tmp)
	else:
		m2 = p - m
		x, y = (m - b * invmod(2 * a, p)) % p, (m2 - b * invmod(2 * a, p)) % p
		if x > y: x, y = y, x
		if (a * x ** 2 + b * x + c) % p == 0: ans.append(x)
		if (a * y ** 2 + b * y + c) % p == 0: ans.append(y)
	if len(ans) == 0: print(-1)
	else: print(*sorted(ans))
0