結果

問題 No.2271 平方根の13桁精度近似計算
ユーザー shobonvipshobonvip
提出日時 2023-04-14 23:04:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,407 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 67,712 KB
最終ジャッジ日時 2024-04-18 20:40:48
合計ジャッジ時間 7,510 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
66,816 KB
testcase_01 AC 72 ms
67,328 KB
testcase_02 AC 73 ms
66,944 KB
testcase_03 AC 73 ms
66,944 KB
testcase_04 AC 73 ms
67,200 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 72 ms
66,944 KB
testcase_08 AC 72 ms
66,944 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 73 ms
66,816 KB
testcase_14 AC 75 ms
66,944 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 72 ms
66,816 KB
testcase_19 WA -
testcase_20 AC 72 ms
67,200 KB
testcase_21 AC 72 ms
67,200 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 72 ms
66,816 KB
testcase_25 AC 72 ms
66,816 KB
testcase_26 AC 71 ms
66,816 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 73 ms
66,944 KB
testcase_32 AC 72 ms
66,944 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 74 ms
67,200 KB
testcase_37 WA -
testcase_38 AC 72 ms
66,816 KB
testcase_39 AC 71 ms
66,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing

def inv_gcd(a: int, b: int) -> typing.Tuple[int, int]:
	a %= b
	if a == 0:
		return (b, 0)
	s = b
	t = a
	m0 = 0
	m1 = 1
	while t:
		u = s // t
		s -= t * u
		m0 -= m1 * u
		s, t = t, s
		m0, m1 = m1, m0
	if m0 < 0:
		m0 += b // s
	return (s, m0)

def inv_mod(x: int, m: int) -> int:
	z = inv_gcd(x, m)
	return z[1]

def crt(r: typing.List[int], m: typing.List[int]) -> typing.Tuple[int, int]:
	r0 = 0
	m0 = 1
	for r1, m1 in zip(r, m):
		r1 %= m1
		if m0 < m1:
			r0, r1 = r1, r0
			m0, m1 = m1, m0
		if m0 % m1 == 0:
			if r0 % m1 != r1:
				return (0, 0)
			continue
		g, im = inv_gcd(m0, m1)
		u1 = m1 // g
		if (r1 - r0) % g:
			return (0, 0)
		x = (r1 - r0) // g % u1 * im % u1
		r0 += x * m0
		m0 *= u1
		if r0 < 0:r0 += m0
	return (r0, m0)


n = int(input())
e = int(input())
# x^2 + y 5^e - n = 0 has integer solution ?
# e = 0 -> ok.
if e == 0:
	print(0)
	exit()


basis = 1
ee = e
saiko = 0
for i in range(e+1):
	if n % (5**i) == 0:
		saiko = i

e -= saiko
n //= 5 ** saiko

if not (n % 5 == 1 or n % 5 == 4):
	print("NaN")
	exit()

a = -1
for i in range(5):
	if (i * i - n) % 5 == 0:
		a = i
		break

assert a >= 0

for t in range(2, e+1):
	c = (a * a - n) // (5 ** (t-1))
	y = -1
	for yy in range(5):
		if (2 * a * yy + c) % 5 == 0:
			y = yy
			break
	assert y >= 0
	b = (a + 5 ** (t-1) * y) % (5 ** t)
	a = b

a *= 5 ** basis
a %= 5 ** ee
if a > 2 ** 29:
	a -= 5 ** ee

print(a)
0