Submission #65037759


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using u128 = __int128_t;

template<typename T>
inline T readint() {
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

inline void writeln(ull x) {
    write(x);
    putchar('\n');
}

ull F(ull n, ull d) 
{
    if (!d)
        return (u128)(n - 1) * n * (2 * n - 1) / 6;
    ull t = n - d;
    u128 s1 = (u128)(t - 1) * t / 2;
    u128 s2 = (u128)(t - 1) * t * (2 * t - 1) / 6;
    u128 s3 = (u128)(d - 1) * d / 2;
    u128 s4 = (u128)(d - 1) * d * (2 * d - 1) / 6;
    u128 P1 = s2 + (u128)d * s1;
    u128 P2 = (u128)t * s3 + s4;
    return (ull)(P1 + P2);
}
int main()
{
    int T = readint<int>();
    while (T--)
    {
        ull N = readint<ull>(), M = readint<ull>(), A = readint<ull>(), 
            B1 = readint<ull>(), B2 = readint<ull>();
        if (!A)
        {
            writeln((u128)N * B1 * B2);
            continue;
        }
        ull g = std::gcd(A, M), L = M / g;
        ull b1 = B1 / g, c1 = B1 % g;
        ull b2 = B2 / g, c2 = B2 % g;
        ull d = (b2 + L - b1) % L;
        ull Sd = F(L, d);
        u128 sumL = (u128)g * g * Sd;
        sumL += (u128)g * (c1 + c2) * L * (L - 1) / 2;
        sumL += (u128)c1 * c2 * L;
        ull q = N / L, r = N % L;
        u128 ans = (u128)q * sumL;
        ull x = B1 % M, y = B2 % M;
        for (ull i = 0; i < r; i++)
        {
            ans += (u128)x * y;
            x += A;
            if (x >= M)
                x -= M;
            y += A;
            if (y >= M)
                y -= M;
        }
        writeln((ull)ans);
    }
    return 0;
}

Submission Info

Submission Time
Task G - Sum of Prod of Mod of Linear
User mywwzh
Language C++ 17 (gcc 12.2)
Score 0
Code Size 2072 Byte
Status TLE
Exec Time 5518 ms
Memory 3640 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 650
Status
AC × 1
AC × 6
TLE × 14
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 1 ms 3640 KiB
01_handmade_00.txt AC 21 ms 3444 KiB
01_handmade_01.txt AC 26 ms 3484 KiB
02_small_00.txt AC 9 ms 3456 KiB
02_small_01.txt AC 9 ms 3380 KiB
02_small_02.txt AC 2 ms 3384 KiB
03_random_00.txt TLE 5518 ms 3180 KiB
03_random_01.txt TLE 5518 ms 3276 KiB
03_random_02.txt TLE 5518 ms 3172 KiB
03_random_03.txt TLE 5518 ms 3188 KiB
03_random_04.txt TLE 5518 ms 3208 KiB
03_random_05.txt TLE 5518 ms 3184 KiB
03_random_06.txt TLE 5518 ms 3136 KiB
03_random_07.txt TLE 5518 ms 3228 KiB
03_random_08.txt TLE 5518 ms 3268 KiB
03_random_09.txt TLE 5515 ms 3224 KiB
03_random_10.txt TLE 5518 ms 3204 KiB
03_random_11.txt TLE 5518 ms 3180 KiB
03_random_12.txt TLE 5518 ms 3360 KiB
03_random_13.txt TLE 5518 ms 3216 KiB