結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 Nachia
提出日時 2023-08-24 21:16:57
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 717 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-12-23 07:18:50
合計ジャッジ時間 5,682 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

def floorsum(n,m,a,b) :
    if a == 0 :
        return b//m * n
    ans = 0
    while n != 0 :
        if a >= m :
            ans += a // m * (n * (n-1) // 2)
            a %= m
        if b >= m :
            ans += b // m * n
            b %= m
        yx = a * n + b
        if yx < m :
            return ans
        n = yx // m
        b = yx % m
        yx = a
        a = m
        m = yx
    return ans

q = int(input())
for i in range(q) :
    n,d,m,s = map(int, input().split())
    s = 2 ** s
    dif = abs(s - m*d)
    if dif == 0 :
        print(n, flush=False)
        continue
    n = min(n, d*s // dif)
    ans = n
    ans -= abs(floorsum(n+1,d,1,0) - floorsum(n+1,s,m,0))
    print(ans,flush=False)
0