結果

問題 No.3307 Almost Equal
コンテスト
ユーザー Carpenters-Cat
提出日時 2025-10-22 03:30:57
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 707 bytes
コンパイル時間 541 ms
コンパイル使用メモリ 82,360 KB
実行使用メモリ 54,576 KB
最終ジャッジ日時 2025-10-22 03:31:02
合計ジャッジ時間 4,251 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 2
other AC * 3 WA * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

def floor_sum(n, m, a, b) :
	# sum_{i=0}^{n-1} floor((a * i + b) // (m)) を返す
	assert a >= 0
	if a == 0 :
		return (b // m) * n
	elif a >= m :
		ret = floor_sum(n, m, a % m, b)
		ret += (a // m) * (n * (n - 1)) // 2
		return ret
	else :
		L = b // m
		R = (a * (n - 1) + b) // m
		ret = L * n
		ret += (R - L) * (n - 1)
		# L + 1 = (a * x + b) / m -> x = ((L + 1) * m - b) / a
		ret -= floor_sum(R - L, a, m, (L + 1) * m - b)
		return ret
def calc(a, b, c, d) :
	if a * d < b * c :
		return calc(c, d, a, b)
	elif a * d == b * c :
		return -1
	else :
		return b * d - (floor_sum(b * d, 2 * d, 2 * c, d) - floor_sum(b * d, 2 * b, 2 * a, b))
a, b, c, d = map(int, input().split())
print(calc(a, b, c, d))
0