QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#93768#6179. 最大平方问题Renshey100 ✓607ms12616kbC++233.8kb2023-04-02 10:32:582023-04-02 10:33:02

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-02 10:33:02]
  • 评测
  • 测评结果:100
  • 用时:607ms
  • 内存:12616kb
  • [2023-04-02 10:32:58]
  • 提交

answer

#include <bits/stdc++.h>
const int maxn = 1200000 + 10;
int a, b, c, n, m, Ans = 1;
int pr[maxn], tot; bool flag[maxn];
long long f[maxn], g[maxn], Delta;
std::unordered_map<long long, int> cnt;
inline long long add (long long x, long long y, long long mod) {return x + y >= mod ? x + y - mod : x + y;}
inline long long mul (long long x, long long y, long long mod) {return (__int128)x * y % mod;}
inline long long power (long long x, long long y, long long mod)
{
    long long z = 1; x %= mod;
    for (; y; y >>= 1, x = mul(x, x, mod)) if (y & 1) z = mul(z, x, mod);
    return z;
}
inline long long Rand (long long l, long long r)
{
    long long x = 0;
    for (int i = 0; i < 4; i++) x = (x << 15) | (rand() & 32767);
    return x % (r - l + 1) + l;
}
namespace Quadratic_Residue
{
    long long p, w, r;
    struct Complex {long long real, imag;} ;
    inline Complex operator * (const Complex &a, const Complex &b)
    {
        Complex c = (Complex){0, 0};
        c.real = add(mul(a.real, b.real, p), mul(mul(a.imag, b.imag, p), r, p), p);
        c.imag = add(mul(a.real, b.imag, p), mul(a.imag, b.real, p), p);
        return c;
    }
    inline Complex Power (Complex x, long long y)
    {
        Complex z = (Complex){1, 0};
        for (; y; y >>= 1, x = x * x) if (y & 1) z = z * x;
        return z;
    }
    inline long long L (long long n)
    {
        if (n == 0) return 0;
        return power(n, (p - 1) / 2, p);
    }
    inline void init (long long q) {p = q;}
    inline long long Sqrt (long long n)
    {
        if (n == 0) return 0;
        if (L(n) == p - 1) return -1;
        do w = Rand(0, p - 1), r = add(mul(w, w, p), p - n, p);
        while (L(r) != p - 1) ;
        return Power((Complex){w, 1}, (p + 1) / 2).real;
    }
}
inline void Divide (long long &x, long long p, int w)
{
    while (x % p == 0) x /= p, (w && cnt[p]++);
}
inline void solve (long long p)
{
    if (p == 2 or a % p == 0)
    {
        for (int j = 1; j <= n; j++) Divide(f[j], p, 1);
    }
    else
    {
        Quadratic_Residue::init(p);
        long long r = Quadratic_Residue::Sqrt((Delta % p + p) % p);
        if (~r)
        {
            long long inv = power(2 * a, p - 2, p);
            long long r1 = mul(p + p - b % p - r, inv, p);
            long long r2 = mul(p + p - b % p + r, inv, p);
            for (long long j = r1; j <= n; j += p) if (j) Divide(f[j], p, 1);
            for (long long j = r2; j <= n; j += p) if (j) Divide(f[j], p, 1);
        }
    }
}
signed main ()
{
    scanf("%d%d%d%d", &a, &b, &c, &n);
    for (int i = 1; i <= n; i++) f[i] = 1LL * a * i * i + 1LL * b * i + c;
    for (int i = 1; i <= 2 * n; i++) g[i] = 1LL * a * i + b;
    m = std::max(2 * n, (int)std::max(pow(f[n], 1.0 / 3), sqrt(g[2 * n])));
    for (int i = 2; i <= m; i++)
    {
        if (!flag[i]) pr[++tot] = i;
        for (int j = 1; j <= tot and i * pr[j] <= m; j++)
        {
            flag[i * pr[j]] = true;
            if (i % pr[j] == 0) break;
        }
    }
    Delta = 1LL * b * b - 4LL * a * c;
    for (int i = 1; i <= tot; i++)
    {
        int p = pr[i]; solve(p);
        if (a % p == 0 or b % p == 0)
        {
            for (int j = 1; j <= 2 * n; j++) Divide(g[j], p, 0);
        }
        else
        {
            int r = 1LL * (p - b % p) * power(a, p - 2, p) % p;
            for (int j = r; j <= 2 * n; j += p) if (j) Divide(g[j], p, 0);
        }
    }
    g[0] = 1; std::sort(g + 1, g + 2 * n + 1);
    for (int i = 1; i <= 2 * n; i++) if (g[i] > g[i - 1]) solve(g[i]);
    for (int i = 1; i <= n; i++) if (f[i] > 1)
    {
        long long x = sqrt(f[i]);
        if (x * x == f[i]) cnt[x] += 2;
    }
    for (auto p: cnt) Ans = 1LL * Ans * power(p.first, p.second / 2 * 2, 998244353) % 998244353;
    return !printf("%d\n", Ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 0ms
memory: 5784kb

input:

17 3 17 14

output:

123798013

result:

ok 1 number(s): "123798013"

Test #2:

score: 5
Accepted
time: 0ms
memory: 5908kb

input:

1 18 0 6

output:

2073600

result:

ok 1 number(s): "2073600"

Test #3:

score: 5
Accepted
time: 0ms
memory: 5836kb

input:

20 6 20 14

output:

315851899

result:

ok 1 number(s): "315851899"

Test #4:

score: 5
Accepted
time: 3ms
memory: 5956kb

input:

81 190 40 125

output:

924770602

result:

ok 1 number(s): "924770602"

Test #5:

score: 5
Accepted
time: 1ms
memory: 5832kb

input:

108 31 110 122

output:

319532761

result:

ok 1 number(s): "319532761"

Test #6:

score: 5
Accepted
time: 2ms
memory: 5924kb

input:

988 194 1412 934

output:

871618879

result:

ok 1 number(s): "871618879"

Test #7:

score: 5
Accepted
time: 5ms
memory: 5900kb

input:

1956 1305 92 1061

output:

681879967

result:

ok 1 number(s): "681879967"

Test #8:

score: 5
Accepted
time: 26ms
memory: 6048kb

input:

75955 165029 149078 5607

output:

739160804

result:

ok 1 number(s): "739160804"

Test #9:

score: 5
Accepted
time: 15ms
memory: 6072kb

input:

3223 4143 3383 4499

output:

139873119

result:

ok 1 number(s): "139873119"

Test #10:

score: 5
Accepted
time: 28ms
memory: 6152kb

input:

9257 3632 1736 9497

output:

158679485

result:

ok 1 number(s): "158679485"

Test #11:

score: 5
Accepted
time: 56ms
memory: 6328kb

input:

13657 8517 9780 16829

output:

499148359

result:

ok 1 number(s): "499148359"

Test #12:

score: 5
Accepted
time: 33ms
memory: 6248kb

input:

152794 105986 145639 8507

output:

432311896

result:

ok 1 number(s): "432311896"

Test #13:

score: 5
Accepted
time: 0ms
memory: 5828kb

input:

2209 13866 42748 227

output:

576767941

result:

ok 1 number(s): "576767941"

Test #14:

score: 5
Accepted
time: 10ms
memory: 5864kb

input:

14279 7290 25915 2265

output:

173729704

result:

ok 1 number(s): "173729704"

Test #15:

score: 5
Accepted
time: 94ms
memory: 6528kb

input:

24703 6569 26356 26534

output:

108700579

result:

ok 1 number(s): "108700579"

Test #16:

score: 5
Accepted
time: 57ms
memory: 6136kb

input:

187348 185285 76358 13718

output:

425402711

result:

ok 1 number(s): "425402711"

Test #17:

score: 5
Accepted
time: 63ms
memory: 6132kb

input:

189507 133399 143282 13930

output:

608644351

result:

ok 1 number(s): "608644351"

Test #18:

score: 5
Accepted
time: 607ms
memory: 12616kb

input:

102698 162019 98595 137546

output:

388084925

result:

ok 1 number(s): "388084925"

Test #19:

score: 5
Accepted
time: 60ms
memory: 6268kb

input:

152381 90362 151048 15578

output:

413943175

result:

ok 1 number(s): "413943175"

Test #20:

score: 5
Accepted
time: 226ms
memory: 7496kb

input:

34402 49259 74598 64198

output:

733372582

result:

ok 1 number(s): "733372582"