結果
問題 |
No.2440 Accuracy of Integer Division Approximate Functions
|
ユーザー |
👑 |
提出日時 | 2023-08-25 14:29:38 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 225 ms / 2,000 ms |
コード長 | 881 bytes |
コンパイル時間 | 196 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 78,336 KB |
最終ジャッジ日時 | 2024-12-23 22:58:58 |
合計ジャッジ時間 | 5,003 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 20 |
ソースコード
import sys readline = sys.stdin.buffer.readline write = sys.stdout.buffer.write # 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(int(readline())): n, d, m, s = map(int, readline().split()) write(bytes("{}\n".format(solve(n, d, m, s)), 'utf-8'))