結果

問題 No.2452 Incline
ユーザー 👑 rin204rin204
提出日時 2023-09-01 21:31:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 500 ms / 2,000 ms
コード長 813 bytes
コンパイル時間 974 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 80,016 KB
最終ジャッジ日時 2023-09-01 21:31:27
合計ジャッジ時間 4,945 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,304 KB
testcase_01 AC 68 ms
71,304 KB
testcase_02 AC 68 ms
71,012 KB
testcase_03 AC 484 ms
80,016 KB
testcase_04 AC 500 ms
77,680 KB
testcase_05 AC 500 ms
78,164 KB
testcase_06 AC 444 ms
78,236 KB
testcase_07 AC 484 ms
77,912 KB
testcase_08 AC 442 ms
78,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def floor_sum(n, m, a, b):
    """
    return \\sum_{i=0}^{n-1} ((a*i+b)//m)
    """
    ret = 0
    while True:
        if a >= m:
            ret += (n - 1) * n // 2 * (a // m)
            a %= m
        if b >= m:
            ret += n * (b // m)
            b %= m
        y_max = (a * n + b) // m
        if y_max == 0:
            return ret
        x_max = y_max * m - b
        ret += (n - (x_max + a - 1) // a) * y_max
        n, m, a, b = y_max, a, m, -x_max % a


MOD = 998244353


def solve():
    n, m, l, r = map(int, input().split())
    ans = floor_sum(r + 1, n - 1, 1, 0)
    ans -= floor_sum(l, n - 1, 1, 0)
    l, r = m - r, m - l
    ans += floor_sum(r + 1, n - 1, 1, 0)
    ans -= floor_sum(l, n - 1, 1, 0)
    ans += r - l + 1
    print(ans % MOD)


for _ in range(int(input())):
    solve()
0