Submission #64804254


Source Code Expand

def floor_sum(n, m, a, b):
    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 = floor_sum(y, a2, m, m + a2 - b2 - 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

import sys

input = sys.stdin.readline
for _ in range(int(input())):
    n, m, a, b1, b2 = map(int, input().split())
    if a == 0:
        print(n * b1 * b2)
        continue
    if b1 >= b2:
        b1, b2 = b2, b1
    ans = 0
    ans += a * a * (n - 1) * n * (2 * n - 1) // 6
    ans += a * (b1 + b2) * (n - 1) * n // 2
    ans += b1 * b2 * n
    f, g, h = floor_sum(n, m, a, b1)
    ans -= m * (a * h + b2 * f)
    f, g, h = floor_sum(n, m, a, b2)
    ans -= m * (a * h + b1 * f)
    res = g
    x = (a * (n - 1) + b2) // m
    f, g, h = floor_sum(x, a, m, a - b1 - 1)
    res -= h + x * min(n, (x * m + a - b1 - 1) // a)
    f, g, h = floor_sum(x, a, m, a - b2 - 1)
    res += h + x * min(n, (x * m + a - b2 - 1) // a)
    ans += m * m * res
    print(ans)

Submission Info

Submission Time
Task G - Sum of Prod of Mod of Linear
User sounansya
Language Python (PyPy 3.10-v7.3.12)
Score 650
Code Size 1345 Byte
Status AC
Exec Time 870 ms
Memory 95660 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 650 / 650
Status
AC × 1
AC × 20
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_handmade_00.txt, 01_handmade_01.txt, 02_small_00.txt, 02_small_01.txt, 02_small_02.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 03_random_03.txt, 03_random_04.txt, 03_random_05.txt, 03_random_06.txt, 03_random_07.txt, 03_random_08.txt, 03_random_09.txt, 03_random_10.txt, 03_random_11.txt, 03_random_12.txt, 03_random_13.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 58 ms 76656 KiB
01_handmade_00.txt AC 102 ms 83004 KiB
01_handmade_01.txt AC 219 ms 84220 KiB
02_small_00.txt AC 283 ms 89136 KiB
02_small_01.txt AC 292 ms 87780 KiB
02_small_02.txt AC 177 ms 87632 KiB
03_random_00.txt AC 601 ms 92460 KiB
03_random_01.txt AC 571 ms 90540 KiB
03_random_02.txt AC 598 ms 92360 KiB
03_random_03.txt AC 828 ms 94568 KiB
03_random_04.txt AC 830 ms 95592 KiB
03_random_05.txt AC 870 ms 93996 KiB
03_random_06.txt AC 586 ms 92132 KiB
03_random_07.txt AC 854 ms 95496 KiB
03_random_08.txt AC 849 ms 95524 KiB
03_random_09.txt AC 835 ms 95660 KiB
03_random_10.txt AC 518 ms 90032 KiB
03_random_11.txt AC 786 ms 92736 KiB
03_random_12.txt AC 726 ms 91388 KiB
03_random_13.txt AC 675 ms 95492 KiB