結果

問題 No.2183 LCA on Rational Tree
ユーザー noya2noya2
提出日時 2023-01-07 19:58:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 956 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 18,560 KB
最終ジャッジ日時 2024-05-08 21:14:56
合計ジャッジ時間 3,926 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
18,560 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

def pfact(m):
	pf = {}
	for i in range(2,int(m**0.5)+1):
		while m%i == 0:
			pf[i] = pf.get(i,0) + 1
			m //= i
	if m>1 : pf[m]=1
	return pf

def gogo(p:int, q:int, lst:list):
	pf = pfact(q-p)
	while q-p > 1:
		x = 10 ** 18
		for i,c in pf.items():
			x = min(x, i-(p%i))
		g = gcd(p+x, q+x)
		lst.append((q-p, p, p+x))
		p, q = (p+x)//g, (q+x)//g
		newqp = q-p
		for i in pf.keys():
			pf[i] = 0
			while newqp % i == 0:
				pf[i] += 1
				newqp //= i
		#print(f"{x} 回操作して, {p}/{q}")
	lst.append((1, p, 9 * 10 ** 18))

Q = int(input())
for _ in range(Q):
	lst1 = []
	lst2 = []
	p1,q1,p2,q2 = map(int,input().split())
	gogo(p1,q1,lst1)
	gogo(p2,q2,lst2)
	g = dict()
	for i, st, ed in lst1:
		g[i] = (st, ed)
	ans1 = 0
	ans2 = 0
	#print(g)
	for i, st, ed in lst2:
		if i in g.keys():
			v = max(0, min(ed, g[i][1]) - max(st, g[i][0]))
			if v > 0:
				ans1 = max(st, g[i][0])
				ans2 = i + ans1
				break
	print(ans1, ans2)
0