結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-08-25 14:50:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 790 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 86,960 KB
実行使用メモリ 83,036 KB
最終ジャッジ日時 2023-08-25 14:50:13
合計ジャッジ時間 5,521 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
71,012 KB
testcase_01 AC 221 ms
83,036 KB
testcase_02 AC 252 ms
81,736 KB
testcase_03 AC 255 ms
81,504 KB
testcase_04 AC 235 ms
81,936 KB
testcase_05 AC 245 ms
82,032 KB
testcase_06 AC 163 ms
82,248 KB
testcase_07 AC 145 ms
82,192 KB
testcase_08 AC 146 ms
81,984 KB
testcase_09 AC 151 ms
81,964 KB
testcase_10 AC 155 ms
82,032 KB
testcase_11 AC 208 ms
81,620 KB
testcase_12 AC 205 ms
81,884 KB
testcase_13 AC 206 ms
81,756 KB
testcase_14 AC 238 ms
82,552 KB
testcase_15 AC 220 ms
81,928 KB
testcase_16 AC 171 ms
82,680 KB
testcase_17 AC 168 ms
82,788 KB
testcase_18 AC 161 ms
82,844 KB
testcase_19 AC 153 ms
82,864 KB
testcase_20 AC 173 ms
82,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
mp = map(int, sys.stdin.read().split())

# calc sum_{i=0}^{n-1} floor((ai + b) / m)
def floor_sum_unsigned(n: int, m: int, a: int, b: int) -> int:
	assert n >= 0 and m > 0 and a >= 0 and b >= 0
	ans = 0
	while True:
		if a >= m:
			q, a = divmod(a, m)
			ans += (n * (n - 1) >> 1) * q
		if b >= m:
			q, b = divmod(b, m)
			ans += n * q
		y_max = a * n + b
		if y_max < m: return ans
		n, b = divmod(y_max, m)
		m, a = a, m

def solve(n: int, d: int, m: int, s: int) -> int:
	assert n >= 0 and d > 0 and m >= 0 and s >= 0
	pow2s, dm = 1 << s, d * m
	if pow2s != dm:
		n = min(n, d * pow2s // abs(dm - pow2s))
		n -= abs(floor_sum_unsigned(n + 1, pow2s, m, 0) - floor_sum_unsigned(n + 1, d, 1, 0))
	return n

for _ in range(next(mp)):
	print(solve(*(next(mp) for _ in range(4))))
0