結果

問題 No.2362 Inversion Number of Mod of Linear
ユーザー 遭難者遭難者
提出日時 2023-06-19 22:25:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 428 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 484 ms
コンパイル使用メモリ 86,872 KB
実行使用メモリ 80,260 KB
最終ジャッジ日時 2023-09-09 17:38:12
合計ジャッジ時間 3,748 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,016 KB
testcase_01 AC 71 ms
71,384 KB
testcase_02 AC 70 ms
71,320 KB
testcase_03 AC 294 ms
78,676 KB
testcase_04 AC 127 ms
77,880 KB
testcase_05 AC 139 ms
78,312 KB
testcase_06 AC 428 ms
80,260 KB
testcase_07 AC 239 ms
79,100 KB
testcase_08 AC 176 ms
78,788 KB
testcase_09 AC 171 ms
78,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
def q(n, m, a, b, c=0):
    if m == 0:
        return 0, 0, 0
    a1, a2 = a // m, a % m
    b1, b2 = b // m, b % m
    y = (a2 * n + b2) // m
    ff, gg, hh = q(y, a2, m, m + a2 - b2 - 1, c + 1)
    nn = n * (n - 1) // 2
    f = n * y - ff
    g = n * y * y - ff - 2 * hh
    h = nn * y + (ff - gg) // 2
    g += n * (2 * n - 1) * (n - 1) * a1 * a1 // 6
    g += 2 * nn * a1 * b1
    g += b1 * b1 * n
    g += 2 * a1 * h
    g += 2 * b1 * f
    f += a1 * nn + b1 * n
    h += nn * (a1 * (2 * n - 1) + 3 * b1) // 3
    return f, g, h
def main() -> None:
    n, m, x, y = map(int, input().split())
    f1, g1, h1 = q(n, m, x, y)
    f2, g2, h2 = q(n, m, x, 0)
    print(2 * h1 - (n - 1) * f1 + h2 - n * f2)
    return
for i in range(int(input())):
    main()
0